coollsd commited on
Commit
4798cd4
·
verified ·
1 Parent(s): ceb6eb9

Update roulette.py

Browse files
Files changed (1) hide show
  1. roulette.py +46 -58
roulette.py CHANGED
@@ -4,11 +4,51 @@ 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, choice: str):
9
- await play_roulette(interaction, bet, choice)
10
-
11
- async def play_roulette(interaction: discord.Interaction, bet: int, choice: str):
12
  user_id = interaction.user.id
13
  balance = user_cash.get(user_id, 0)
14
 
@@ -19,63 +59,11 @@ async def play_roulette(interaction: discord.Interaction, bet: int, choice: str)
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
- valid_choices = ["red", "black", "green"] + [str(i) for i in range(37)]
24
- if choice not in valid_choices:
25
- await interaction.response.send_message("Invalid choice. Choose 'red', 'black', 'green', or a number between 0 and 36.")
26
- return
27
 
28
  embed = discord.Embed(title="Roulette Game", description=f"{interaction.user.name} is betting ${bet:.2f}", color=0x787878)
29
  embed.add_field(name="Your Bet", value=f"Choice: {choice}", inline=False)
30
  embed.add_field(name="Current Balance", value=f"${balance:.2f}", inline=False)
 
 
31
 
32
- spin_button = discord.ui.Button(style=discord.ButtonStyle.primary, label="Spin the Roulette 🎡", custom_id="spin_roulette")
33
-
34
- async def spin_roulette_callback(interaction: discord.Interaction):
35
- nonlocal balance
36
- result_number = random.randint(0, 36)
37
- result_color = "green" if result_number == 0 else ("red" if result_number % 2 == 1 else "black")
38
-
39
- win = False
40
- payout = -bet
41
- if choice.isdigit():
42
- win = int(choice) == result_number
43
- payout = bet * 35 if win else payout
44
- else:
45
- win = choice == result_color
46
- payout = bet * 2 if win else payout
47
-
48
- balance += payout
49
- user_cash[user_id] = balance
50
-
51
- result_text = f"The ball landed on {result_color} {result_number}. "
52
- result_text += f"You {'won' if win else 'lost'} ${abs(payout):.2f}."
53
-
54
- embed.clear_fields()
55
- embed.add_field(name="Result", value=result_text, inline=False)
56
- embed.add_field(name="New Balance", value=f"${balance:.2f}", inline=False)
57
-
58
- spin_again_button = discord.ui.Button(style=discord.ButtonStyle.primary, label="Spin Again 🔄", custom_id="spin_again")
59
-
60
- async def spin_again_callback(interaction: discord.Interaction):
61
- if interaction.user.id == user_id:
62
- await play_roulette(interaction, bet, choice)
63
- else:
64
- await interaction.response.send_message("You can't spin this.", ephemeral=True)
65
-
66
- spin_again_button.callback = spin_again_callback
67
-
68
- new_view = discord.ui.View()
69
- new_view.add_item(spin_again_button)
70
-
71
- await interaction.response.edit_message(embed=embed, view=new_view)
72
-
73
- spin_button.callback = spin_roulette_callback
74
-
75
- view = discord.ui.View()
76
- view.add_item(spin_button)
77
-
78
- if interaction.response.is_done():
79
- await interaction.followup.send(embed=embed, view=view)
80
- else:
81
- await interaction.response.send_message(embed=embed, view=view)
 
4
 
5
  from cash import user_cash
6
 
7
+ class RouletteGame(discord.ui.View):
8
+ def __init__(self, user_id, bet, choice):
9
+ super().__init__()
10
+ self.user_id = user_id
11
+ self.bet = bet
12
+ self.choice = choice
13
+ self.balance = user_cash.get(user_id, 0)
14
+
15
+ @discord.ui.button(style=discord.ButtonStyle.primary, label="Spin the Roulette 🎡", custom_id="spin_roulette")
16
+ async def spin_roulette_callback(self, interaction: discord.Interaction, button: discord.ui.Button):
17
+ if interaction.user.id != self.user_id:
18
+ await interaction.response.send_message("You can't spin this.", ephemeral=True)
19
+ return
20
+
21
+ result_number = random.randint(0, 36)
22
+ result_color = "green" if result_number == 0 else ("red" if result_number % 2 == 1 else "black")
23
+
24
+ win = False
25
+ payout = -self.bet
26
+ if self.choice.isdigit():
27
+ win = int(self.choice) == result_number
28
+ payout = self.bet * 35 if win else payout
29
+ else:
30
+ win = self.choice == result_color
31
+ payout = self.bet * 2 if win else payout
32
+
33
+ self.balance += payout
34
+ user_cash[self.user_id] = self.balance
35
+
36
+ embed = discord.Embed(title="Roulette Game", color=0x787878)
37
+ embed.add_field(name="Result", value=f"The ball landed on {result_color} {result_number}. You {'won' if win else 'lost'} ${abs(payout):.2f}.", inline=False)
38
+ embed.add_field(name="New Balance", value=f"${self.balance:.2f}", inline=False)
39
+
40
+ await interaction.response.edit_message(embed=embed, view=None)
41
+
42
  @app_commands.command(name="roulette", description="Play roulette and bet")
43
+ @app_commands.describe(choice="Choose 'red', 'black', 'green', or a number between 0 and 36.")
44
+ @app_commands.choices(
45
+ choice=[
46
+ app_commands.Choice(name="Red", value="red"),
47
+ app_commands.Choice(name="Black", value="black"),
48
+ app_commands.Choice(name="Green", value="green")
49
+ ] + [app_commands.Choice(name=str(i), value=str(i)) for i in range(37)]
50
+ )
51
  async def roulette(interaction: discord.Interaction, bet: int, choice: str):
 
 
 
52
  user_id = interaction.user.id
53
  balance = user_cash.get(user_id, 0)
54
 
 
59
  if bet > balance:
60
  await interaction.response.send_message(f"You don't have enough cash. Your current balance is ${balance:.2f}")
61
  return
 
 
 
 
 
62
 
63
  embed = discord.Embed(title="Roulette Game", description=f"{interaction.user.name} is betting ${bet:.2f}", color=0x787878)
64
  embed.add_field(name="Your Bet", value=f"Choice: {choice}", inline=False)
65
  embed.add_field(name="Current Balance", value=f"${balance:.2f}", inline=False)
66
+
67
+ view = RouletteGame(user_id=user_id, bet=bet, choice=choice)
68
 
69
+ await interaction.response.send_message(embed=embed, view=view)