Spaces:
Building
Building
Update sportbet.py
Browse files- sportbet.py +33 -10
sportbet.py
CHANGED
@@ -2,7 +2,7 @@ import discord
|
|
2 |
from discord import app_commands
|
3 |
import aiohttp
|
4 |
import asyncio
|
5 |
-
from datetime import datetime, timezone
|
6 |
from cash import user_cash
|
7 |
|
8 |
user_cash = {}
|
@@ -113,8 +113,7 @@ class BetModal(discord.ui.Modal, title="Place Your Bet"):
|
|
113 |
|
114 |
await asyncio.sleep(300)
|
115 |
|
116 |
-
|
117 |
-
async def sportbet(interaction: discord.Interaction):
|
118 |
scores = await fetch_nhl_scores()
|
119 |
upcoming_games = [game for game in scores['games'] if game['status']['state'] == 'PREVIEW']
|
120 |
|
@@ -126,7 +125,7 @@ async def sportbet(interaction: discord.Interaction):
|
|
126 |
game_select = GameSelect(upcoming_games)
|
127 |
view.add_item(game_select)
|
128 |
|
129 |
-
await interaction.response.send_message("game to bet on:", view=view)
|
130 |
|
131 |
async def game_callback(interaction: discord.Interaction):
|
132 |
selected_game = next(game for game in upcoming_games if f"{game['teams']['away']['abbreviation']}_{game['teams']['home']['abbreviation']}" == game_select.values[0])
|
@@ -135,7 +134,7 @@ async def sportbet(interaction: discord.Interaction):
|
|
135 |
team_select = TeamSelect(selected_game['teams']['away'], selected_game['teams']['home'])
|
136 |
team_view.add_item(team_select)
|
137 |
|
138 |
-
await interaction.response.edit_message(content="team to bet on:", view=team_view)
|
139 |
|
140 |
async def team_callback(interaction: discord.Interaction):
|
141 |
selected_team = team_select.values[0]
|
@@ -145,11 +144,10 @@ async def sportbet(interaction: discord.Interaction):
|
|
145 |
|
146 |
game_select.callback = game_callback
|
147 |
|
148 |
-
|
149 |
-
async def currentbets(interaction: discord.Interaction):
|
150 |
user_id = interaction.user.id
|
151 |
if user_id not in user_bets or not user_bets[user_id]:
|
152 |
-
await interaction.response.send_message("You have no bets.")
|
153 |
return
|
154 |
|
155 |
embed = discord.Embed(title="Your Current Bets", color=0x787878)
|
@@ -158,7 +156,7 @@ async def currentbets(interaction: discord.Interaction):
|
|
158 |
game = next((g for g in scores['games'] if g['teams']['away']['abbreviation'] == bet['game_data']['teams']['away']['abbreviation'] and
|
159 |
g['teams']['home']['abbreviation'] == bet['game_data']['teams']['home']['abbreviation']), None)
|
160 |
|
161 |
-
embed.add_field(name=f"Bet {i+1}", value=f"Team: {bet['team']}\nAmount: ${bet['amount']}\nGame: {bet['game_data']['teams']['away']['teamName']} vs {bet['game_data']['teams']['home']['teamName']}\nCurrent Score: {game['scores']['away']} - {game['scores']['home']}", inline=False)
|
162 |
|
163 |
view = discord.ui.View()
|
164 |
cancel_select = discord.ui.Select(placeholder="Select a bet to cancel", options=[
|
@@ -174,4 +172,29 @@ async def currentbets(interaction: discord.Interaction):
|
|
174 |
|
175 |
cancel_select.callback = cancel_callback
|
176 |
|
177 |
-
await interaction.response.send_message(embed=embed, view=view)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
from discord import app_commands
|
3 |
import aiohttp
|
4 |
import asyncio
|
5 |
+
from datetime import datetime, timezone
|
6 |
from cash import user_cash
|
7 |
|
8 |
user_cash = {}
|
|
|
113 |
|
114 |
await asyncio.sleep(300)
|
115 |
|
116 |
+
async def show_game_selection(interaction: discord.Interaction):
|
|
|
117 |
scores = await fetch_nhl_scores()
|
118 |
upcoming_games = [game for game in scores['games'] if game['status']['state'] == 'PREVIEW']
|
119 |
|
|
|
125 |
game_select = GameSelect(upcoming_games)
|
126 |
view.add_item(game_select)
|
127 |
|
128 |
+
await interaction.response.send_message("Select a game to bet on:", view=view)
|
129 |
|
130 |
async def game_callback(interaction: discord.Interaction):
|
131 |
selected_game = next(game for game in upcoming_games if f"{game['teams']['away']['abbreviation']}_{game['teams']['home']['abbreviation']}" == game_select.values[0])
|
|
|
134 |
team_select = TeamSelect(selected_game['teams']['away'], selected_game['teams']['home'])
|
135 |
team_view.add_item(team_select)
|
136 |
|
137 |
+
await interaction.response.edit_message(content="Select a team to bet on:", view=team_view)
|
138 |
|
139 |
async def team_callback(interaction: discord.Interaction):
|
140 |
selected_team = team_select.values[0]
|
|
|
144 |
|
145 |
game_select.callback = game_callback
|
146 |
|
147 |
+
async def show_current_bets(interaction: discord.Interaction):
|
|
|
148 |
user_id = interaction.user.id
|
149 |
if user_id not in user_bets or not user_bets[user_id]:
|
150 |
+
await interaction.response.send_message("You have no active bets.")
|
151 |
return
|
152 |
|
153 |
embed = discord.Embed(title="Your Current Bets", color=0x787878)
|
|
|
156 |
game = next((g for g in scores['games'] if g['teams']['away']['abbreviation'] == bet['game_data']['teams']['away']['abbreviation'] and
|
157 |
g['teams']['home']['abbreviation'] == bet['game_data']['teams']['home']['abbreviation']), None)
|
158 |
|
159 |
+
embed.add_field(name=f"Bet {i+1}", value=f"Team: {bet['team']}\nAmount: ${bet['amount']}\nGame: {bet['game_data']['teams']['away']['teamName']} vs {bet['game_data']['teams']['home']['teamName']}\nStart Time: {format_discord_timestamp(datetime.fromisoformat(bet['game_data']['startTime'].replace('Z', '+00:00')))}\nCurrent Score: {game['scores']['away']} - {game['scores']['home']}", inline=False)
|
160 |
|
161 |
view = discord.ui.View()
|
162 |
cancel_select = discord.ui.Select(placeholder="Select a bet to cancel", options=[
|
|
|
172 |
|
173 |
cancel_select.callback = cancel_callback
|
174 |
|
175 |
+
await interaction.response.send_message(embed=embed, view=view)
|
176 |
+
|
177 |
+
@app_commands.command(name="sportbet", description="bet on sports game or view current bets")
|
178 |
+
async def sportbet(interaction: discord.Interaction):
|
179 |
+
user_id = interaction.user.id
|
180 |
+
has_bets = user_id in user_bets and len(user_bets[user_id]) > 0
|
181 |
+
|
182 |
+
view = discord.ui.View()
|
183 |
+
bet_button = discord.ui.Button(style=discord.ButtonStyle.primary, label="Bet Again" if has_bets else "Place Bet")
|
184 |
+
view.add_item(bet_button)
|
185 |
+
|
186 |
+
if has_bets:
|
187 |
+
view_bets_button = discord.ui.Button(style=discord.ButtonStyle.secondary, label="View Bets")
|
188 |
+
view.add_item(view_bets_button)
|
189 |
+
|
190 |
+
async def view_bets_callback(interaction: discord.Interaction):
|
191 |
+
await show_current_bets(interaction)
|
192 |
+
|
193 |
+
view_bets_button.callback = view_bets_callback
|
194 |
+
|
195 |
+
async def bet_callback(interaction: discord.Interaction):
|
196 |
+
await show_game_selection(interaction)
|
197 |
+
|
198 |
+
bet_button.callback = bet_callback
|
199 |
+
|
200 |
+
await interaction.response.send_message("What would you like to do?", view=view)
|