Spaces:
Building
Building
Update sportbet.py
Browse files- sportbet.py +23 -11
sportbet.py
CHANGED
@@ -7,6 +7,11 @@ from datetime import datetime, timezone
|
|
7 |
user_cash = {}
|
8 |
user_bets = {}
|
9 |
|
|
|
|
|
|
|
|
|
|
|
10 |
def load_database():
|
11 |
global user_cash
|
12 |
try:
|
@@ -64,17 +69,14 @@ class BetModal(discord.ui.Modal, title="Place Your Bet"):
|
|
64 |
raise ValueError("poor")
|
65 |
|
66 |
user_cash[self.user_id] -= bet_amount
|
67 |
-
|
|
|
68 |
user = await interaction.client.fetch_user(self.user_id)
|
69 |
embed = discord.Embed(title="Bet Placed", color=0x787878)
|
70 |
embed.add_field(name="Team", value=self.team, inline=False)
|
71 |
embed.add_field(name="Amount", value=f"${bet_amount}", inline=False)
|
72 |
embed.add_field(name="Game", value=f"{self.game_data['teams']['away']['teamName']} vs {self.game_data['teams']['home']['teamName']}", inline=False)
|
73 |
-
|
74 |
-
start_time = datetime.fromisoformat(self.game_data['startTime'].replace('Z', '+00:00'))
|
75 |
-
discord_time = discord.utils.format_dt(start_time, style='F')
|
76 |
-
embed.add_field(name="Start Time", value=discord_time, inline=False)
|
77 |
-
|
78 |
await user.send(embed=embed)
|
79 |
|
80 |
if self.user_id not in user_bets:
|
@@ -88,7 +90,6 @@ class BetModal(discord.ui.Modal, title="Place Your Bet"):
|
|
88 |
view = discord.ui.View()
|
89 |
view.add_item(discord.ui.Button(label="Bet Again", custom_id="bet_again"))
|
90 |
view.add_item(discord.ui.Button(label="View Current Bets", custom_id="view_bets"))
|
91 |
-
|
92 |
await interaction.response.send_message("Bet placed successfully!", view=view)
|
93 |
|
94 |
asyncio.create_task(self.monitor_game(interaction, bet_amount))
|
@@ -109,6 +110,7 @@ class BetModal(discord.ui.Modal, title="Place Your Bet"):
|
|
109 |
if game['teams'][winner]['abbreviation'] == self.team:
|
110 |
winnings = bet_amount * 2
|
111 |
user_cash[self.user_id] += winnings
|
|
|
112 |
await interaction.user.send(f"WOO YOUR TEAM WON you won ${winnings}!")
|
113 |
else:
|
114 |
await interaction.user.send(f"Sorry, your team lost booo!")
|
@@ -120,6 +122,13 @@ class BetModal(discord.ui.Modal, title="Place Your Bet"):
|
|
120 |
|
121 |
@app_commands.command(name="sportbet", description="bet on sports game")
|
122 |
async def sportbet(interaction: discord.Interaction):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
123 |
scores = await fetch_nhl_scores()
|
124 |
upcoming_games = [game for game in scores['games'] if game['status']['state'] == 'PREVIEW']
|
125 |
|
@@ -153,14 +162,16 @@ async def sportbet(interaction: discord.Interaction):
|
|
153 |
async def view_current_bets(interaction: discord.Interaction):
|
154 |
user_id = interaction.user.id
|
155 |
if user_id not in user_bets or not user_bets[user_id]:
|
156 |
-
await interaction.response.send_message("You have no
|
157 |
return
|
158 |
|
159 |
embed = discord.Embed(title="Your Current Bets", color=0x787878)
|
160 |
for i, bet in enumerate(user_bets[user_id]):
|
161 |
-
|
162 |
-
|
163 |
-
|
|
|
|
|
164 |
|
165 |
view = discord.ui.View()
|
166 |
cancel_select = discord.ui.Select(placeholder="Select a bet to cancel", options=[
|
@@ -172,6 +183,7 @@ async def view_current_bets(interaction: discord.Interaction):
|
|
172 |
bet_index = int(cancel_select.values[0])
|
173 |
cancelled_bet = user_bets[user_id].pop(bet_index)
|
174 |
user_cash[user_id] += cancelled_bet['amount']
|
|
|
175 |
await interaction.response.send_message(f"Bet cancelled. ${cancelled_bet['amount']} has been refunded.", ephemeral=True)
|
176 |
|
177 |
cancel_select.callback = cancel_callback
|
|
|
7 |
user_cash = {}
|
8 |
user_bets = {}
|
9 |
|
10 |
+
def save_database():
|
11 |
+
with open("database.txt", "w") as f:
|
12 |
+
for user_id, cash in user_cash.items():
|
13 |
+
f.write(f"{user_id} cash({cash})\n")
|
14 |
+
|
15 |
def load_database():
|
16 |
global user_cash
|
17 |
try:
|
|
|
69 |
raise ValueError("poor")
|
70 |
|
71 |
user_cash[self.user_id] -= bet_amount
|
72 |
+
save_database() # Save the database after updating cash
|
73 |
+
|
74 |
user = await interaction.client.fetch_user(self.user_id)
|
75 |
embed = discord.Embed(title="Bet Placed", color=0x787878)
|
76 |
embed.add_field(name="Team", value=self.team, inline=False)
|
77 |
embed.add_field(name="Amount", value=f"${bet_amount}", inline=False)
|
78 |
embed.add_field(name="Game", value=f"{self.game_data['teams']['away']['teamName']} vs {self.game_data['teams']['home']['teamName']}", inline=False)
|
79 |
+
embed.add_field(name="Start Time", value=discord.utils.format_dt(datetime.fromisoformat(self.game_data['startTime'].replace('Z', '+00:00')), style='F'), inline=False)
|
|
|
|
|
|
|
|
|
80 |
await user.send(embed=embed)
|
81 |
|
82 |
if self.user_id not in user_bets:
|
|
|
90 |
view = discord.ui.View()
|
91 |
view.add_item(discord.ui.Button(label="Bet Again", custom_id="bet_again"))
|
92 |
view.add_item(discord.ui.Button(label="View Current Bets", custom_id="view_bets"))
|
|
|
93 |
await interaction.response.send_message("Bet placed successfully!", view=view)
|
94 |
|
95 |
asyncio.create_task(self.monitor_game(interaction, bet_amount))
|
|
|
110 |
if game['teams'][winner]['abbreviation'] == self.team:
|
111 |
winnings = bet_amount * 2
|
112 |
user_cash[self.user_id] += winnings
|
113 |
+
save_database() # Save the database after updating cash
|
114 |
await interaction.user.send(f"WOO YOUR TEAM WON you won ${winnings}!")
|
115 |
else:
|
116 |
await interaction.user.send(f"Sorry, your team lost booo!")
|
|
|
122 |
|
123 |
@app_commands.command(name="sportbet", description="bet on sports game")
|
124 |
async def sportbet(interaction: discord.Interaction):
|
125 |
+
if interaction.user.id in user_bets and user_bets[interaction.user.id]:
|
126 |
+
view = discord.ui.View()
|
127 |
+
view.add_item(discord.ui.Button(label="Bet Again", custom_id="bet_again"))
|
128 |
+
view.add_item(discord.ui.Button(label="View Current Bets", custom_id="view_bets"))
|
129 |
+
await interaction.response.send_message("You have active bets. What would you like to do?", view=view)
|
130 |
+
return
|
131 |
+
|
132 |
scores = await fetch_nhl_scores()
|
133 |
upcoming_games = [game for game in scores['games'] if game['status']['state'] == 'PREVIEW']
|
134 |
|
|
|
162 |
async def view_current_bets(interaction: discord.Interaction):
|
163 |
user_id = interaction.user.id
|
164 |
if user_id not in user_bets or not user_bets[user_id]:
|
165 |
+
await interaction.response.send_message("You have no bets.", ephemeral=True)
|
166 |
return
|
167 |
|
168 |
embed = discord.Embed(title="Your Current Bets", color=0x787878)
|
169 |
for i, bet in enumerate(user_bets[user_id]):
|
170 |
+
scores = await fetch_nhl_scores()
|
171 |
+
game = next((g for g in scores['games'] if g['teams']['away']['abbreviation'] == bet['game_data']['teams']['away']['abbreviation'] and
|
172 |
+
g['teams']['home']['abbreviation'] == bet['game_data']['teams']['home']['abbreviation']), None)
|
173 |
+
|
174 |
+
embed.add_field(name=f"Bet {i+1}", value=f"Team: {bet['team']}\nAmount: ${bet['amount']}\nGame: {bet['game_data']['teams']['away']['teamName']} vs {bet['game_data']['teams']['home']['teamName']}\nCurrent Score: {game['scores']['away']} - {game['scores']['home']}", inline=False)
|
175 |
|
176 |
view = discord.ui.View()
|
177 |
cancel_select = discord.ui.Select(placeholder="Select a bet to cancel", options=[
|
|
|
183 |
bet_index = int(cancel_select.values[0])
|
184 |
cancelled_bet = user_bets[user_id].pop(bet_index)
|
185 |
user_cash[user_id] += cancelled_bet['amount']
|
186 |
+
save_database() # Save the database after updating cash
|
187 |
await interaction.response.send_message(f"Bet cancelled. ${cancelled_bet['amount']} has been refunded.", ephemeral=True)
|
188 |
|
189 |
cancel_select.callback = cancel_callback
|