coollsd commited on
Commit
f338b32
β€’
1 Parent(s): 50bbe01

Update roulette.py

Browse files
Files changed (1) hide show
  1. roulette.py +99 -70
roulette.py CHANGED
@@ -2,101 +2,130 @@ 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("You are not authorized to use this button.", ephemeral=True)
23
  return
24
 
25
- # Simulate spinning the roulette
26
- await interaction.response.defer() # Acknowledge the interaction to allow more time
 
 
 
27
 
28
- result = random.choices(ROULETTE_COLORS, weights=[18, 1, 18], k=1)[0]
 
 
 
29
 
30
- if result == self.choice:
31
- if result == "green":
32
- winnings = self.bet * 14 # Green typically has higher payout
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 **{result.upper()}**."
42
 
43
- # Update user balance
44
- user_cash[self.user_id] = self.balance
45
 
46
- embed = discord.Embed(title="🎰 Roulette Result", color=0xFFD700)
47
- embed.add_field(name="Result", value=result_text, inline=False)
48
- embed.add_field(name="New Balance", value=f"${self.balance:.2f}", inline=False)
49
 
50
- # Disable the button after spin to prevent multiple spins
51
- self.disable_all_items()
 
52
 
53
- await interaction.edit_original_response(embed=embed, view=self)
54
 
55
- class RouletteBotClient(discord.Client):
 
 
 
 
 
 
 
56
  def __init__(self):
57
- intents = discord.Intents.default()
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
- if choice not in ROULETTE_COLORS:
81
- await interaction.response.send_message("choose **red**, **green**, or **black**.", ephemeral=True)
82
- return
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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(f"You don't have MONEY. Your current balance is ${balance:.2f}.", ephemeral=True)
 
 
 
92
  return
93
 
94
- # Deduct the bet initially
95
- balance -= bet
96
- user_cash[user_id] = balance
97
-
98
- embed = discord.Embed(title="🎲 Roulette Started", description=f"{interaction.user.mention} has placed a bet of ${bet:.2f} on **{choice.upper()}**.", color=0x787878)
 
99
  embed.add_field(name="Current Balance", value=f"${balance:.2f}", inline=False)
100
 
101
- view = RouletteView(user_id, bet, choice, balance)
102
- await interaction.response.send_message(embed=embed, view=view)
 
 
 
 
 
 
 
 
 
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)