Spaces:
Building
Building
Update sportbet.py
Browse files- sportbet.py +14 -36
sportbet.py
CHANGED
@@ -12,13 +12,6 @@ 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 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,14 +43,12 @@ class BetModal(discord.ui.Modal, title="Place Your Bet"):
|
|
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 >
|
58 |
-
raise ValueError(
|
59 |
|
60 |
-
user_cash[self.user_id]
|
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,7 +57,6 @@ class BetModal(discord.ui.Modal, title="Place Your Bet"):
|
|
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,8 +75,6 @@ class BetModal(discord.ui.Modal, title="Place Your Bet"):
|
|
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}
|
91 |
|
92 |
while True:
|
@@ -108,11 +96,10 @@ class BetModal(discord.ui.Modal, title="Place Your Bet"):
|
|
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
|
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,26 +111,12 @@ class SportBetView(discord.ui.View):
|
|
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
|
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,7 +129,7 @@ async def show_game_selection(interaction: discord.Interaction):
|
|
156 |
game_select = GameSelect(upcoming_games)
|
157 |
view.add_item(game_select)
|
158 |
|
159 |
-
await interaction.response.
|
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,7 +173,7 @@ async def show_current_bets(interaction: discord.Interaction):
|
|
200 |
|
201 |
embed.add_field(name=f"Bet {i+1}", value=(
|
202 |
f"Team: {bet['team']}\n"
|
203 |
-
f"Amount: ${bet['amount']
|
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,7 +190,7 @@ async def show_current_bets(interaction: discord.Interaction):
|
|
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']
|
221 |
|
222 |
cancel_select.callback = cancel_callback
|
223 |
|
@@ -225,4 +198,9 @@ async def show_current_bets(interaction: discord.Interaction):
|
|
225 |
|
226 |
@app_commands.command(name="sportbet", description="Bet on sports games")
|
227 |
async def sportbet(interaction: discord.Interaction):
|
228 |
-
|
|
|
|
|
|
|
|
|
|
|
|
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 |
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 > user_cash.get(self.user_id, 0):
|
49 |
+
raise ValueError("you are too poor check ur balance and try again")
|
50 |
|
51 |
+
user_cash[self.user_id] -= bet_amount
|
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 |
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 |
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 |
previous_scores = {'away': 0, 'home': 0}
|
79 |
|
80 |
while True:
|
|
|
96 |
if game['teams'][winner]['abbreviation'] == self.team:
|
97 |
winnings = bet_amount * 2
|
98 |
user_cash[self.user_id] += winnings
|
99 |
+
await interaction.user.send(f"Congratulations! Your team won. You won ${winnings}!")
|
100 |
else:
|
101 |
await interaction.user.send(f"Sorry, your team lost CRY")
|
102 |
|
|
|
103 |
user_bets[self.user_id] = [bet for bet in user_bets[self.user_id] if bet['game_data'] != self.game_data]
|
104 |
break
|
105 |
|
|
|
111 |
|
112 |
@discord.ui.button(label="Bet Again", style=discord.ButtonStyle.primary)
|
113 |
async def bet_again(self, interaction: discord.Interaction, button: discord.ui.Button):
|
114 |
+
await show_game_selection(interaction)
|
115 |
|
116 |
@discord.ui.button(label="View Bets", style=discord.ButtonStyle.secondary)
|
117 |
async def view_bets(self, interaction: discord.Interaction, button: discord.ui.Button):
|
118 |
await show_current_bets(interaction)
|
119 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
120 |
async def show_game_selection(interaction: discord.Interaction):
|
121 |
scores = await fetch_nhl_scores()
|
122 |
upcoming_games = [game for game in scores['games'] if game['status']['state'] == 'PREVIEW']
|
|
|
129 |
game_select = GameSelect(upcoming_games)
|
130 |
view.add_item(game_select)
|
131 |
|
132 |
+
await interaction.response.send_message("Select a game to bet on:", view=view)
|
133 |
|
134 |
async def game_callback(interaction: discord.Interaction):
|
135 |
selected_game = next(game for game in upcoming_games if f"{game['teams']['away']['abbreviation']}_{game['teams']['home']['abbreviation']}" == game_select.values[0])
|
|
|
173 |
|
174 |
embed.add_field(name=f"Bet {i+1}", value=(
|
175 |
f"Team: {bet['team']}\n"
|
176 |
+
f"Amount: ${bet['amount']}\n"
|
177 |
f"Game: {bet['game_data']['teams']['away']['teamName']} vs {bet['game_data']['teams']['home']['teamName']}\n"
|
178 |
f"Status: {status}\n"
|
179 |
f"Current Score: {score}\n"
|
|
|
190 |
bet_index = int(cancel_select.values[0])
|
191 |
cancelled_bet = user_bets[user_id].pop(bet_index)
|
192 |
user_cash[user_id] += cancelled_bet['amount']
|
193 |
+
await interaction.response.send_message(f"Bet cancelled. ${cancelled_bet['amount']} has been refunded to your balance.")
|
194 |
|
195 |
cancel_select.callback = cancel_callback
|
196 |
|
|
|
198 |
|
199 |
@app_commands.command(name="sportbet", description="Bet on sports games")
|
200 |
async def sportbet(interaction: discord.Interaction):
|
201 |
+
user_id = interaction.user.id
|
202 |
+
if user_id in user_bets and user_bets[user_id]:
|
203 |
+
view = SportBetView()
|
204 |
+
await interaction.response.send_message("?", view=view)
|
205 |
+
else:
|
206 |
+
await show_game_selection(interaction)
|