File size: 3,750 Bytes
14b4ddb
 
cf00815
14b4ddb
cf00815
0304b78
 
 
cf00815
0304b78
 
cf00815
0304b78
cf00815
 
 
 
 
 
0304b78
 
cf00815
0304b78
 
 
 
 
cf00815
0304b78
 
 
 
 
 
cf00815
0304b78
cf00815
0304b78
 
 
 
 
 
 
 
 
cf00815
 
 
 
 
 
0304b78
 
cf00815
0304b78
 
 
cf00815
0304b78
 
 
 
 
cf00815
0304b78
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from discord import app_commands
import discord
from main import user_cash, user_bets, fetch_nhl_scores

@app_commands.command(name="currentbets", description="vie wbets")
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)