Spaces:
Paused
Paused
| import discord | |
| from discord import app_commands | |
| user_cash = {} | |
| def save_database(): | |
| with open("database.txt", "w") as f: | |
| for user_id, cash in user_cash.items(): | |
| f.write(f"{user_id} cash({cash})\n") | |
| def load_database(): | |
| global user_cash | |
| try: | |
| with open("database.txt", "r") as f: | |
| for line in f: | |
| parts = line.strip().split() | |
| if len(parts) == 2 and parts[1].startswith("cash(") and parts[1].endswith(")"): | |
| user_id = int(parts[0]) | |
| cash = int(parts[1][5:-1]) | |
| user_cash[user_id] = cash | |
| except FileNotFoundError: | |
| print("No database found. Creating a new one.") | |
| load_database() | |
| async def cash(interaction: discord.Interaction): | |
| user_id = interaction.user.id | |
| balance = user_cash.get(user_id, 0) | |
| if balance == 0: | |
| user_cash[user_id] = 1000 | |
| balance = 1000 | |
| message = "You are too poor. Here is $1,000!" | |
| else: | |
| message = f"Your current balance is ${balance:,}" | |
| embed = discord.Embed(title="Cash Balance", description=message, color=0x787878) | |
| embed.set_footer(text="Use /dice to bet your cash!") | |
| await interaction.response.send_message(embed=embed) | |
| save_database() # Save the database after each cash operation |