Spaces:
Building
Building
Update sportbet.py
Browse files- sportbet.py +32 -51
sportbet.py
CHANGED
@@ -7,11 +7,6 @@ from datetime import datetime, timezone
|
|
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,13 +64,15 @@ class BetModal(discord.ui.Modal, title="Place Your Bet"):
|
|
69 |
raise ValueError("poor")
|
70 |
|
71 |
user_cash[self.user_id] -= bet_amount
|
72 |
-
|
73 |
-
|
|
|
74 |
embed = discord.Embed(title="Bet Placed", color=0x787878)
|
75 |
embed.add_field(name="Team", value=self.team, inline=False)
|
76 |
embed.add_field(name="Amount", value=f"${bet_amount}", inline=False)
|
77 |
embed.add_field(name="Game", value=f"{self.game_data['teams']['away']['teamName']} vs {self.game_data['teams']['home']['teamName']}", inline=False)
|
78 |
-
embed.add_field(name="Start Time", value=
|
|
|
79 |
|
80 |
if self.user_id not in user_bets:
|
81 |
user_bets[self.user_id] = []
|
@@ -85,11 +82,6 @@ class BetModal(discord.ui.Modal, title="Place Your Bet"):
|
|
85 |
"game_data": self.game_data
|
86 |
})
|
87 |
|
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 |
-
await interaction.response.send_message(embed=embed, view=view)
|
92 |
-
|
93 |
asyncio.create_task(self.monitor_game(interaction, bet_amount))
|
94 |
except ValueError as e:
|
95 |
await interaction.response.send_message(str(e), ephemeral=True)
|
@@ -108,7 +100,6 @@ class BetModal(discord.ui.Modal, title="Place Your Bet"):
|
|
108 |
if game['teams'][winner]['abbreviation'] == self.team:
|
109 |
winnings = bet_amount * 2
|
110 |
user_cash[self.user_id] += winnings
|
111 |
-
save_database()
|
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!")
|
@@ -118,38 +109,8 @@ class BetModal(discord.ui.Modal, title="Place Your Bet"):
|
|
118 |
|
119 |
await asyncio.sleep(300)
|
120 |
|
121 |
-
class SportBetView(discord.ui.View):
|
122 |
-
def __init__(self, upcoming_games):
|
123 |
-
super().__init__()
|
124 |
-
self.add_item(GameSelect(upcoming_games))
|
125 |
-
|
126 |
-
@discord.ui.select(cls=GameSelect)
|
127 |
-
async def on_game_select(self, interaction: discord.Interaction, select: GameSelect):
|
128 |
-
selected_game = next(game for game in self.upcoming_games if f"{game['teams']['away']['abbreviation']}_{game['teams']['home']['abbreviation']}" == select.values[0])
|
129 |
-
|
130 |
-
team_view = discord.ui.View()
|
131 |
-
team_select = TeamSelect(selected_game['teams']['away'], selected_game['teams']['home'])
|
132 |
-
team_view.add_item(team_select)
|
133 |
-
|
134 |
-
@discord.ui.select(cls=TeamSelect)
|
135 |
-
async def on_team_select(interaction: discord.Interaction, select: TeamSelect):
|
136 |
-
selected_team = select.values[0]
|
137 |
-
modal = BetModal(selected_team, interaction.user.id, selected_game)
|
138 |
-
await interaction.response.send_modal(modal)
|
139 |
-
|
140 |
-
team_view.on_team_select = on_team_select
|
141 |
-
|
142 |
-
await interaction.response.edit_message(content="team to bet on:", view=team_view)
|
143 |
-
|
144 |
@app_commands.command(name="sportbet", description="bet on sports game")
|
145 |
async def sportbet(interaction: discord.Interaction):
|
146 |
-
if interaction.user.id in user_bets and user_bets[interaction.user.id]:
|
147 |
-
view = discord.ui.View()
|
148 |
-
view.add_item(discord.ui.Button(label="Bet Again", custom_id="bet_again"))
|
149 |
-
view.add_item(discord.ui.Button(label="View Current Bets", custom_id="view_bets"))
|
150 |
-
await interaction.response.send_message("You have active bets. What would you like to do?", view=view)
|
151 |
-
return
|
152 |
-
|
153 |
scores = await fetch_nhl_scores()
|
154 |
upcoming_games = [game for game in scores['games'] if game['status']['state'] == 'PREVIEW']
|
155 |
|
@@ -157,13 +118,34 @@ async def sportbet(interaction: discord.Interaction):
|
|
157 |
await interaction.response.send_message("No games for betting.")
|
158 |
return
|
159 |
|
160 |
-
view =
|
|
|
|
|
|
|
161 |
await interaction.response.send_message("game to bet on:", view=view)
|
162 |
|
163 |
-
async def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
164 |
user_id = interaction.user.id
|
165 |
if user_id not in user_bets or not user_bets[user_id]:
|
166 |
-
await interaction.response.send_message("You have no bets."
|
167 |
return
|
168 |
|
169 |
embed = discord.Embed(title="Your Current Bets", color=0x787878)
|
@@ -176,7 +158,7 @@ async def view_current_bets(interaction: discord.Interaction):
|
|
176 |
|
177 |
view = discord.ui.View()
|
178 |
cancel_select = discord.ui.Select(placeholder="Select a bet to cancel", options=[
|
179 |
-
discord.SelectOption(label=f"
|
180 |
])
|
181 |
view.add_item(cancel_select)
|
182 |
|
@@ -184,9 +166,8 @@ async def view_current_bets(interaction: discord.Interaction):
|
|
184 |
bet_index = int(cancel_select.values[0])
|
185 |
cancelled_bet = user_bets[user_id].pop(bet_index)
|
186 |
user_cash[user_id] += cancelled_bet['amount']
|
187 |
-
|
188 |
-
await interaction.response.send_message(f"Bet cancelled. ${cancelled_bet['amount']} has been refunded.", ephemeral=True)
|
189 |
|
190 |
cancel_select.callback = cancel_callback
|
191 |
|
192 |
-
await interaction.response.send_message(embed=embed, view=view
|
|
|
7 |
user_cash = {}
|
8 |
user_bets = {}
|
9 |
|
|
|
|
|
|
|
|
|
|
|
10 |
def load_database():
|
11 |
global user_cash
|
12 |
try:
|
|
|
64 |
raise ValueError("poor")
|
65 |
|
66 |
user_cash[self.user_id] -= bet_amount
|
67 |
+
await interaction.response.send_message(f"Bet placed on {self.team} for ${bet_amount}")
|
68 |
+
|
69 |
+
user = await interaction.client.fetch_user(self.user_id)
|
70 |
embed = discord.Embed(title="Bet Placed", color=0x787878)
|
71 |
embed.add_field(name="Team", value=self.team, inline=False)
|
72 |
embed.add_field(name="Amount", value=f"${bet_amount}", inline=False)
|
73 |
embed.add_field(name="Game", value=f"{self.game_data['teams']['away']['teamName']} vs {self.game_data['teams']['home']['teamName']}", inline=False)
|
74 |
+
embed.add_field(name="Start Time", value=self.game_data['startTime'], inline=False)
|
75 |
+
await user.send(embed=embed)
|
76 |
|
77 |
if self.user_id not in user_bets:
|
78 |
user_bets[self.user_id] = []
|
|
|
82 |
"game_data": self.game_data
|
83 |
})
|
84 |
|
|
|
|
|
|
|
|
|
|
|
85 |
asyncio.create_task(self.monitor_game(interaction, bet_amount))
|
86 |
except ValueError as e:
|
87 |
await interaction.response.send_message(str(e), ephemeral=True)
|
|
|
100 |
if game['teams'][winner]['abbreviation'] == self.team:
|
101 |
winnings = bet_amount * 2
|
102 |
user_cash[self.user_id] += winnings
|
|
|
103 |
await interaction.user.send(f"WOO YOUR TEAM WON you won ${winnings}!")
|
104 |
else:
|
105 |
await interaction.user.send(f"Sorry, your team lost booo!")
|
|
|
109 |
|
110 |
await asyncio.sleep(300)
|
111 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
112 |
@app_commands.command(name="sportbet", description="bet on sports game")
|
113 |
async def sportbet(interaction: discord.Interaction):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
scores = await fetch_nhl_scores()
|
115 |
upcoming_games = [game for game in scores['games'] if game['status']['state'] == 'PREVIEW']
|
116 |
|
|
|
118 |
await interaction.response.send_message("No games for betting.")
|
119 |
return
|
120 |
|
121 |
+
view = discord.ui.View()
|
122 |
+
game_select = GameSelect(upcoming_games)
|
123 |
+
view.add_item(game_select)
|
124 |
+
|
125 |
await interaction.response.send_message("game to bet on:", view=view)
|
126 |
|
127 |
+
async def game_callback(interaction: discord.Interaction):
|
128 |
+
selected_game = next(game for game in upcoming_games if f"{game['teams']['away']['abbreviation']}_{game['teams']['home']['abbreviation']}" == game_select.values[0])
|
129 |
+
|
130 |
+
team_view = discord.ui.View()
|
131 |
+
team_select = TeamSelect(selected_game['teams']['away'], selected_game['teams']['home'])
|
132 |
+
team_view.add_item(team_select)
|
133 |
+
|
134 |
+
await interaction.response.edit_message(content="team to bet on:", view=team_view)
|
135 |
+
|
136 |
+
async def team_callback(interaction: discord.Interaction):
|
137 |
+
selected_team = team_select.values[0]
|
138 |
+
await interaction.response.send_modal(BetModal(selected_team, interaction.user.id, selected_game))
|
139 |
+
|
140 |
+
team_select.callback = team_callback
|
141 |
+
|
142 |
+
game_select.callback = game_callback
|
143 |
+
|
144 |
+
@app_commands.command(name="currentbets", description="view your bets")
|
145 |
+
async def currentbets(interaction: discord.Interaction):
|
146 |
user_id = interaction.user.id
|
147 |
if user_id not in user_bets or not user_bets[user_id]:
|
148 |
+
await interaction.response.send_message("You have no bets.")
|
149 |
return
|
150 |
|
151 |
embed = discord.Embed(title="Your Current Bets", color=0x787878)
|
|
|
158 |
|
159 |
view = discord.ui.View()
|
160 |
cancel_select = discord.ui.Select(placeholder="Select a bet to cancel", options=[
|
161 |
+
discord.SelectOption(label=f"Bet {i+1}", value=str(i)) for i in range(len(user_bets[user_id]))
|
162 |
])
|
163 |
view.add_item(cancel_select)
|
164 |
|
|
|
166 |
bet_index = int(cancel_select.values[0])
|
167 |
cancelled_bet = user_bets[user_id].pop(bet_index)
|
168 |
user_cash[user_id] += cancelled_bet['amount']
|
169 |
+
await interaction.response.send_message(f"Bet cancelled. ${cancelled_bet['amount']} has been refunded.")
|
|
|
170 |
|
171 |
cancel_select.callback = cancel_callback
|
172 |
|
173 |
+
await interaction.response.send_message(embed=embed, view=view)
|