coollsd commited on
Commit
a4b7e80
·
verified ·
1 Parent(s): 18a9237

Update sportbet.py

Browse files
Files changed (1) hide show
  1. sportbet.py +33 -24
sportbet.py CHANGED
@@ -35,7 +35,7 @@ class TeamSelect(discord.ui.Select):
35
  super().__init__(placeholder="Select a team to bet on", options=options)
36
 
37
  class BetModal(discord.ui.Modal, title="Place Your Bet"):
38
- bet_amount = discord.ui.TextInput(label="Bet Amount", placeholder="Enter bet amount")
39
 
40
  def __init__(self, team, user_id, game_data):
41
  super().__init__()
@@ -53,7 +53,7 @@ class BetModal(discord.ui.Modal, title="Place Your Bet"):
53
 
54
  user_cash[self.user_id] -= bet_amount
55
  await interaction.response.send_message(f"Bet placed on {self.team} for ${bet_amount}")
56
-
57
  user = await interaction.client.fetch_user(self.user_id)
58
  embed = discord.Embed(title="Bet Placed", color=0x787878)
59
  embed.add_field(name="Team", value=self.team, inline=False)
@@ -67,16 +67,19 @@ class BetModal(discord.ui.Modal, title="Place Your Bet"):
67
  user_bets[self.user_id].append({
68
  "team": self.team,
69
  "amount": bet_amount,
70
- "game_data": self.game_data
 
71
  })
72
-
73
  asyncio.create_task(self.monitor_game(interaction, bet_amount))
74
  except ValueError as e:
75
  await interaction.response.send_message(str(e), ephemeral=True)
76
 
77
  async def monitor_game(self, interaction, bet_amount):
78
  game_start = datetime.fromisoformat(self.game_data['eventTime'].replace('Z', '+00:00'))
79
- await asyncio.sleep((game_start - datetime.now(timezone.utc)).total_seconds())
 
 
80
 
81
  user = await interaction.client.fetch_user(self.user_id)
82
  await user.send(f"Your team {self.team} has started playing!")
@@ -85,23 +88,27 @@ class BetModal(discord.ui.Modal, title="Place Your Bet"):
85
 
86
  while True:
87
  scores = await fetch_nhl_scores()
88
- game = next((g for g in scores['sectionList'][0]['events'] if g['lowerTeam']['name'] == self.game_data['lowerTeam']['name'] and
 
89
  g['upperTeam']['name'] == self.game_data['upperTeam']['name']), None)
90
 
91
- if game is None or game['eventStatus'] == 'PREVIEW':
92
  await asyncio.sleep(60)
93
  continue
94
 
95
- current_scores = {g['lowerTeam']['name']: g.get('score', 0), g['upperTeam']['name']: g.get('score', 0)}
96
-
 
 
 
97
  for team in [self.game_data['lowerTeam']['name'], self.game_data['upperTeam']['name']]:
98
  if current_scores[team] > previous_scores[team]:
99
  team_name = self.game_data['lowerTeam']['longName'] if team == self.game_data['lowerTeam']['name'] else self.game_data['upperTeam']['longName']
100
  await user.send(f"{team_name} SCORED! Current score: {current_scores[self.game_data['lowerTeam']['name']]} - {current_scores[self.game_data['upperTeam']['name']]}")
101
-
102
  previous_scores = current_scores.copy()
103
-
104
- if game['eventStatus'] == 'FINAL':
105
  winner = self.game_data['lowerTeam']['name'] if current_scores[self.game_data['lowerTeam']['name']] > current_scores[self.game_data['upperTeam']['name']] else self.game_data['upperTeam']['name']
106
  if winner == self.team:
107
  winnings = bet_amount * 2
@@ -109,10 +116,10 @@ class BetModal(discord.ui.Modal, title="Place Your Bet"):
109
  await user.send(f"Congratulations! Your team won. You won ${winnings}!")
110
  else:
111
  await user.send(f"Sorry, your team lost. Better luck next time!")
112
-
113
  user_bets[self.user_id] = [bet for bet in user_bets[self.user_id] if bet['game_data'] != self.game_data]
114
  break
115
-
116
  await asyncio.sleep(60) # Check for updates every minute
117
 
118
  class SportBetView(discord.ui.View):
@@ -129,7 +136,8 @@ class SportBetView(discord.ui.View):
129
 
130
  async def show_game_selection(interaction: discord.Interaction):
131
  scores = await fetch_nhl_scores()
132
- upcoming_games = [game for game in scores['sectionList'][0]['events'] if game['eventStatus'] == 'PREVIEW']
 
133
 
134
  if not upcoming_games:
135
  await interaction.response.send_message("No games available for betting.")
@@ -166,9 +174,10 @@ async def show_current_bets(interaction: discord.Interaction):
166
 
167
  embed = discord.Embed(title="Your Current Bets", color=0x787878)
168
  scores = await fetch_nhl_scores()
169
-
 
170
  for i, bet in enumerate(user_bets[user_id]):
171
- game = next((g for g in scores['sectionList'][0]['events'] if g['lowerTeam']['name'] == bet['game_data']['lowerTeam']['name'] and
172
  g['upperTeam']['name'] == bet['game_data']['upperTeam']['name']), None)
173
 
174
  start_time = datetime.fromisoformat(bet['game_data']['eventTime'].replace('Z', '+00:00'))
@@ -178,16 +187,16 @@ async def show_current_bets(interaction: discord.Interaction):
178
  status = f"Starts <t:{int(start_time.timestamp())}:R>"
179
  score = "N/A"
180
  else:
181
- status = game['eventStatus'] if game else "Unknown"
182
  score = f"{game.get('lowerTeam', {}).get('score', 'Unknown')} - {game.get('upperTeam', {}).get('score', 'Unknown')}" if game else "Unknown"
183
 
184
  embed.add_field(name=f"Bet {i+1}", value=(
185
- f"Team: {bet['team']}\n"
186
- f"Amount: ${bet['amount']}\n"
187
- f"Game: {bet['game_data']['lowerTeam']['longName']} vs {bet['game_data']['upperTeam']['longName']}\n"
188
- f"Status: {status}\n"
189
- f"Current Score: {score}\n"
190
- f"Start Time: <t:{int(start_time.timestamp())}:F>"
191
  ), inline=False)
192
 
193
  view = discord.ui.View()
 
35
  super().__init__(placeholder="Select a team to bet on", options=options)
36
 
37
  class BetModal(discord.ui.Modal, title="Place Your Bet"):
38
+ bet_amount = discord.ui.TextInput(label="Bet Amount", placeholder="Enter bet amount", required=True)
39
 
40
  def __init__(self, team, user_id, game_data):
41
  super().__init__()
 
53
 
54
  user_cash[self.user_id] -= bet_amount
55
  await interaction.response.send_message(f"Bet placed on {self.team} for ${bet_amount}")
56
+
57
  user = await interaction.client.fetch_user(self.user_id)
58
  embed = discord.Embed(title="Bet Placed", color=0x787878)
59
  embed.add_field(name="Team", value=self.team, inline=False)
 
67
  user_bets[self.user_id].append({
68
  "team": self.team,
69
  "amount": bet_amount,
70
+ "game_data": self.game_data,
71
+ "bet_time": datetime.now(timezone.utc)
72
  })
73
+
74
  asyncio.create_task(self.monitor_game(interaction, bet_amount))
75
  except ValueError as e:
76
  await interaction.response.send_message(str(e), ephemeral=True)
77
 
78
  async def monitor_game(self, interaction, bet_amount):
79
  game_start = datetime.fromisoformat(self.game_data['eventTime'].replace('Z', '+00:00'))
80
+ sleep_duration = (game_start - datetime.now(timezone.utc)).total_seconds()
81
+ if sleep_duration > 0:
82
+ await asyncio.sleep(sleep_duration)
83
 
84
  user = await interaction.client.fetch_user(self.user_id)
85
  await user.send(f"Your team {self.team} has started playing!")
 
88
 
89
  while True:
90
  scores = await fetch_nhl_scores()
91
+ events = scores.get('sectionList', [])[0].get('events', [])
92
+ game = next((g for g in events if g['lowerTeam']['name'] == self.game_data['lowerTeam']['name'] and
93
  g['upperTeam']['name'] == self.game_data['upperTeam']['name']), None)
94
 
95
+ if game is None or game['eventStatus'] < 2:
96
  await asyncio.sleep(60)
97
  continue
98
 
99
+ current_scores = {
100
+ game['lowerTeam']['name']: game.get('score', 0),
101
+ game['upperTeam']['name']: game.get('score', 0)
102
+ }
103
+
104
  for team in [self.game_data['lowerTeam']['name'], self.game_data['upperTeam']['name']]:
105
  if current_scores[team] > previous_scores[team]:
106
  team_name = self.game_data['lowerTeam']['longName'] if team == self.game_data['lowerTeam']['name'] else self.game_data['upperTeam']['longName']
107
  await user.send(f"{team_name} SCORED! Current score: {current_scores[self.game_data['lowerTeam']['name']]} - {current_scores[self.game_data['upperTeam']['name']]}")
108
+
109
  previous_scores = current_scores.copy()
110
+
111
+ if game['eventStatus'] == 3:
112
  winner = self.game_data['lowerTeam']['name'] if current_scores[self.game_data['lowerTeam']['name']] > current_scores[self.game_data['upperTeam']['name']] else self.game_data['upperTeam']['name']
113
  if winner == self.team:
114
  winnings = bet_amount * 2
 
116
  await user.send(f"Congratulations! Your team won. You won ${winnings}!")
117
  else:
118
  await user.send(f"Sorry, your team lost. Better luck next time!")
119
+
120
  user_bets[self.user_id] = [bet for bet in user_bets[self.user_id] if bet['game_data'] != self.game_data]
121
  break
122
+
123
  await asyncio.sleep(60) # Check for updates every minute
124
 
125
  class SportBetView(discord.ui.View):
 
136
 
137
  async def show_game_selection(interaction: discord.Interaction):
138
  scores = await fetch_nhl_scores()
139
+ events = scores.get('sectionList', [])[0].get('events', [])
140
+ upcoming_games = [game for game in events if game['eventStatus'] == 2]
141
 
142
  if not upcoming_games:
143
  await interaction.response.send_message("No games available for betting.")
 
174
 
175
  embed = discord.Embed(title="Your Current Bets", color=0x787878)
176
  scores = await fetch_nhl_scores()
177
+ events = scores.get('sectionList', [])[0].get('events', [])
178
+
179
  for i, bet in enumerate(user_bets[user_id]):
180
+ game = next((g for g in events if g['lowerTeam']['name'] == bet['game_data']['lowerTeam']['name'] and
181
  g['upperTeam']['name'] == bet['game_data']['upperTeam']['name']), None)
182
 
183
  start_time = datetime.fromisoformat(bet['game_data']['eventTime'].replace('Z', '+00:00'))
 
187
  status = f"Starts <t:{int(start_time.timestamp())}:R>"
188
  score = "N/A"
189
  else:
190
+ status = "FINAL" if game and game['eventStatus'] == 3 else "In Progress"
191
  score = f"{game.get('lowerTeam', {}).get('score', 'Unknown')} - {game.get('upperTeam', {}).get('score', 'Unknown')}" if game else "Unknown"
192
 
193
  embed.add_field(name=f"Bet {i+1}", value=(
194
+ f"**Team:** {bet['team']}\n"
195
+ f"**Amount:** ${bet['amount']}\n"
196
+ f"**Game:** {bet['game_data']['lowerTeam']['longName']} vs {bet['game_data']['upperTeam']['longName']}\n"
197
+ f"**Status:** {status}\n"
198
+ f"**Current Score:** {score}\n"
199
+ f"**Start Time:** <t:{int(start_time.timestamp())}:F>"
200
  ), inline=False)
201
 
202
  view = discord.ui.View()