botbotbotbot / roulette.py
coollsd's picture
Update roulette.py
7cfcec3 verified
import discord
from discord import app_commands
import random
from cash import user_cash
class RouletteGame(discord.ui.View):
def __init__(self, user_id, bet, choice):
super().__init__()
self.user_id = user_id
self.bet = bet
self.choice = choice
self.balance = user_cash.get(user_id, 0)
@discord.ui.button(style=discord.ButtonStyle.primary, label="Spin the Roulette 🎡", custom_id="spin_roulette")
async def spin_roulette_callback(self, interaction: discord.Interaction, button: discord.ui.Button):
if interaction.user.id != self.user_id:
await interaction.response.send_message("You can't spin this.", ephemeral=True)
return
result_number = random.randint(0, 36)
result_color = "green" if result_number == 0 else ("red" if result_number % 2 == 1 else "black")
win = False
payout = -self.bet
if self.choice.isdigit():
win = int(self.choice) == result_number
payout = self.bet * 35 if win else payout
else:
win = self.choice == result_color
payout = self.bet * 2 if win else payout
self.balance += payout
user_cash[self.user_id] = self.balance
embed = discord.Embed(title="Roulette Game", color=0x787878)
embed.add_field(name="Result", value=f"The ball landed on {result_color} {result_number}. You {'won' if win else 'lost'} ${abs(payout):.2f}.", inline=False)
embed.add_field(name="New Balance", value=f"${self.balance:.2f}", inline=False)
await interaction.response.edit_message(embed=embed, view=None)
@app_commands.command(name="roulette", description="Play roulette and bet")
@app_commands.describe(
bet="The amount of money to bet.",
choice="Choose 'red', 'black', 'green', or a number between 0 and 36."
)
@app_commands.choices(
choice=[
app_commands.Choice(name="Red", value="red"),
app_commands.Choice(name="Black", value="black"),
app_commands.Choice(name="Green", value="green")
] + [app_commands.Choice(name=str(i), value=str(i)) for i in range(37)]
)
async def roulette(interaction: discord.Interaction, bet: int, choice: str):
user_id = interaction.user.id
balance = user_cash.get(user_id, 0)
if bet <= 0:
await interaction.response.send_message("Bet higher than 0, please.")
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="Roulette", description=f"{interaction.user.name} is betting ${bet:.2f}", color=0x787878)
embed.add_field(name="Your Bet", value=f"Choice: {choice}", inline=False)
embed.add_field(name="Current Balance", value=f"${balance:.2f}", inline=False)
view = RouletteGame(user_id=user_id, bet=bet, choice=choice)
await interaction.response.send_message(embed=embed, view=view)