Spaces:
Paused
Paused
Update roulette.py
Browse files- roulette.py +99 -70
roulette.py
CHANGED
|
@@ -2,101 +2,130 @@ import discord
|
|
| 2 |
from discord import app_commands
|
| 3 |
import random
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
user_cash = {}
|
| 7 |
-
|
| 8 |
-
# Colors for roulette
|
| 9 |
-
ROULETTE_COLORS = ["red", "green", "black"]
|
| 10 |
|
| 11 |
class RouletteView(discord.ui.View):
|
| 12 |
-
def __init__(self, user_id, bet
|
| 13 |
super().__init__(timeout=None)
|
| 14 |
self.user_id = user_id
|
| 15 |
self.bet = bet
|
| 16 |
-
self.choice = choice
|
| 17 |
self.balance = balance
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
await interaction.response.send_message("You are not authorized to use this button.", ephemeral=True)
|
| 23 |
return
|
| 24 |
|
| 25 |
-
# Simulate
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
if
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
self.balance += winnings
|
| 34 |
-
result_text = f"π You won ${winnings:.2f}! The ball landed on **GREEN**."
|
| 35 |
-
else:
|
| 36 |
-
winnings = self.bet * 2
|
| 37 |
-
self.balance += winnings
|
| 38 |
-
result_text = f"π You won ${winnings:.2f}! The ball landed on **{result.upper()}**."
|
| 39 |
else:
|
| 40 |
-
self.balance -= self.bet
|
| 41 |
-
result_text = f"π You lost ${self.bet:.2f}. The ball landed on
|
| 42 |
|
| 43 |
-
# Update user balance
|
| 44 |
-
user_cash[self.user_id] = self.balance
|
| 45 |
|
| 46 |
-
|
| 47 |
-
embed.
|
| 48 |
-
embed.add_field(name="New Balance", value=f"${self.balance:.2f}", inline=False)
|
| 49 |
|
| 50 |
-
#
|
| 51 |
-
self.
|
|
|
|
| 52 |
|
| 53 |
-
await interaction.
|
| 54 |
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
def __init__(self):
|
| 57 |
-
|
| 58 |
-
intents.message_content = True
|
| 59 |
-
super().__init__(intents=intents)
|
| 60 |
-
self.tree = app_commands.CommandTree(self)
|
| 61 |
-
|
| 62 |
-
async def setup_hook(self):
|
| 63 |
-
await self.tree.sync()
|
| 64 |
-
|
| 65 |
-
async def on_ready(self):
|
| 66 |
-
print(f'Logged in as {self.user} (ID: {self.user.id})')
|
| 67 |
-
print('------')
|
| 68 |
-
|
| 69 |
-
client = RouletteBotClient()
|
| 70 |
-
|
| 71 |
-
@client.tree.command(name="roulette", description="Play a game of roulette!")
|
| 72 |
-
@app_commands.describe(
|
| 73 |
-
bet="Amount you want to bet",
|
| 74 |
-
choice="Choose red, green, or black"
|
| 75 |
-
)
|
| 76 |
-
async def roulette(interaction: discord.Interaction, bet: float, choice: str):
|
| 77 |
-
user_id = interaction.user.id
|
| 78 |
-
choice = choice.lower()
|
| 79 |
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
if bet <= 0:
|
| 85 |
-
await interaction.response.send_message("Your bet must be higher than $0.", ephemeral=True)
|
| 86 |
return
|
| 87 |
|
| 88 |
-
balance = user_cash.get(user_id, 1000.0) # Default starting balance
|
| 89 |
-
|
| 90 |
if bet > balance:
|
| 91 |
-
await interaction.response.send_message(
|
|
|
|
|
|
|
|
|
|
| 92 |
return
|
| 93 |
|
| 94 |
-
#
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
|
|
|
| 99 |
embed.add_field(name="Current Balance", value=f"${balance:.2f}", inline=False)
|
| 100 |
|
| 101 |
-
view
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from discord import app_commands
|
| 3 |
import random
|
| 4 |
|
| 5 |
+
from cash import user_cash # Ensure you have a 'cash.py' with a 'user_cash' dictionary
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
class RouletteView(discord.ui.View):
|
| 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("β Your bet must be higher than $0.", ephemeral=True)
|
| 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 |
+
# Create an embed to show the betting status
|
| 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 |
+
# Add the RollRouletteButton to the view
|
| 126 |
+
view.add_item(RollRouletteButton())
|
| 127 |
+
|
| 128 |
+
if interaction.response.is_done():
|
| 129 |
+
await interaction.followup.send(embed=embed, view=view)
|
| 130 |
+
else:
|
| 131 |
+
await interaction.response.send_message(embed=embed, view=view)
|