botbotbotbot / roulette.py
coollsd's picture
Update roulette.py
3a9dd4c verified
raw
history blame
3.34 kB
import discord
from discord import app_commands
import random
from cash import user_cash
@app_commands.command(name="roulette", description="Play roulette and bet")
async def roulette(interaction: discord.Interaction, bet: int, choice: str):
await play_roulette(interaction, bet, choice)
async def play_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 got no money. Your current balance is ${balance:.2f}")
return
if choice not in ["red", "black", "green"] and not (choice.isdigit() and 0 <= int(choice) <= 36):
await interaction.response.send_message("Invalid. Choose 'red', 'black', 'green', or a number between 0 and 36.")
return
embed = discord.Embed(title="Roulette Game", description=f"{interaction.user.name} is betting ${bet:.2f} on {choice}", color=0x787878)
embed.add_field(name="Current Balance", value=f"${balance:.2f}", inline=False)
spin_button = discord.ui.Button(style=discord.ButtonStyle.primary, label="Spin the Roulette", custom_id="spin_roulette")
async def spin_roulette_callback(interaction: discord.Interaction):
nonlocal balance
result_number = random.randint(0, 36)
result_color = "green" if result_number == 0 else ("red" if result_number % 2 == 1 else "black")
win = False
if choice.isdigit():
win = int(choice) == result_number
payout = bet * 35 if win else -bet
else:
win = choice == result_color
payout = bet * 2 if win else -bet
balance += payout
user_cash[user_id] = balance
result_text = f"The ball landed on {result_color} {result_number}. "
result_text += f"You {'won' if win else 'lost'} ${abs(payout):.2f}."
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)
spin_again_button = discord.ui.Button(style=discord.ButtonStyle.primary, label="Spin Again", custom_id="spin_again")
async def spin_again_callback(interaction: discord.Interaction):
if interaction.user.id == user_id:
await play_roulette(interaction, bet, choice)
else:
await interaction.response.send_message("You can't spin this.", ephemeral=True)
spin_again_button.callback = spin_again_callback
new_view = discord.ui.View()
new_view.add_item(spin_again_button)
await interaction.response.edit_message(embed=embed, view=new_view)
spin_button.callback = spin_roulette_callback
view = discord.ui.View()
view.add_item(spin_button)
if interaction.response.is_done():
await interaction.followup.send(embed=embed, view=view)
else:
await interaction.response.send_message(embed=embed, view=view)
# Ensure you have set up your bot and registered this command appropriately in your bot setup code.