botbotbotbot / database.py
coollsd's picture
Update database.py
66b91a4 verified
raw
history blame
1.1 kB
import discord
from discord import app_commands
import json
from cash import user_cash
@app_commands.command(name="database", description="database")
@app_commands.describe(action="'save' or 'load'")
async def database(interaction: discord.Interaction, action: str):
if action.lower() == "save":
with open("database.txt", "w") as f:
json.dump(user_cash, f)
await interaction.response.send_message("Database saved to database.txt")
elif action.lower() == "load":
try:
with open("database.txt", "r") as f:
loaded_data = json.load(f)
user_cash.clear()
user_cash.update({int(k): v for k, v in loaded_data.items()})
await interaction.response.send_message("Database")
except FileNotFoundError:
await interaction.response.send_message(". '/database save' first to create a database.")
except json.JSONDecodeError:
await interaction.response.send_message("ERRRERERER")
else:
await interaction.response.send_message(" Use 'save' or 'load'.")