Spaces:
Paused
Paused
Update roulette.py
Browse files- roulette.py +48 -110
roulette.py
CHANGED
|
@@ -2,129 +2,67 @@ import discord
|
|
| 2 |
from discord import app_commands
|
| 3 |
import random
|
| 4 |
|
| 5 |
-
from cash import user_cash
|
| 6 |
|
| 7 |
-
|
| 8 |
-
def __init__(self, user_id: int, bet: int, balance: float):
|
| 9 |
-
super().__init__(timeout=None)
|
| 10 |
-
self.user_id = user_id
|
| 11 |
-
self.bet = bet
|
| 12 |
-
self.balance = balance
|
| 13 |
-
self.add_item(RollRouletteButton())
|
| 14 |
-
|
| 15 |
-
class RollRouletteButton(discord.ui.Button):
|
| 16 |
-
def __init__(self):
|
| 17 |
-
super().__init__(style=discord.ButtonStyle.primary, label="Spin the Wheel", custom_id="roll_roulette")
|
| 18 |
-
|
| 19 |
-
async def callback(self, interaction: discord.Interaction):
|
| 20 |
-
if interaction.user.id != self.view.user_id:
|
| 21 |
-
await interaction.response.send_message("You can't spin this wheel.", ephemeral=True)
|
| 22 |
-
return
|
| 23 |
-
|
| 24 |
-
# Simulate roulette spin
|
| 25 |
-
number = random.randint(0, 36)
|
| 26 |
-
color = self.get_color(number)
|
| 27 |
-
win = False
|
| 28 |
-
payout = 0
|
| 29 |
-
|
| 30 |
-
# Simple roulette logic: win if number is even, lose otherwise
|
| 31 |
-
if number != 0 and number % 2 == 0:
|
| 32 |
-
win = True
|
| 33 |
-
payout = self.view.bet # 1:1 payout
|
| 34 |
-
|
| 35 |
-
if win:
|
| 36 |
-
self.view.balance += payout
|
| 37 |
-
result_text = f"🎉 You won ${payout:.2f}! The ball landed on {number} ({color})."
|
| 38 |
-
else:
|
| 39 |
-
self.view.balance -= self.view.bet
|
| 40 |
-
result_text = f"😞 You lost ${self.view.bet:.2f}. The ball landed on {number} ({color})."
|
| 41 |
-
|
| 42 |
-
# Update user's balance
|
| 43 |
-
user_cash[self.view.user_id] = self.view.balance
|
| 44 |
-
|
| 45 |
-
# Create a new embed with the result
|
| 46 |
-
embed = discord.Embed(title="🎰 Roulette Spin", description=result_text, color=0x787878)
|
| 47 |
-
embed.add_field(name="New Balance", value=f"${self.view.balance:.2f}", inline=False)
|
| 48 |
-
|
| 49 |
-
# Update the view with a "Spin Again" button
|
| 50 |
-
self.view.clear_items()
|
| 51 |
-
self.view.add_item(SpinAgainButton())
|
| 52 |
-
|
| 53 |
-
await interaction.response.edit_message(embed=embed, view=self.view)
|
| 54 |
-
|
| 55 |
-
@staticmethod
|
| 56 |
-
def get_color(number: int) -> str:
|
| 57 |
-
if number == 0:
|
| 58 |
-
return "Green"
|
| 59 |
-
# Simplified color assignment for demonstration
|
| 60 |
-
return "Red" if number % 2 == 0 else "Black"
|
| 61 |
-
|
| 62 |
-
class SpinAgainButton(discord.ui.Button):
|
| 63 |
-
def __init__(self):
|
| 64 |
-
super().__init__(style=discord.ButtonStyle.success, label="Spin Again", custom_id="spin_again")
|
| 65 |
-
|
| 66 |
-
async def callback(self, interaction: discord.Interaction):
|
| 67 |
-
if interaction.user.id != self.view.user_id:
|
| 68 |
-
await interaction.response.send_message("You can't spin this wheel.", ephemeral=True)
|
| 69 |
-
return
|
| 70 |
-
|
| 71 |
-
# Prevent betting if balance is insufficient
|
| 72 |
-
if self.view.bet > self.view.balance:
|
| 73 |
-
await interaction.response.send_message(
|
| 74 |
-
f"You don't have enough cash to spin again. Your current balance is ${self.view.balance:.2f}",
|
| 75 |
-
ephemeral=True
|
| 76 |
-
)
|
| 77 |
-
return
|
| 78 |
-
|
| 79 |
-
await interaction.response.defer()
|
| 80 |
-
|
| 81 |
-
# Create an embed to show the betting status
|
| 82 |
-
embed = discord.Embed(
|
| 83 |
-
title="🎰 Roulette Spin",
|
| 84 |
-
description=f"{interaction.user.name} is betting ${self.view.bet:.2f}",
|
| 85 |
-
color=0xFFA500
|
| 86 |
-
)
|
| 87 |
-
embed.add_field(name="Current Balance", value=f"${self.view.balance:.2f}", inline=False)
|
| 88 |
-
|
| 89 |
-
# Replace buttons with a new roll button
|
| 90 |
-
self.view.clear_items()
|
| 91 |
-
self.view.add_item(RollRouletteButton())
|
| 92 |
-
|
| 93 |
-
await interaction.edit_message(embed=embed, view=self.view)
|
| 94 |
-
|
| 95 |
-
@app_commands.command(name="roulette", description="Play roulette and place a bet.")
|
| 96 |
async def roulette(interaction: discord.Interaction, bet: int):
|
| 97 |
await play_roulette(interaction, bet)
|
| 98 |
|
| 99 |
async def play_roulette(interaction: discord.Interaction, bet: int):
|
| 100 |
user_id = interaction.user.id
|
| 101 |
balance = user_cash.get(user_id, 0)
|
| 102 |
-
|
| 103 |
if bet <= 0:
|
| 104 |
-
await interaction.response.send_message("
|
| 105 |
return
|
| 106 |
-
|
| 107 |
if bet > balance:
|
| 108 |
-
await interaction.response.send_message(
|
| 109 |
-
f"❌ You don't have enough cash to bet ${bet:.2f}. Your current balance is ${balance:.2f}.",
|
| 110 |
-
ephemeral=True
|
| 111 |
-
)
|
| 112 |
return
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
embed = discord.Embed(
|
| 116 |
-
title="🎰 Roulette Spin",
|
| 117 |
-
description=f"{interaction.user.name} is betting ${bet:.2f}",
|
| 118 |
-
color=0xFFA500
|
| 119 |
-
)
|
| 120 |
embed.add_field(name="Current Balance", value=f"${balance:.2f}", inline=False)
|
| 121 |
-
|
| 122 |
-
# Initialize the view with the Spin button
|
| 123 |
-
view = RouletteView(user_id=user_id, bet=bet, balance=balance)
|
| 124 |
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
if interaction.response.is_done():
|
| 129 |
await interaction.followup.send(embed=embed, view=view)
|
| 130 |
else:
|
|
|
|
| 2 |
from discord import app_commands
|
| 3 |
import random
|
| 4 |
|
| 5 |
+
from cash import user_cash
|
| 6 |
|
| 7 |
+
@app_commands.command(name="roulette", description="Play roulette and bet")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
async def roulette(interaction: discord.Interaction, bet: int):
|
| 9 |
await play_roulette(interaction, bet)
|
| 10 |
|
| 11 |
async def play_roulette(interaction: discord.Interaction, bet: int):
|
| 12 |
user_id = interaction.user.id
|
| 13 |
balance = user_cash.get(user_id, 0)
|
| 14 |
+
|
| 15 |
if bet <= 0:
|
| 16 |
+
await interaction.response.send_message("Bet higher than 0, please.")
|
| 17 |
return
|
| 18 |
+
|
| 19 |
if bet > balance:
|
| 20 |
+
await interaction.response.send_message(f"You don't have enough cash. Your current balance is ${balance:.2f}")
|
|
|
|
|
|
|
|
|
|
| 21 |
return
|
| 22 |
+
|
| 23 |
+
embed = discord.Embed(title="Roulette Game", description=f"{interaction.user.name} is betting ${bet:.2f}", color=0x3498db)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
embed.add_field(name="Current Balance", value=f"${balance:.2f}", inline=False)
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
spin_button = discord.ui.Button(style=discord.ButtonStyle.primary, label="Spin the Roulette", custom_id="spin_roulette")
|
| 27 |
+
|
| 28 |
+
async def spin_roulette_callback(interaction: discord.Interaction):
|
| 29 |
+
nonlocal balance
|
| 30 |
+
result = random.choice(["win", "lose"])
|
| 31 |
+
|
| 32 |
+
if result == "win":
|
| 33 |
+
winnings = bet * 2 # Assuming a simple win condition for demonstration
|
| 34 |
+
balance += winnings
|
| 35 |
+
result_text = f"You won ${winnings:.2f}!"
|
| 36 |
+
else:
|
| 37 |
+
balance -= bet
|
| 38 |
+
result_text = f"You lost ${bet:.2f}."
|
| 39 |
+
|
| 40 |
+
user_cash[user_id] = balance
|
| 41 |
+
|
| 42 |
+
embed.clear_fields()
|
| 43 |
+
embed.add_field(name="Result", value=result_text, inline=False)
|
| 44 |
+
embed.add_field(name="New Balance", value=f"${balance:.2f}", inline=False)
|
| 45 |
+
|
| 46 |
+
spin_again_button = discord.ui.Button(style=discord.ButtonStyle.primary, label="Spin Again", custom_id="spin_again")
|
| 47 |
+
|
| 48 |
+
async def spin_again_callback(interaction: discord.Interaction):
|
| 49 |
+
if interaction.user.id == user_id:
|
| 50 |
+
await play_roulette(interaction, bet)
|
| 51 |
+
else:
|
| 52 |
+
await interaction.response.send_message("You can't spin this.", ephemeral=True)
|
| 53 |
+
|
| 54 |
+
spin_again_button.callback = spin_again_callback
|
| 55 |
+
|
| 56 |
+
new_view = discord.ui.View()
|
| 57 |
+
new_view.add_item(spin_again_button)
|
| 58 |
+
|
| 59 |
+
await interaction.response.edit_message(embed=embed, view=new_view)
|
| 60 |
+
|
| 61 |
+
spin_button.callback = spin_roulette_callback
|
| 62 |
+
|
| 63 |
+
view = discord.ui.View()
|
| 64 |
+
view.add_item(spin_button)
|
| 65 |
+
|
| 66 |
if interaction.response.is_done():
|
| 67 |
await interaction.followup.send(embed=embed, view=view)
|
| 68 |
else:
|