Spaces:
Building
Building
Update sportbet.py
Browse files- sportbet.py +34 -15
sportbet.py
CHANGED
@@ -12,6 +12,13 @@ async def fetch_nhl_scores():
|
|
12 |
async with session.get("https://nhl-score-api.herokuapp.com/api/scores/latest") as response:
|
13 |
return await response.json()
|
14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
class GameSelect(discord.ui.Select):
|
16 |
def __init__(self, games):
|
17 |
options = [
|
@@ -43,12 +50,14 @@ class BetModal(discord.ui.Modal, title="Place Your Bet"):
|
|
43 |
async def on_submit(self, interaction: discord.Interaction):
|
44 |
try:
|
45 |
bet_amount = int(self.bet_amount.value)
|
|
|
|
|
46 |
if bet_amount <= 0:
|
47 |
raise ValueError("Bet more than 0 dollars")
|
48 |
-
if bet_amount >
|
49 |
-
raise ValueError("
|
50 |
|
51 |
-
user_cash[self.user_id]
|
52 |
await interaction.response.send_message(f"Bet placed on {self.team} for ${bet_amount}")
|
53 |
|
54 |
user = await interaction.client.fetch_user(self.user_id)
|
@@ -57,6 +66,7 @@ class BetModal(discord.ui.Modal, title="Place Your Bet"):
|
|
57 |
embed.add_field(name="Amount", value=f"${bet_amount}", inline=False)
|
58 |
embed.add_field(name="Game", value=f"{self.game_data['teams']['away']['teamName']} vs {self.game_data['teams']['home']['teamName']}", inline=False)
|
59 |
embed.add_field(name="Start Time", value=f"<t:{int(datetime.fromisoformat(self.game_data['startTime'].replace('Z', '+00:00')).timestamp())}:R>", inline=False)
|
|
|
60 |
await user.send(embed=embed)
|
61 |
|
62 |
if self.user_id not in user_bets:
|
@@ -75,7 +85,6 @@ class BetModal(discord.ui.Modal, title="Place Your Bet"):
|
|
75 |
game_start = datetime.fromisoformat(self.game_data['startTime'].replace('Z', '+00:00'))
|
76 |
await asyncio.sleep((game_start - datetime.now(timezone.utc)).total_seconds())
|
77 |
|
78 |
-
# Notify user that the game has started
|
79 |
await interaction.user.send(f"GAME STARTED! Your team {self.team} is now playing!")
|
80 |
|
81 |
previous_scores = {'away': 0, 'home': 0}
|
@@ -99,10 +108,11 @@ class BetModal(discord.ui.Modal, title="Place Your Bet"):
|
|
99 |
if game['teams'][winner]['abbreviation'] == self.team:
|
100 |
winnings = bet_amount * 2
|
101 |
user_cash[self.user_id] += winnings
|
102 |
-
await interaction.user.send(f"Congratulations! Your team won. You won ${winnings}!")
|
103 |
else:
|
104 |
await interaction.user.send(f"Sorry, your team lost CRY")
|
105 |
|
|
|
106 |
user_bets[self.user_id] = [bet for bet in user_bets[self.user_id] if bet['game_data'] != self.game_data]
|
107 |
break
|
108 |
|
@@ -114,12 +124,26 @@ class SportBetView(discord.ui.View):
|
|
114 |
|
115 |
@discord.ui.button(label="Bet Again", style=discord.ButtonStyle.primary)
|
116 |
async def bet_again(self, interaction: discord.Interaction, button: discord.ui.Button):
|
117 |
-
await
|
118 |
|
119 |
@discord.ui.button(label="View Bets", style=discord.ButtonStyle.secondary)
|
120 |
async def view_bets(self, interaction: discord.Interaction, button: discord.ui.Button):
|
121 |
await show_current_bets(interaction)
|
122 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
123 |
async def show_game_selection(interaction: discord.Interaction):
|
124 |
scores = await fetch_nhl_scores()
|
125 |
upcoming_games = [game for game in scores['games'] if game['status']['state'] == 'PREVIEW']
|
@@ -132,7 +156,7 @@ async def show_game_selection(interaction: discord.Interaction):
|
|
132 |
game_select = GameSelect(upcoming_games)
|
133 |
view.add_item(game_select)
|
134 |
|
135 |
-
await interaction.response.
|
136 |
|
137 |
async def game_callback(interaction: discord.Interaction):
|
138 |
selected_game = next(game for game in upcoming_games if f"{game['teams']['away']['abbreviation']}_{game['teams']['home']['abbreviation']}" == game_select.values[0])
|
@@ -176,7 +200,7 @@ async def show_current_bets(interaction: discord.Interaction):
|
|
176 |
|
177 |
embed.add_field(name=f"Bet {i+1}", value=(
|
178 |
f"Team: {bet['team']}\n"
|
179 |
-
f"Amount: ${bet['amount']}\n"
|
180 |
f"Game: {bet['game_data']['teams']['away']['teamName']} vs {bet['game_data']['teams']['home']['teamName']}\n"
|
181 |
f"Status: {status}\n"
|
182 |
f"Current Score: {score}\n"
|
@@ -193,7 +217,7 @@ async def show_current_bets(interaction: discord.Interaction):
|
|
193 |
bet_index = int(cancel_select.values[0])
|
194 |
cancelled_bet = user_bets[user_id].pop(bet_index)
|
195 |
user_cash[user_id] += cancelled_bet['amount']
|
196 |
-
await interaction.response.send_message(f"Bet cancelled. ${cancelled_bet['amount']} has been refunded to your balance.")
|
197 |
|
198 |
cancel_select.callback = cancel_callback
|
199 |
|
@@ -201,9 +225,4 @@ async def show_current_bets(interaction: discord.Interaction):
|
|
201 |
|
202 |
@app_commands.command(name="sportbet", description="Bet on sports games")
|
203 |
async def sportbet(interaction: discord.Interaction):
|
204 |
-
|
205 |
-
if user_id in user_bets and user_bets[user_id]:
|
206 |
-
view = SportBetView()
|
207 |
-
await interaction.response.send_message("?", view=view)
|
208 |
-
else:
|
209 |
-
await show_game_selection(interaction)
|
|
|
12 |
async with session.get("https://nhl-score-api.herokuapp.com/api/scores/latest") as response:
|
13 |
return await response.json()
|
14 |
|
15 |
+
class SportSelect(discord.ui.Select):
|
16 |
+
def __init__(self):
|
17 |
+
options = [
|
18 |
+
discord.SelectOption(label="NHL", value="nhl", description="National Hockey League")
|
19 |
+
]
|
20 |
+
super().__init__(placeholder="Select a sport", options=options)
|
21 |
+
|
22 |
class GameSelect(discord.ui.Select):
|
23 |
def __init__(self, games):
|
24 |
options = [
|
|
|
50 |
async def on_submit(self, interaction: discord.Interaction):
|
51 |
try:
|
52 |
bet_amount = int(self.bet_amount.value)
|
53 |
+
balance = user_cash.get(self.user_id, 0)
|
54 |
+
|
55 |
if bet_amount <= 0:
|
56 |
raise ValueError("Bet more than 0 dollars")
|
57 |
+
if bet_amount > balance:
|
58 |
+
raise ValueError(f"You don't have enough cash. Your current balance is ${balance:.2f}")
|
59 |
|
60 |
+
user_cash[self.user_id] = balance - bet_amount
|
61 |
await interaction.response.send_message(f"Bet placed on {self.team} for ${bet_amount}")
|
62 |
|
63 |
user = await interaction.client.fetch_user(self.user_id)
|
|
|
66 |
embed.add_field(name="Amount", value=f"${bet_amount}", inline=False)
|
67 |
embed.add_field(name="Game", value=f"{self.game_data['teams']['away']['teamName']} vs {self.game_data['teams']['home']['teamName']}", inline=False)
|
68 |
embed.add_field(name="Start Time", value=f"<t:{int(datetime.fromisoformat(self.game_data['startTime'].replace('Z', '+00:00')).timestamp())}:R>", inline=False)
|
69 |
+
embed.add_field(name="New Balance", value=f"${user_cash[self.user_id]:.2f}", inline=False)
|
70 |
await user.send(embed=embed)
|
71 |
|
72 |
if self.user_id not in user_bets:
|
|
|
85 |
game_start = datetime.fromisoformat(self.game_data['startTime'].replace('Z', '+00:00'))
|
86 |
await asyncio.sleep((game_start - datetime.now(timezone.utc)).total_seconds())
|
87 |
|
|
|
88 |
await interaction.user.send(f"GAME STARTED! Your team {self.team} is now playing!")
|
89 |
|
90 |
previous_scores = {'away': 0, 'home': 0}
|
|
|
108 |
if game['teams'][winner]['abbreviation'] == self.team:
|
109 |
winnings = bet_amount * 2
|
110 |
user_cash[self.user_id] += winnings
|
111 |
+
await interaction.user.send(f"Congratulations! Your team won. You won ${winnings:.2f}!")
|
112 |
else:
|
113 |
await interaction.user.send(f"Sorry, your team lost CRY")
|
114 |
|
115 |
+
await interaction.user.send(f"Your new balance is ${user_cash[self.user_id]:.2f}")
|
116 |
user_bets[self.user_id] = [bet for bet in user_bets[self.user_id] if bet['game_data'] != self.game_data]
|
117 |
break
|
118 |
|
|
|
124 |
|
125 |
@discord.ui.button(label="Bet Again", style=discord.ButtonStyle.primary)
|
126 |
async def bet_again(self, interaction: discord.Interaction, button: discord.ui.Button):
|
127 |
+
await show_sport_selection(interaction)
|
128 |
|
129 |
@discord.ui.button(label="View Bets", style=discord.ButtonStyle.secondary)
|
130 |
async def view_bets(self, interaction: discord.Interaction, button: discord.ui.Button):
|
131 |
await show_current_bets(interaction)
|
132 |
|
133 |
+
async def show_sport_selection(interaction: discord.Interaction):
|
134 |
+
view = discord.ui.View()
|
135 |
+
sport_select = SportSelect()
|
136 |
+
view.add_item(sport_select)
|
137 |
+
|
138 |
+
await interaction.response.send_message("Select a sport to bet on:", view=view)
|
139 |
+
|
140 |
+
async def sport_callback(interaction: discord.Interaction):
|
141 |
+
selected_sport = sport_select.values[0]
|
142 |
+
if selected_sport == "nhl":
|
143 |
+
await show_game_selection(interaction)
|
144 |
+
|
145 |
+
sport_select.callback = sport_callback
|
146 |
+
|
147 |
async def show_game_selection(interaction: discord.Interaction):
|
148 |
scores = await fetch_nhl_scores()
|
149 |
upcoming_games = [game for game in scores['games'] if game['status']['state'] == 'PREVIEW']
|
|
|
156 |
game_select = GameSelect(upcoming_games)
|
157 |
view.add_item(game_select)
|
158 |
|
159 |
+
await interaction.response.edit_message(content="Select a game to bet on:", view=view)
|
160 |
|
161 |
async def game_callback(interaction: discord.Interaction):
|
162 |
selected_game = next(game for game in upcoming_games if f"{game['teams']['away']['abbreviation']}_{game['teams']['home']['abbreviation']}" == game_select.values[0])
|
|
|
200 |
|
201 |
embed.add_field(name=f"Bet {i+1}", value=(
|
202 |
f"Team: {bet['team']}\n"
|
203 |
+
f"Amount: ${bet['amount']:.2f}\n"
|
204 |
f"Game: {bet['game_data']['teams']['away']['teamName']} vs {bet['game_data']['teams']['home']['teamName']}\n"
|
205 |
f"Status: {status}\n"
|
206 |
f"Current Score: {score}\n"
|
|
|
217 |
bet_index = int(cancel_select.values[0])
|
218 |
cancelled_bet = user_bets[user_id].pop(bet_index)
|
219 |
user_cash[user_id] += cancelled_bet['amount']
|
220 |
+
await interaction.response.send_message(f"Bet cancelled. ${cancelled_bet['amount']:.2f} has been refunded to your balance.")
|
221 |
|
222 |
cancel_select.callback = cancel_callback
|
223 |
|
|
|
225 |
|
226 |
@app_commands.command(name="sportbet", description="Bet on sports games")
|
227 |
async def sportbet(interaction: discord.Interaction):
|
228 |
+
await show_sport_selection(interaction)
|
|
|
|
|
|
|
|
|
|