File size: 1,759 Bytes
ebb0b1d
 
 
114843b
ebb0b1d
114843b
66b91a4
ebb0b1d
 
 
114843b
 
 
 
ebb0b1d
 
 
114843b
 
 
ebb0b1d
114843b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ebb0b1d
114843b
ebb0b1d
114843b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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'.")