import discord from discord import app_commands from cash import user_cash import io @app_commands.command(name="database", description="Save or load ") @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: for user_id, cash in user_cash.items(): f.write(f"{user_id} cash ({cash})\n") await interaction.response.send_message("Database saved") elif action.lower() == "load": try: with open("database.txt", "r") as f: content = f.read() if content: user_cash.clear() for line in content.split('\n'): if line: user_id, cash_info = line.split(' cash ') cash = int(cash_info.strip('()')) user_cash[int(user_id)] = cash # Create a text file with the current database content buffer = io.StringIO() for user_id, cash in user_cash.items(): buffer.write(f"{user_id} cash ({cash})\n") buffer.seek(0) # Send the file file = discord.File(fp=buffer, filename="current_database.txt") await interaction.response.send_message("Current database :", file=file) else: await interaction.response.send_message("The database is empty.") except FileNotFoundError: await interaction.response.send_message("errer") else: await interaction.response.send_message("'save' or 'load'.")