Spaces:
Building
Building
Create roulette.py
Browse files- roulette.py +97 -0
roulette.py
ADDED
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import discord
|
2 |
+
from discord import app_commands
|
3 |
+
import random
|
4 |
+
|
5 |
+
# Simple in-memory storage for user balances
|
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, choice, balance):
|
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 |
+
@discord.ui.button(label="Spin the Roulette", style=discord.ButtonStyle.primary, custom_id="spin_button")
|
20 |
+
async def spin_button_callback(self, interaction: discord.Interaction, button: discord.ui.Button):
|
21 |
+
if interaction.user.id != self.user_id:
|
22 |
+
await interaction.response.send_message("non.", ephemeral=True)
|
23 |
+
return
|
24 |
+
|
25 |
+
result = random.choices(ROULETTE_COLORS, weights=[18, 1, 18], k=1)[0]
|
26 |
+
|
27 |
+
if result == self.choice:
|
28 |
+
if result == "green":
|
29 |
+
winnings = self.bet * 14 # Green typically has higher payout
|
30 |
+
self.balance += winnings
|
31 |
+
result_text = f"π You won ${winnings:.2f}! The ball landed on **GREEN**."
|
32 |
+
else:
|
33 |
+
winnings = self.bet * 2
|
34 |
+
self.balance += winnings
|
35 |
+
result_text = f"π You won ${winnings:.2f}! The ball landed on **{result.upper()}**."
|
36 |
+
else:
|
37 |
+
self.balance -= self.bet
|
38 |
+
result_text = f"π You lost ${self.bet:.2f}. The ball landed on **{result.upper()}**."
|
39 |
+
|
40 |
+
# Update user balance
|
41 |
+
user_cash[self.user_id] = self.balance
|
42 |
+
|
43 |
+
embed = discord.Embed(title="π° Roulette Result", color=0xFFD700)
|
44 |
+
embed.add_field(name="Result", value=result_text, inline=False)
|
45 |
+
embed.add_field(name="New Balance", value=f"${self.balance:.2f}", inline=False)
|
46 |
+
|
47 |
+
# Disable the button after spin
|
48 |
+
self.disable_all_items()
|
49 |
+
|
50 |
+
await interaction.response.edit_message(embed=embed, view=self)
|
51 |
+
|
52 |
+
class RouletteBotClient(discord.Client):
|
53 |
+
def __init__(self):
|
54 |
+
intents = discord.Intents.default()
|
55 |
+
intents.message_content = True
|
56 |
+
super().__init__(intents=intents)
|
57 |
+
self.tree = app_commands.CommandTree(self)
|
58 |
+
|
59 |
+
async def on_ready(self):
|
60 |
+
await self.tree.sync()
|
61 |
+
print(f'Logged in as {self.user} (ID: {self.user.id})')
|
62 |
+
print('------')
|
63 |
+
|
64 |
+
client = RouletteBotClient()
|
65 |
+
|
66 |
+
@client.tree.command(name="roulette", description="roulette simple as that")
|
67 |
+
@app_commands.describe(
|
68 |
+
bet="Amount you want to bet",
|
69 |
+
choice="Choose red, green, or black"
|
70 |
+
)
|
71 |
+
async def roulette(interaction: discord.Interaction, bet: float, choice: str):
|
72 |
+
user_id = interaction.user.id
|
73 |
+
choice = choice.lower()
|
74 |
+
|
75 |
+
if choice not in ROULETTE_COLORS:
|
76 |
+
await interaction.response.send_message("Invalid choice! Please choose **red**, **green**, or **black**.", ephemeral=True)
|
77 |
+
return
|
78 |
+
|
79 |
+
if bet <= 0:
|
80 |
+
await interaction.response.send_message("Your bet must be higher than $0.", ephemeral=True)
|
81 |
+
return
|
82 |
+
|
83 |
+
balance = user_cash.get(user_id, 1000.0) # Default starting balance
|
84 |
+
|
85 |
+
if bet > balance:
|
86 |
+
await interaction.response.send_message(f"you dont got money this is how much you got ${balance:.2f}.", ephemeral=True)
|
87 |
+
return
|
88 |
+
|
89 |
+
# Deduct the bet initially
|
90 |
+
balance -= bet
|
91 |
+
user_cash[user_id] = balance
|
92 |
+
|
93 |
+
embed = discord.Embed(title="π² Roulette Started", description=f"{interaction.user.mention} has placed a bet of ${bet:.2f} on **{choice.upper()}**.", color=0x787878)
|
94 |
+
embed.add_field(name="Current Balance", value=f"${balance:.2f}", inline=False)
|
95 |
+
|
96 |
+
view = RouletteView(user_id, bet, choice, balance)
|
97 |
+
view.message = await interaction.response.send_message(embed=embed, view=view)
|