import discord from discord import app_commands import random from cash import user_cash @app_commands.command(name="dice", description="Roll the dice and bet") async def dice(interaction: discord.Interaction, bet: int): await roll_dice(interaction, bet) async def roll_dice(interaction: discord.Interaction, bet: int): user_id = interaction.user.id balance = user_cash.get(user_id, 0) if bet <= 0: await interaction.response.send_message("Bet Higher than 0 Idiot.") return if bet > balance: await interaction.response.send_message(f"You don't have enough cash. Your current balance is ${balance:.2f}") return embed = discord.Embed(title="Dice Roll", description=f"{interaction.user.name} is betting ${bet:.2f}", color=0x787878) embed.add_field(name="Current Balance", value=f"${balance:.2f}", inline=False) roll_button = discord.ui.Button(style=discord.ButtonStyle.primary, label="Roll the Dice", custom_id="roll_dice") async def roll_dice_callback(interaction: discord.Interaction): nonlocal balance result = random.choice(["win", "lose"]) if result == "win": winnings = bet balance += winnings result_text = f"You won ${winnings:.2f}!" else: balance -= bet result_text = f"You lost ${bet:.2f}." user_cash[user_id] = balance embed.clear_fields() embed.add_field(name="Result", value=result_text, inline=False) embed.add_field(name="New Balance", value=f"${balance:.2f}", inline=False) roll_again_button = discord.ui.Button(style=discord.ButtonStyle.primary, label="Roll Again", custom_id="roll_again") async def roll_again_callback(interaction: discord.Interaction): if interaction.user.id == user_id: await roll_dice(interaction, bet) else: await interaction.response.send_message("you cant roll this", ephemeral=True) roll_again_button.callback = roll_again_callback new_view = discord.ui.View() new_view.add_item(roll_again_button) await interaction.response.edit_message(embed=embed, view=new_view) roll_button.callback = roll_dice_callback view = discord.ui.View() view.add_item(roll_button) if interaction.response.is_done(): await interaction.followup.send(embed=embed, view=view) else: await interaction.response.send_message(embed=embed, view=view)