coollsd commited on
Commit
5c5186f
·
verified ·
1 Parent(s): 7d4d0ad

Update sportbet.py

Browse files
Files changed (1) hide show
  1. sportbet.py +28 -14
sportbet.py CHANGED
@@ -2,7 +2,7 @@ import discord
2
  from discord import app_commands
3
  import aiohttp
4
  import asyncio
5
- from datetime import datetime, timezone
6
 
7
  user_cash = {}
8
  user_bets = {}
@@ -32,13 +32,27 @@ async def fetch_nhl_scores():
32
  async with session.get("https://nhl-score-api.herokuapp.com/api/scores/latest") as response:
33
  return await response.json()
34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  class GameSelect(discord.ui.Select):
36
  def __init__(self, games):
37
  options = [
38
  discord.SelectOption(
39
  label=f"{game['teams']['away']['teamName']} vs {game['teams']['home']['teamName']}",
40
  value=f"{game['teams']['away']['abbreviation']}_{game['teams']['home']['abbreviation']}",
41
- description=f"Start time: {game['startTime']}"
42
  ) for game in games
43
  ]
44
  super().__init__(placeholder="Select a game", options=options)
@@ -66,7 +80,7 @@ class BetModal(discord.ui.Modal, title="Place Your Bet"):
66
  if bet_amount <= 0:
67
  raise ValueError("Bet more than 0 dollars")
68
  if bet_amount > user_cash.get(self.user_id, 0):
69
- raise ValueError("Insufficient funds")
70
 
71
  user_cash[self.user_id] -= bet_amount
72
  save_database()
@@ -77,7 +91,7 @@ class BetModal(discord.ui.Modal, title="Place Your Bet"):
77
  embed.add_field(name="Team", value=self.team, inline=False)
78
  embed.add_field(name="Amount", value=f"${bet_amount}", inline=False)
79
  embed.add_field(name="Game", value=f"{self.game_data['teams']['away']['teamName']} vs {self.game_data['teams']['home']['teamName']}", inline=False)
80
- embed.add_field(name="Start Time", value=self.game_data['startTime'], inline=False)
81
  await user.send(embed=embed)
82
 
83
  if self.user_id not in user_bets:
@@ -107,29 +121,29 @@ class BetModal(discord.ui.Modal, title="Place Your Bet"):
107
  winnings = bet_amount * 2
108
  user_cash[self.user_id] += winnings
109
  save_database()
110
- await interaction.user.send(f"Congratulations! Your team won. You won ${winnings}!")
111
  else:
112
- await interaction.user.send(f"Sorry, your team lost. Better luck next time!")
113
 
114
  user_bets[self.user_id] = [bet for bet in user_bets[self.user_id] if bet['game_data'] != self.game_data]
115
  break
116
 
117
  await asyncio.sleep(300)
118
 
119
- @app_commands.command(name="sportbet", description="Bet on sports games")
120
  async def sportbet(interaction: discord.Interaction):
121
  scores = await fetch_nhl_scores()
122
  upcoming_games = [game for game in scores['games'] if game['status']['state'] == 'PREVIEW']
123
 
124
  if not upcoming_games:
125
- await interaction.response.send_message("No games available for betting.")
126
  return
127
 
128
  view = discord.ui.View()
129
  game_select = GameSelect(upcoming_games)
130
  view.add_item(game_select)
131
 
132
- await interaction.response.send_message("Select a game to bet on:", view=view)
133
 
134
  async def game_callback(interaction: discord.Interaction):
135
  selected_game = next(game for game in upcoming_games if f"{game['teams']['away']['abbreviation']}_{game['teams']['home']['abbreviation']}" == game_select.values[0])
@@ -138,7 +152,7 @@ async def sportbet(interaction: discord.Interaction):
138
  team_select = TeamSelect(selected_game['teams']['away'], selected_game['teams']['home'])
139
  team_view.add_item(team_select)
140
 
141
- await interaction.response.edit_message(content="Select a team to bet on:", view=team_view)
142
 
143
  async def team_callback(interaction: discord.Interaction):
144
  selected_team = team_select.values[0]
@@ -148,11 +162,11 @@ async def sportbet(interaction: discord.Interaction):
148
 
149
  game_select.callback = game_callback
150
 
151
- @app_commands.command(name="currentbets", description="View your current bets")
152
  async def currentbets(interaction: discord.Interaction):
153
  user_id = interaction.user.id
154
  if user_id not in user_bets or not user_bets[user_id]:
155
- await interaction.response.send_message("You have no active bets.")
156
  return
157
 
158
  embed = discord.Embed(title="Your Current Bets", color=0x787878)
@@ -189,11 +203,11 @@ async def cash(interaction: discord.Interaction):
189
  user_cash[user_id] = 1000
190
  balance = 1000
191
  message = "You have no cash. Here's $1,000 to start!"
 
192
  else:
193
  message = f"Your current balance is ${balance:,}"
194
 
195
  embed = discord.Embed(title="Cash Balance", description=message, color=0x787878)
196
  embed.set_footer(text="Use /sportbet to bet your cash!")
197
 
198
- await interaction.response.send_message(embed=embed)
199
- save_database()
 
2
  from discord import app_commands
3
  import aiohttp
4
  import asyncio
5
+ from datetime import datetime, timezone, timedelta
6
 
7
  user_cash = {}
8
  user_bets = {}
 
32
  async with session.get("https://nhl-score-api.herokuapp.com/api/scores/latest") as response:
33
  return await response.json()
34
 
35
+ def format_time_difference(start_time):
36
+ now = datetime.now(timezone.utc)
37
+ start = datetime.fromisoformat(start_time.replace('Z', '+00:00'))
38
+ diff = start - now
39
+
40
+ if diff < timedelta(0):
41
+ return "Game has already started"
42
+ elif diff < timedelta(hours=1):
43
+ return f"{diff.seconds // 60} minutes from now"
44
+ elif diff < timedelta(days=1):
45
+ return f"{diff.seconds // 3600} hours from now"
46
+ else:
47
+ return f"{diff.days} days from now"
48
+
49
  class GameSelect(discord.ui.Select):
50
  def __init__(self, games):
51
  options = [
52
  discord.SelectOption(
53
  label=f"{game['teams']['away']['teamName']} vs {game['teams']['home']['teamName']}",
54
  value=f"{game['teams']['away']['abbreviation']}_{game['teams']['home']['abbreviation']}",
55
+ description=f"Start: {format_time_difference(game['startTime'])}"
56
  ) for game in games
57
  ]
58
  super().__init__(placeholder="Select a game", options=options)
 
80
  if bet_amount <= 0:
81
  raise ValueError("Bet more than 0 dollars")
82
  if bet_amount > user_cash.get(self.user_id, 0):
83
+ raise ValueError(f"Insufficient funds. Your balance is ${user_cash.get(self.user_id, 0)}")
84
 
85
  user_cash[self.user_id] -= bet_amount
86
  save_database()
 
91
  embed.add_field(name="Team", value=self.team, inline=False)
92
  embed.add_field(name="Amount", value=f"${bet_amount}", inline=False)
93
  embed.add_field(name="Game", value=f"{self.game_data['teams']['away']['teamName']} vs {self.game_data['teams']['home']['teamName']}", inline=False)
94
+ embed.add_field(name="Start Time", value=format_time_difference(self.game_data['startTime']), inline=False)
95
  await user.send(embed=embed)
96
 
97
  if self.user_id not in user_bets:
 
121
  winnings = bet_amount * 2
122
  user_cash[self.user_id] += winnings
123
  save_database()
124
+ await interaction.user.send(f"WOO YOUR TEAM WON you won ${winnings}!")
125
  else:
126
+ await interaction.user.send(f"Sorry, your team lost booo!")
127
 
128
  user_bets[self.user_id] = [bet for bet in user_bets[self.user_id] if bet['game_data'] != self.game_data]
129
  break
130
 
131
  await asyncio.sleep(300)
132
 
133
+ @app_commands.command(name="sportbet", description="bet on sports game")
134
  async def sportbet(interaction: discord.Interaction):
135
  scores = await fetch_nhl_scores()
136
  upcoming_games = [game for game in scores['games'] if game['status']['state'] == 'PREVIEW']
137
 
138
  if not upcoming_games:
139
+ await interaction.response.send_message("No games for betting.")
140
  return
141
 
142
  view = discord.ui.View()
143
  game_select = GameSelect(upcoming_games)
144
  view.add_item(game_select)
145
 
146
+ await interaction.response.send_message("game to bet on:", view=view)
147
 
148
  async def game_callback(interaction: discord.Interaction):
149
  selected_game = next(game for game in upcoming_games if f"{game['teams']['away']['abbreviation']}_{game['teams']['home']['abbreviation']}" == game_select.values[0])
 
152
  team_select = TeamSelect(selected_game['teams']['away'], selected_game['teams']['home'])
153
  team_view.add_item(team_select)
154
 
155
+ await interaction.response.edit_message(content="team to bet on:", view=team_view)
156
 
157
  async def team_callback(interaction: discord.Interaction):
158
  selected_team = team_select.values[0]
 
162
 
163
  game_select.callback = game_callback
164
 
165
+ @app_commands.command(name="currentbets", description="view your bets")
166
  async def currentbets(interaction: discord.Interaction):
167
  user_id = interaction.user.id
168
  if user_id not in user_bets or not user_bets[user_id]:
169
+ await interaction.response.send_message("You have no bets.")
170
  return
171
 
172
  embed = discord.Embed(title="Your Current Bets", color=0x787878)
 
203
  user_cash[user_id] = 1000
204
  balance = 1000
205
  message = "You have no cash. Here's $1,000 to start!"
206
+ save_database()
207
  else:
208
  message = f"Your current balance is ${balance:,}"
209
 
210
  embed = discord.Embed(title="Cash Balance", description=message, color=0x787878)
211
  embed.set_footer(text="Use /sportbet to bet your cash!")
212
 
213
+ await interaction.response.send_message(embed=embed)