Spaces:
Building
Building
from discord import app_commands | |
import discord | |
from main import user_cash, user_bets, fetch_nhl_scores | |
async def currentbets(interaction: discord.Interaction): | |
user_id = interaction.user.id | |
if user_id not in user_bets or not user_bets[user_id]: | |
await interaction.response.send_message("You got any bets.", ephemeral=True) | |
return | |
embed = discord.Embed(title="Your Current Bets", color=0x787878) | |
for i, bet in enumerate(user_bets[user_id], 1): | |
scores = await fetch_nhl_scores() | |
game = next((g for g in scores['games'] if g['teams']['away']['abbreviation'] == bet['game_data']['teams']['away']['abbreviation'] and | |
g['teams']['home']['abbreviation'] == bet['game_data']['teams']['home']['abbreviation']), None) | |
score_info = f"Current Score: {game['scores']['away']} - {game['scores']['home']}" if game and 'scores' in game else "Score not available" | |
embed.add_field( | |
name=f"Bet {i}", | |
value=f"Team: {bet['team']}\nAmount: ${bet['amount']}\nGame: {bet['game_data']['teams']['away']['teamName']} vs {bet['game_data']['teams']['home']['teamName']}\n{score_info}", | |
inline=False | |
) | |
view = discord.ui.View() | |
select = discord.ui.Select( | |
placeholder="Select a bet to cancel", | |
options=[discord.SelectOption(label=f"Bet {i}", value=str(i-1)) for i in range(1, len(user_bets[user_id])+1)] | |
) | |
async def select_callback(interaction: discord.Interaction): | |
bet_index = int(select.values[0]) | |
removed_bet = user_bets[user_id].pop(bet_index) | |
user_cash[user_id] += removed_bet['amount'] # Refund the bet amount | |
await interaction.response.send_message(f"Bet on {removed_bet['team']} for ${removed_bet['amount']} has been cancelled and refunded.", ephemeral=True) | |
if not user_bets[user_id]: | |
del user_bets[user_id] | |
# Update the embed and view | |
new_embed = discord.Embed(title="Your Current Bets", color=0x787878) | |
new_view = discord.ui.View() | |
if user_id in user_bets and user_bets[user_id]: | |
for i, bet in enumerate(user_bets[user_id], 1): | |
scores = await fetch_nhl_scores() | |
game = next((g for g in scores['games'] if g['teams']['away']['abbreviation'] == bet['game_data']['teams']['away']['abbreviation'] and | |
g['teams']['home']['abbreviation'] == bet['game_data']['teams']['home']['abbreviation']), None) | |
score_info = f"Current Score: {game['scores']['away']} - {game['scores']['home']}" if game and 'scores' in game else "Score not available" | |
new_embed.add_field( | |
name=f"Bet {i}", | |
value=f"Team: {bet['team']}\nAmount: ${bet['amount']}\nGame: {bet['game_data']['teams']['away']['teamName']} vs {bet['game_data']['teams']['home']['teamName']}\n{score_info}", | |
inline=False | |
) | |
new_select = discord.ui.Select( | |
placeholder="Select a bet to cancel", | |
options=[discord.SelectOption(label=f"Bet {i}", value=str(i-1)) for i in range(1, len(user_bets[user_id])+1)] | |
) | |
new_select.callback = select_callback | |
new_view.add_item(new_select) | |
else: | |
new_embed.description = "none active bets." | |
await interaction.message.edit(embed=new_embed, view=new_view) | |
select.callback = select_callback | |
view.add_item(select) | |
await interaction.response.send_message(embed=embed, view=view, ephemeral=True) |