File size: 10,710 Bytes
aad95f7
 
 
 
4d9c217
 
aad95f7
7917f65
 
47a6106
 
4d9c217
47a6106
 
4d9c217
 
29254f8
 
4d9c217
 
 
29254f8
 
4d9c217
94166cb
4d9c217
 
29254f8
4d9c217
 
29254f8
4d9c217
7917f65
23567d4
4d9c217
7917f65
4d9c217
23567d4
7917f65
 
 
 
 
 
 
 
4d9c217
7917f65
4d9c217
7917f65
 
4d9c217
 
23567d4
 
 
 
4d9c217
 
23567d4
 
 
 
 
7917f65
 
5a642ea
7917f65
4d9c217
47a6106
7917f65
4d9c217
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21b7479
4d9c217
 
5a642ea
47a6106
 
 
4d9c217
 
 
 
5a642ea
47a6106
 
 
5a642ea
4d9c217
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47a6106
5a642ea
 
4d9c217
5a642ea
 
 
4d9c217
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5a642ea
 
4d9c217
 
 
ec17a11
 
4d9c217
 
 
ec17a11
5a642ea
ec17a11
4d9c217
ec17a11
4d9c217
5a642ea
 
 
4d9c217
47a6106
ec17a11
 
4d9c217
 
 
 
 
 
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import discord
from discord import app_commands
import aiohttp
import asyncio
from datetime import datetime, timezone
from cash import user_cash

user_bets = {}

async def fetch_nhl_scores():
    async with aiohttp.ClientSession() as session:
        async with session.get("https://nhl-score-api.herokuapp.com/api/scores/latest") as response:
            return await response.json()

class GameSelect(discord.ui.Select):
    def __init__(self, games):
        options = [
            discord.SelectOption(
                label=f"{game['teams']['away']['teamName']} vs {game['teams']['home']['teamName']}",
                value=f"{game['teams']['away']['abbreviation']}_{game['teams']['home']['abbreviation']}",
                description=f"Start time: <t:{int(datetime.fromisoformat(game['startTime'].replace('Z', '+00:00')).timestamp())}:R>"
            ) for game in games
        ]
        super().__init__(placeholder="Select a game", options=options)

class TeamSelect(discord.ui.Select):
    def __init__(self, away_team, home_team):
        options = [
            discord.SelectOption(label=away_team['teamName'], value=away_team['abbreviation']),
            discord.SelectOption(label=home_team['teamName'], value=home_team['abbreviation'])
        ]
        super().__init__(placeholder="Select a team to bet on", options=options)

class BetModal(discord.ui.Modal, title="Place Your Bet"):
    bet_amount = discord.ui.TextInput(label="Bet Amount", placeholder="Enter bet amount")

    def __init__(self, team, user_id, game_data):
        super().__init__()
        self.team = team
        self.user_id = user_id
        self.game_data = game_data

    async def on_submit(self, interaction: discord.Interaction):
        try:
            bet_amount = int(self.bet_amount.value)
            if bet_amount <= 0:
                raise ValueError("Bet more than 0 dollars")
            if bet_amount > user_cash.get(self.user_id, 0):
                raise ValueError("You are too poor. Check your balance and try again")

            user_cash[self.user_id] -= bet_amount
            await interaction.response.send_message(f"Bet placed on {self.team} for ${bet_amount}")
            
            user = await interaction.client.fetch_user(self.user_id)
            embed = discord.Embed(title="Bet Placed", color=0x787878)
            embed.add_field(name="Team", value=self.team, inline=False)
            embed.add_field(name="Amount", value=f"${bet_amount}", inline=False)
            embed.add_field(name="Game", value=f"{self.game_data['teams']['away']['teamName']} vs {self.game_data['teams']['home']['teamName']}", inline=False)
            embed.add_field(name="Start Time", value=f"<t:{int(datetime.fromisoformat(self.game_data['startTime'].replace('Z', '+00:00')).timestamp())}:R>", inline=False)
            await user.send(embed=embed)

            if self.user_id not in user_bets:
                user_bets[self.user_id] = []
            user_bets[self.user_id].append({
                "team": self.team,
                "amount": bet_amount,
                "game_data": self.game_data
            })
            
            asyncio.create_task(self.monitor_game(interaction, bet_amount))
        except ValueError as e:
            await interaction.response.send_message(str(e), ephemeral=True)

    async def monitor_game(self, interaction, bet_amount):
        game_start = datetime.fromisoformat(self.game_data['startTime'].replace('Z', '+00:00'))
        await asyncio.sleep((game_start - datetime.now(timezone.utc)).total_seconds())

        user = await interaction.client.fetch_user(self.user_id)
        await user.send(f"Your team {self.team} has started playing!")

        previous_scores = {self.game_data['teams']['away']['abbreviation']: 0, self.game_data['teams']['home']['abbreviation']: 0}

        while True:
            scores = await fetch_nhl_scores()
            game = next((g for g in scores['games'] if g['teams']['away']['abbreviation'] == self.game_data['teams']['away']['abbreviation'] and 
                         g['teams']['home']['abbreviation'] == self.game_data['teams']['home']['abbreviation']), None)
            
            if game is None or game['status']['state'] == 'PREVIEW':
                await asyncio.sleep(60)
                continue

            current_scores = game['scores']
            
            for team in [self.game_data['teams']['away']['abbreviation'], self.game_data['teams']['home']['abbreviation']]:
                if current_scores[team] > previous_scores[team]:
                    team_name = game['teams']['away']['teamName'] if team == game['teams']['away']['abbreviation'] else game['teams']['home']['teamName']
                    await user.send(f"{team_name} SCORED! Current score: {current_scores[self.game_data['teams']['away']['abbreviation']]} - {current_scores[self.game_data['teams']['home']['abbreviation']]}")
            
            previous_scores = current_scores.copy()
            
            if game['status']['state'] == 'FINAL':
                winner = self.game_data['teams']['away']['abbreviation'] if current_scores[self.game_data['teams']['away']['abbreviation']] > current_scores[self.game_data['teams']['home']['abbreviation']] else self.game_data['teams']['home']['abbreviation']
                if winner == self.team:
                    winnings = bet_amount * 2
                    user_cash[self.user_id] += winnings
                    await user.send(f"Congratulations! Your team won. You won ${winnings}!")
                else:
                    await user.send(f"Sorry, your team lost. Better luck next time!")
                
                user_bets[self.user_id] = [bet for bet in user_bets[self.user_id] if bet['game_data'] != self.game_data]
                break
            
            await asyncio.sleep(60)  # Check for updates every minute

class SportBetView(discord.ui.View):
    def __init__(self):
        super().__init__()

    @discord.ui.button(label="Bet Again", style=discord.ButtonStyle.primary)
    async def bet_again(self, interaction: discord.Interaction, button: discord.ui.Button):
        await show_game_selection(interaction)

    @discord.ui.button(label="View Bets", style=discord.ButtonStyle.secondary)
    async def view_bets(self, interaction: discord.Interaction, button: discord.ui.Button):
        await show_current_bets(interaction)

async def show_game_selection(interaction: discord.Interaction):
    scores = await fetch_nhl_scores()
    upcoming_games = [game for game in scores['games'] if game['status']['state'] == 'PREVIEW']

    if not upcoming_games:
        await interaction.response.send_message("No games available for betting.")
        return

    view = discord.ui.View()
    game_select = GameSelect(upcoming_games)
    view.add_item(game_select)

    await interaction.response.send_message("Select a game to bet on:", view=view)

    async def game_callback(interaction: discord.Interaction):
        selected_game = next(game for game in upcoming_games if f"{game['teams']['away']['abbreviation']}_{game['teams']['home']['abbreviation']}" == game_select.values[0])
        
        team_view = discord.ui.View()
        team_select = TeamSelect(selected_game['teams']['away'], selected_game['teams']['home'])
        team_view.add_item(team_select)

        await interaction.response.edit_message(content="Select a team to bet on:", view=team_view)

        async def team_callback(interaction: discord.Interaction):
            selected_team = team_select.values[0]
            await interaction.response.send_modal(BetModal(selected_team, interaction.user.id, selected_game))

        team_select.callback = team_callback

    game_select.callback = game_callback

async def show_current_bets(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 have no active bets.")
        return

    embed = discord.Embed(title="Your Current Bets", color=0x787878)
    scores = await fetch_nhl_scores()
    
    for i, bet in enumerate(user_bets[user_id]):
        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)
        
        start_time = datetime.fromisoformat(bet['game_data']['startTime'].replace('Z', '+00:00'))
        current_time = datetime.now(timezone.utc)
        
        if current_time < start_time:
            status = f"Starts <t:{int(start_time.timestamp())}:R>"
            score = "N/A"
        else:
            status = game['status']['state'] if game else "Unknown"
            score = f"{game['scores'][bet['game_data']['teams']['away']['abbreviation']]} - {game['scores'][bet['game_data']['teams']['home']['abbreviation']]}" if game and 'scores' in game else "Unknown"
        
        embed.add_field(name=f"Bet {i+1}", value=(
            f"Team: {bet['team']}\n"
            f"Amount: ${bet['amount']}\n"
            f"Game: {bet['game_data']['teams']['away']['teamName']} vs {bet['game_data']['teams']['home']['teamName']}\n"
            f"Status: {status}\n"
            f"Current Score: {score}\n"
            f"Start Time: <t:{int(start_time.timestamp())}:F>"
        ), inline=False)

    view = discord.ui.View()
    cancel_select = discord.ui.Select(placeholder="Select a bet to cancel", options=[
        discord.SelectOption(label=f"Bet {i+1}", value=str(i)) for i in range(len(user_bets[user_id]))
    ])
    view.add_item(cancel_select)

    async def cancel_callback(interaction: discord.Interaction):
        if interaction.user.id != user_id:
            await interaction.response.send_message("You cannot cancel other users' bets.", ephemeral=True)
            return

        bet_index = int(cancel_select.values[0])
        cancelled_bet = user_bets[user_id].pop(bet_index)
        user_cash[user_id] += cancelled_bet['amount']
        await interaction.response.send_message(f"Bet cancelled. ${cancelled_bet['amount']} has been refunded")

    cancel_select.callback = cancel_callback

    await interaction.response.send_message(embed=embed, view=view)

@app_commands.command(name="sportbet", description="Bet on sports games")
async def sportbet(interaction: discord.Interaction):
    user_id = interaction.user.id
    if user_id in user_bets and user_bets[user_id]:
        view = SportBetView()
        await interaction.response.send_message("?", view=view)
    else:
        await show_game_selection(interaction)