Spaces:
Building
Building
Create dice.py
Browse files
dice.py
ADDED
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import discord
|
2 |
+
from discord import app_commands
|
3 |
+
import random
|
4 |
+
|
5 |
+
from cash import user_cash
|
6 |
+
|
7 |
+
@app_commands.command(name="dice", description="Roll the dice and bet")
|
8 |
+
async def dice(interaction: discord.Interaction, bet: int):
|
9 |
+
user_id = interaction.user.id
|
10 |
+
balance = user_cash.get(user_id, 0)
|
11 |
+
|
12 |
+
if bet <= 0:
|
13 |
+
await interaction.response.send_message("Your bet must be greater than 0.")
|
14 |
+
return
|
15 |
+
|
16 |
+
if bet > balance:
|
17 |
+
await interaction.response.send_message(f"You don't have enough cash. Your current balance is ${balance:.2f}")
|
18 |
+
return
|
19 |
+
|
20 |
+
embed = discord.Embed(title="Dice Roll", description=f"{interaction.user.name} is betting ${bet:.2f}", color=0x00ff00)
|
21 |
+
embed.add_field(name="Current Balance", value=f"${balance:.2f}", inline=False)
|
22 |
+
|
23 |
+
roll_button = discord.ui.Button(style=discord.ButtonStyle.primary, label="Roll the Dice", custom_id="roll_dice")
|
24 |
+
|
25 |
+
async def roll_dice_callback(interaction: discord.Interaction):
|
26 |
+
nonlocal balance
|
27 |
+
result = random.choice(["win", "lose"])
|
28 |
+
|
29 |
+
if result == "win":
|
30 |
+
winnings = bet
|
31 |
+
balance += winnings
|
32 |
+
result_text = f"You won ${winnings:.2f}!"
|
33 |
+
else:
|
34 |
+
balance -= bet
|
35 |
+
result_text = f"You lost ${bet:.2f}."
|
36 |
+
|
37 |
+
user_cash[user_id] = balance
|
38 |
+
|
39 |
+
embed.clear_fields()
|
40 |
+
embed.add_field(name="Result", value=result_text, inline=False)
|
41 |
+
embed.add_field(name="New Balance", value=f"${balance:.2f}", inline=False)
|
42 |
+
|
43 |
+
roll_again_button = discord.ui.Button(style=discord.ButtonStyle.primary, label="Roll Again", custom_id="roll_again")
|
44 |
+
roll_again_button.callback = lambda i: dice(i, bet)
|
45 |
+
|
46 |
+
new_view = discord.ui.View()
|
47 |
+
new_view.add_item(roll_again_button)
|
48 |
+
|
49 |
+
await interaction.response.edit_message(embed=embed, view=new_view)
|
50 |
+
|
51 |
+
roll_button.callback = roll_dice_callback
|
52 |
+
|
53 |
+
view = discord.ui.View()
|
54 |
+
view.add_item(roll_button)
|
55 |
+
|
56 |
+
await interaction.response.send_message(embed=embed, view=view)
|