Spaces:
Building
Building
Create database.py
Browse files- database.py +23 -0
database.py
ADDED
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import discord
|
2 |
+
from discord import app_commands
|
3 |
+
import json
|
4 |
+
from cash import user_cash
|
5 |
+
|
6 |
+
@app_commands.command(name="database", description="database")
|
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 |
+
json.dump(user_cash, f)
|
12 |
+
await interaction.response.send_message("saved to database.txt")
|
13 |
+
elif action.lower() == "load":
|
14 |
+
try:
|
15 |
+
with open("database.txt", "r") as f:
|
16 |
+
loaded_data = json.load(f)
|
17 |
+
user_cash.clear()
|
18 |
+
user_cash.update(loaded_data)
|
19 |
+
await interaction.response.send_message("Database from database.txt")
|
20 |
+
except FileNotFoundError:
|
21 |
+
await interaction.response.send_message("AAAAAAAAAA.")
|
22 |
+
else:
|
23 |
+
await interaction.response.send_message("Use 'save' or 'load'.")
|