import discord from discord import app_commands import random from cash import user_cash # Ensure you have a 'cash.py' with a 'user_cash' dictionary class RouletteView(discord.ui.View): def __init__(self, user_id: int, bet: int, balance: float): super().__init__(timeout=None) self.user_id = user_id self.bet = bet self.balance = balance self.add_item(RollRouletteButton()) class RollRouletteButton(discord.ui.Button): def __init__(self): super().__init__(style=discord.ButtonStyle.primary, label="Spin the Wheel", custom_id="roll_roulette") async def callback(self, interaction: discord.Interaction): if interaction.user.id != self.view.user_id: await interaction.response.send_message("You can't spin this wheel.", ephemeral=True) return # Simulate roulette spin number = random.randint(0, 36) color = self.get_color(number) win = False payout = 0 # Simple roulette logic: win if number is even, lose otherwise if number != 0 and number % 2 == 0: win = True payout = self.view.bet # 1:1 payout if win: self.view.balance += payout result_text = f"🎉 You won ${payout:.2f}! The ball landed on {number} ({color})." else: self.view.balance -= self.view.bet result_text = f"😞 You lost ${self.view.bet:.2f}. The ball landed on {number} ({color})." # Update user's balance user_cash[self.view.user_id] = self.view.balance # Create a new embed with the result embed = discord.Embed(title="🎰 Roulette Spin", description=result_text, color=0x787878) embed.add_field(name="New Balance", value=f"${self.view.balance:.2f}", inline=False) # Update the view with a "Spin Again" button self.view.clear_items() self.view.add_item(SpinAgainButton()) await interaction.response.edit_message(embed=embed, view=self.view) @staticmethod def get_color(number: int) -> str: if number == 0: return "Green" # Simplified color assignment for demonstration return "Red" if number % 2 == 0 else "Black" class SpinAgainButton(discord.ui.Button): def __init__(self): super().__init__(style=discord.ButtonStyle.success, label="Spin Again", custom_id="spin_again") async def callback(self, interaction: discord.Interaction): if interaction.user.id != self.view.user_id: await interaction.response.send_message("You can't spin this wheel.", ephemeral=True) return # Prevent betting if balance is insufficient if self.view.bet > self.view.balance: await interaction.response.send_message( f"You don't have enough cash to spin again. Your current balance is ${self.view.balance:.2f}", ephemeral=True ) return await interaction.response.defer() # Create an embed to show the betting status embed = discord.Embed( title="🎰 Roulette Spin", description=f"{interaction.user.name} is betting ${self.view.bet:.2f}", color=0xFFA500 ) embed.add_field(name="Current Balance", value=f"${self.view.balance:.2f}", inline=False) # Replace buttons with a new roll button self.view.clear_items() self.view.add_item(RollRouletteButton()) await interaction.edit_message(embed=embed, view=self.view) @app_commands.command(name="roulette", description="Play roulette and place a bet.") async def roulette(interaction: discord.Interaction, bet: int): await play_roulette(interaction, bet) async def play_roulette(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("❌ Your bet must be higher than $0.", ephemeral=True) return if bet > balance: await interaction.response.send_message( f"❌ You don't have enough cash to bet ${bet:.2f}. Your current balance is ${balance:.2f}.", ephemeral=True ) return # Create an embed to show the betting status embed = discord.Embed( title="🎰 Roulette Spin", description=f"{interaction.user.name} is betting ${bet:.2f}", color=0xFFA500 ) embed.add_field(name="Current Balance", value=f"${balance:.2f}", inline=False) # Initialize the view with the Spin button view = RouletteView(user_id=user_id, bet=bet, balance=balance) # Add the RollRouletteButton to the view view.add_item(RollRouletteButton()) if interaction.response.is_done(): await interaction.followup.send(embed=embed, view=view) else: await interaction.response.send_message(embed=embed, view=view)