coollsd commited on
Commit
87f59cd
·
verified ·
1 Parent(s): 7f28b05

Update database.py

Browse files
Files changed (1) hide show
  1. database.py +10 -37
database.py CHANGED
@@ -1,42 +1,15 @@
1
  import discord
2
  from discord import app_commands
3
- from cash import user_cash
4
- import io
5
 
6
- @app_commands.command(name="database", description="Save or load ")
7
- @app_commands.describe(action="'save' or 'load'")
8
- async def database(interaction: discord.Interaction, action: str):
9
- if action.lower() == "save":
10
- with open("database.txt", "w") as f:
11
- for user_id, cash in user_cash.items():
12
- f.write(f"{user_id} cash ({cash})\n")
13
- await interaction.response.send_message("Database saved")
14
 
15
- elif action.lower() == "load":
16
- try:
17
- with open("database.txt", "r") as f:
18
- content = f.read()
19
-
20
- if content:
21
- user_cash.clear()
22
- for line in content.split('\n'):
23
- if line:
24
- user_id, cash_info = line.split(' cash ')
25
- cash = int(cash_info.strip('()'))
26
- user_cash[int(user_id)] = cash
27
-
28
- # Create a text file with the current database content
29
- buffer = io.StringIO()
30
- for user_id, cash in user_cash.items():
31
- buffer.write(f"{user_id} cash ({cash})\n")
32
- buffer.seek(0)
33
-
34
- # Send the file
35
- file = discord.File(fp=buffer, filename="current_database.txt")
36
- await interaction.response.send_message("Current database :", file=file)
37
- else:
38
- await interaction.response.send_message("The database is empty.")
39
- except FileNotFoundError:
40
- await interaction.response.send_message("errer")
41
  else:
42
- await interaction.response.send_message("'save' or 'load'.")
 
1
  import discord
2
  from discord import app_commands
3
+ from cash import user_cash, save_database
 
4
 
5
+ @app_commands.command(name="database", description="database")
6
+ async def database(interaction: discord.Interaction):
7
+ save_database()
 
 
 
 
 
8
 
9
+ with open("database.txt", "r") as f:
10
+ content = f.read()
11
+
12
+ if content:
13
+ await interaction.response.send_message(f"database :\n```\n{content}\n```")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  else:
15
+ await interaction.response.send_message("The database is empty.")