coollsd commited on
Commit
5aca793
·
verified ·
1 Parent(s): 114843b

Update cash.py

Browse files
Files changed (1) hide show
  1. cash.py +16 -11
cash.py CHANGED
@@ -1,23 +1,28 @@
1
  import discord
2
  from discord import app_commands
3
- import json
4
- import os
5
 
6
  user_cash = {}
7
 
8
  def save_database():
9
  with open("database.txt", "w") as f:
10
- json.dump(user_cash, f)
 
11
 
12
  def load_database():
13
  global user_cash
14
- if os.path.exists("database.txt"):
15
- try:
16
- with open("database.txt", "r") as f:
17
- loaded_data = json.load(f)
18
- user_cash = {int(k): v for k, v in loaded_data.items()}
19
- except json.JSONDecodeError:
20
- print("errer")
 
 
 
 
 
 
21
 
22
  # Load the database when the module is imported
23
  load_database()
@@ -30,7 +35,7 @@ async def cash(interaction: discord.Interaction):
30
  if balance == 0:
31
  user_cash[user_id] = 1000
32
  balance = 1000
33
- message = "you are too poor so here is $1000"
34
  else:
35
  message = f"Your current balance is ${balance:.2f}"
36
 
 
1
  import discord
2
  from discord import app_commands
 
 
3
 
4
  user_cash = {}
5
 
6
  def save_database():
7
  with open("database.txt", "w") as f:
8
+ for user_id, cash in user_cash.items():
9
+ f.write(f"{user_id} cash ({cash})\n")
10
 
11
  def load_database():
12
  global user_cash
13
+ try:
14
+ with open("database.txt", "r") as f:
15
+ content = f.read()
16
+
17
+ if content:
18
+ user_cash.clear()
19
+ for line in content.split('\n'):
20
+ if line:
21
+ user_id, cash_info = line.split(' cash ')
22
+ cash = int(cash_info.strip('()'))
23
+ user_cash[int(user_id)] = cash
24
+ except FileNotFoundError:
25
+ print("empty database.")
26
 
27
  # Load the database when the module is imported
28
  load_database()
 
35
  if balance == 0:
36
  user_cash[user_id] = 1000
37
  balance = 1000
38
+ message = "you are too poor is here $1000"
39
  else:
40
  message = f"Your current balance is ${balance:.2f}"
41