Spaces:
Paused
Paused
Update sportbet.py
Browse files- sportbet.py +135 -129
sportbet.py
CHANGED
|
@@ -150,150 +150,156 @@ class BetModal(discord.ui.Modal, title="Place Your Bet"):
|
|
| 150 |
|
| 151 |
if self.user_id not in user_bets:
|
| 152 |
user_bets[self.user_id] = []
|
| 153 |
-
|
| 154 |
-
# Initialize previous scores to None to track changes only when they occur.
|
| 155 |
user_bets[self.user_id].append({
|
| 156 |
"league": self.league,
|
| 157 |
"team": self.team,
|
| 158 |
"amount": bet_amount,
|
| 159 |
-
"game_data": self.game_data
|
| 160 |
-
"previous_scores": {
|
| 161 |
-
"away": None,
|
| 162 |
-
"home": None,
|
| 163 |
-
}
|
| 164 |
})
|
| 165 |
|
| 166 |
-
asyncio.create_task(self.monitor_game(interaction))
|
| 167 |
-
|
| 168 |
except ValueError as e:
|
| 169 |
await interaction.response.send_message(str(e), ephemeral=False)
|
| 170 |
|
| 171 |
-
async def monitor_game(self, interaction):
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
break
|
| 188 |
-
|
| 189 |
-
if not current_game:
|
| 190 |
-
await asyncio.sleep(60)
|
| 191 |
-
continue
|
| 192 |
-
|
| 193 |
-
current_scores={
|
| 194 |
-
"away": current_game['lowerTeam'].get('score'),
|
| 195 |
-
"home": current_game['upperTeam'].get('score'),
|
| 196 |
-
}
|
| 197 |
-
|
| 198 |
-
score_changed=False
|
| 199 |
-
|
| 200 |
-
for key in ["away","home"]:
|
| 201 |
-
current_score=current_scores[key]
|
| 202 |
-
previous_score=previous_scores[key]
|
| 203 |
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
|
| 208 |
-
|
|
|
|
|
|
|
|
|
|
| 209 |
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
message=f"Score update: {away_score} - {home_score}"
|
| 214 |
-
user=await interaction.client.fetch_user(self.user_id)
|
| 215 |
-
await user.send(message)
|
| 216 |
|
| 217 |
-
|
|
|
|
|
|
|
| 218 |
|
| 219 |
-
|
| 220 |
-
|
|
|
|
|
|
|
|
|
|
| 221 |
|
| 222 |
-
|
| 223 |
|
| 224 |
-
|
| 225 |
-
def __init__(self):
|
| 226 |
-
super().__init__()
|
| 227 |
-
self.add_item(SportSelect())
|
| 228 |
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
async def show_current_bets(interaction:discord.Interaction ):
|
| 234 |
-
user_id=interaction.user.id
|
| 235 |
-
|
| 236 |
-
if user_id not in user_bets or not user_bets[user_id]:
|
| 237 |
-
await interaction.response.send_message("You have no active bets." ,ephemeral=False )
|
| 238 |
-
return
|
| 239 |
-
|
| 240 |
-
embed=discord.Embed(title ="Your Current Bets" ,color=0x787878 )
|
| 241 |
-
|
| 242 |
-
for i ,bet in enumerate(user_bets[user_id] ,1 ):
|
| 243 |
-
league=bet ['league']
|
| 244 |
-
team=bet ['team']
|
| 245 |
-
amount=bet ['amount']
|
| 246 |
-
game=bet ['game_data']
|
| 247 |
-
away_score='N/A' if 'score' not in game ['lowerTeam'] else str(game ['lowerTeam']['score'])
|
| 248 |
-
home_score='N/A' if 'score' not in game ['upperTeam'] else str(game ['upperTeam']['score'])
|
| 249 |
-
status='Final' if 'score' in game ['upperTeam'] else f"Starts <t:{int(datetime.fromisoformat(game ['eventTime'].replace('Z','+00:00')).timestamp())}:R>"
|
| 250 |
-
|
| 251 |
-
embed.add_field(
|
| 252 |
-
name=f"Bet {i}: {league}" ,
|
| 253 |
-
value=(f"**Team:** {team}\n"
|
| 254 |
-
f"**Amount:** ${amount}\n"
|
| 255 |
-
f"**Game:** {game ['lowerTeam']['longName']} vs {game ['upperTeam']['longName']}\n"
|
| 256 |
-
f"**Status:** {status}\n"
|
| 257 |
-
f"**Current Score:** {away_score} - {home_score}\n"
|
| 258 |
-
f"**Start Time:** <t:{int(datetime.fromisoformat(game ['eventTime'].replace('Z','+00:00')).timestamp())}:F>"),
|
| 259 |
-
inline=False )
|
| 260 |
-
|
| 261 |
-
view=discord.ui.View()
|
| 262 |
-
cancel_select=discord.ui.Select(
|
| 263 |
-
placeholder ="Select a bet to cancel",
|
| 264 |
-
min_values=1 ,
|
| 265 |
-
max_values=1 ,
|
| 266 |
-
options=[
|
| 267 |
-
discord.SelectOption(label=f"Bet {i}" ,value=str(i-1)) for i in range(1 ,len(user_bets[user_id])+1 )
|
| 268 |
-
]
|
| 269 |
-
)
|
| 270 |
-
view.add_item(cancel_select)
|
| 271 |
-
|
| 272 |
-
async def cancel_callback(interaction_cancel :discord.Interaction ):
|
| 273 |
-
if interaction_cancel.user.id !=user_id :
|
| 274 |
-
await interaction_cancel.response.send_message("You cannot cancel other users' bets." ,ephemeral=False )
|
| 275 |
-
return
|
| 276 |
-
|
| 277 |
-
bet_index=int(cancel_select.values[0])
|
| 278 |
-
cancelled_bet=user_bets[user_id][bet_index]
|
| 279 |
-
start_time=datetime.fromisoformat(cancelled_bet ['game_data']['eventTime'].replace('Z','+00:00'))
|
| 280 |
-
|
| 281 |
-
if datetime.now(timezone.utc)>=start_time :
|
| 282 |
-
await interaction_cancel.response.send_message("You cannot cancel your bet as the game has already started." ,ephemeral=False )
|
| 283 |
-
return
|
| 284 |
-
|
| 285 |
-
user_cash[user_id]+=cancelled_bet ['amount']
|
| 286 |
-
user_bets[user_id].pop(bet_index)
|
| 287 |
-
await interaction_cancel.response.send_message(f"Bet cancelled. **${cancelled_bet ['amount']}** has been refunded." ,ephemeral=False )
|
| 288 |
-
|
| 289 |
-
if not user_bets[user_id]:
|
| 290 |
-
del user_bets[user_id]
|
| 291 |
-
|
| 292 |
-
cancel_select.callback=cancel_callback
|
| 293 |
-
|
| 294 |
-
await interaction.response.send_message(embed=embed ,view=view ,ephemeral=False )
|
| 295 |
-
|
| 296 |
-
@app_commands.command(name ="sportbet" ,description ="Bet on sports games")
|
| 297 |
-
async def sportbet(interaction :discord.Interaction ):
|
| 298 |
-
view=SportBetView()
|
| 299 |
-
await interaction.response.send_message("Select a sport to bet on:" ,view=view ,ephemeral=False )
|
|
|
|
| 150 |
|
| 151 |
if self.user_id not in user_bets:
|
| 152 |
user_bets[self.user_id] = []
|
|
|
|
|
|
|
| 153 |
user_bets[self.user_id].append({
|
| 154 |
"league": self.league,
|
| 155 |
"team": self.team,
|
| 156 |
"amount": bet_amount,
|
| 157 |
+
"game_data": self.game_data
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
})
|
| 159 |
|
| 160 |
+
asyncio.create_task(self.monitor_game(interaction, bet_amount))
|
|
|
|
| 161 |
except ValueError as e:
|
| 162 |
await interaction.response.send_message(str(e), ephemeral=False)
|
| 163 |
|
| 164 |
+
async def monitor_game(self, interaction, bet_amount):
|
| 165 |
+
game_start = datetime.fromisoformat(self.game_data['eventTime'].replace('Z', '+00:00'))
|
| 166 |
+
event_id = self.game_data['id']
|
| 167 |
+
fetch_scores = fetch_nhl_scores if self.league == "NHL" else fetch_nfl_scores
|
| 168 |
+
|
| 169 |
+
sleep_duration = (game_start - datetime.now(timezone.utc)).total_seconds()
|
| 170 |
+
if sleep_duration > 0:
|
| 171 |
+
await asyncio.sleep(sleep_duration)
|
| 172 |
+
|
| 173 |
+
user = await interaction.client.fetch_user(self.user_id)
|
| 174 |
+
await user.send(f"Your team **{self.team}** has started playing!")
|
| 175 |
+
|
| 176 |
+
previous_scores = {
|
| 177 |
+
self.game_data['lowerTeam']['name']: self.game_data.get('lowerTeam', {}).get('score', 0),
|
| 178 |
+
self.game_data['upperTeam']['name']: self.game_data.get('upperTeam', {}).get('score', 0)
|
| 179 |
+
}
|
| 180 |
+
|
| 181 |
+
while True:
|
| 182 |
+
scores = await fetch_scores()
|
| 183 |
+
game = None
|
| 184 |
+
for section in scores.get('sectionList', []):
|
| 185 |
+
for evt in section.get('events', []):
|
| 186 |
+
if evt['id'] == event_id:
|
| 187 |
+
game = evt
|
| 188 |
+
break
|
| 189 |
+
if game:
|
| 190 |
+
break
|
| 191 |
+
if not game:
|
| 192 |
+
await asyncio.sleep(60)
|
| 193 |
+
continue
|
| 194 |
+
|
| 195 |
+
event_status = game.get('eventStatus', 2)
|
| 196 |
+
current_scores = {
|
| 197 |
+
game['lowerTeam']['name']: game['lowerTeam'].get('score', 0),
|
| 198 |
+
game['upperTeam']['name']: game['upperTeam'].get('score', 0)
|
| 199 |
+
}
|
| 200 |
+
is_final = event_status == 3
|
| 201 |
+
|
| 202 |
+
# Check for score updates
|
| 203 |
+
for team, score in current_scores.items():
|
| 204 |
+
if score > previous_scores.get(team, 0):
|
| 205 |
+
team_name = self.game_data['lowerTeam']['longName'] if team == self.game_data['lowerTeam']['name'] else self.game_data['upperTeam']['longName']
|
| 206 |
+
message = f"**{team_name}** SCORED! Current score: {current_scores[self.game_data['lowerTeam']['name']]} - {current_scores[self.game_data['upperTeam']['name']]}"
|
| 207 |
+
await user.send(message)
|
| 208 |
+
|
| 209 |
+
previous_scores = current_scores.copy()
|
| 210 |
+
|
| 211 |
+
if is_final:
|
| 212 |
+
away_score = current_scores.get(self.game_data['lowerTeam']['name'], 0)
|
| 213 |
+
home_score = current_scores.get(self.game_data['upperTeam']['name'], 0)
|
| 214 |
+
winner = self.game_data['lowerTeam']['name'] if away_score > home_score else self.game_data['upperTeam']['name']
|
| 215 |
+
|
| 216 |
+
if winner == self.team:
|
| 217 |
+
winnings = bet_amount * 2
|
| 218 |
+
user_cash[self.user_id] += winnings
|
| 219 |
+
await user.send(f"🎉 **Congratulations!** Your team **{self.team}** won! You won **${winnings}**.")
|
| 220 |
+
else:
|
| 221 |
+
await user.send(f"😞 **Sorry!** Your team **{self.team}** lost. Better luck next time!")
|
| 222 |
+
|
| 223 |
+
# Remove the completed bet
|
| 224 |
+
user_bets[self.user_id] = [bet for bet in user_bets[self.user_id] if not (bet['league'] == self.league and bet['team'] == self.team and bet['game_data']['id'] == event_id)]
|
| 225 |
+
break
|
| 226 |
+
|
| 227 |
+
await asyncio.sleep(60) # Check every minute
|
| 228 |
|
| 229 |
+
class SportBetView(discord.ui.View):
|
| 230 |
+
def __init__(self):
|
| 231 |
+
super().__init__()
|
| 232 |
+
self.add_item(SportSelect())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 233 |
|
| 234 |
+
@discord.ui.button(label="View Bets", style=discord.ButtonStyle.secondary)
|
| 235 |
+
async def view_bets(self, interaction: discord.Interaction, button: discord.ui.Button):
|
| 236 |
+
await show_current_bets(interaction)
|
| 237 |
+
|
| 238 |
+
async def show_current_bets(interaction: discord.Interaction):
|
| 239 |
+
user_id = interaction.user.id
|
| 240 |
+
if user_id not in user_bets or not user_bets[user_id]:
|
| 241 |
+
await interaction.response.send_message("You have no active bets.", ephemeral=False)
|
| 242 |
+
return
|
| 243 |
+
|
| 244 |
+
embed = discord.Embed(title="Your Current Bets", color=0x787878)
|
| 245 |
+
for i, bet in enumerate(user_bets[user_id], 1):
|
| 246 |
+
league = bet['league']
|
| 247 |
+
team = bet['team']
|
| 248 |
+
amount = bet['amount']
|
| 249 |
+
game = bet['game_data']
|
| 250 |
+
game_description = f"{game['lowerTeam']['longName']} vs {game['upperTeam']['longName']}"
|
| 251 |
+
start_time = game['eventTime']
|
| 252 |
+
score = f"{game['lowerTeam'].get('score', 'N/A')} - {game['upperTeam'].get('score', 'N/A')}"
|
| 253 |
+
status = "Final" if game.get('upperTeam', {}).get('score') is not None else f"Starts <t:{int(datetime.fromisoformat(start_time.replace('Z', '+00:00')).timestamp())}:R>"
|
| 254 |
+
|
| 255 |
+
embed.add_field(
|
| 256 |
+
name=f"Bet {i}: {league}",
|
| 257 |
+
value=(
|
| 258 |
+
f"**Team:** {team}\n"
|
| 259 |
+
f"**Amount:** ${amount}\n"
|
| 260 |
+
f"**Game:** {game_description}\n"
|
| 261 |
+
f"**Status:** {status}\n"
|
| 262 |
+
f"**Current Score:** {score}\n"
|
| 263 |
+
f"**Start Time:** <t:{int(datetime.fromisoformat(start_time.replace('Z', '+00:00')).timestamp())}:F>"
|
| 264 |
+
),
|
| 265 |
+
inline=False
|
| 266 |
+
)
|
| 267 |
+
|
| 268 |
+
view = discord.ui.View()
|
| 269 |
+
cancel_select = discord.ui.Select(
|
| 270 |
+
placeholder="Select a bet to cancel",
|
| 271 |
+
min_values=1,
|
| 272 |
+
max_values=1,
|
| 273 |
+
options=[
|
| 274 |
+
discord.SelectOption(label=f"Bet {i}", value=str(i-1)) for i in range(1, len(user_bets[user_id]) + 1)
|
| 275 |
+
]
|
| 276 |
+
)
|
| 277 |
+
view.add_item(cancel_select)
|
| 278 |
|
| 279 |
+
async def cancel_callback(interaction_cancel: discord.Interaction):
|
| 280 |
+
if interaction_cancel.user.id != user_id:
|
| 281 |
+
await interaction_cancel.response.send_message("You cannot cancel other users' bets.", ephemeral=False)
|
| 282 |
+
return
|
| 283 |
|
| 284 |
+
bet_index = int(cancel_select.values[0])
|
| 285 |
+
cancelled_bet = user_bets[user_id][bet_index]
|
| 286 |
+
start_time = datetime.fromisoformat(cancelled_bet['game_data']['eventTime'].replace('Z', '+00:00'))
|
|
|
|
|
|
|
|
|
|
| 287 |
|
| 288 |
+
if datetime.now(timezone.utc) >= start_time:
|
| 289 |
+
await interaction_cancel.response.send_message("You cannot cancel your bet as the game has already started.", ephemeral=False)
|
| 290 |
+
return
|
| 291 |
|
| 292 |
+
user_cash[user_id] += cancelled_bet['amount']
|
| 293 |
+
user_bets[user_id].pop(bet_index)
|
| 294 |
+
await interaction_cancel.response.send_message(f"Bet cancelled. **${cancelled_bet['amount']}** has been refunded.", ephemeral=False)
|
| 295 |
+
if not user_bets[user_id]:
|
| 296 |
+
del user_bets[user_id]
|
| 297 |
|
| 298 |
+
cancel_select.callback = cancel_callback
|
| 299 |
|
| 300 |
+
await interaction.response.send_message(embed=embed, view=view, ephemeral=False)
|
|
|
|
|
|
|
|
|
|
| 301 |
|
| 302 |
+
@app_commands.command(name="sportbet", description="Bet on sports games")
|
| 303 |
+
async def sportbet(interaction: discord.Interaction):
|
| 304 |
+
view = SportBetView()
|
| 305 |
+
await interaction.response.send_message("Select a sport to bet on:", view=view, ephemeral=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|