Spaces:
Building
Building
Update sportbet.py
Browse files- sportbet.py +43 -22
sportbet.py
CHANGED
@@ -3,10 +3,28 @@ from discord import app_commands
|
|
3 |
import aiohttp
|
4 |
import asyncio
|
5 |
from datetime import datetime, timezone
|
6 |
-
from cash import user_cash, save_database, load_database
|
7 |
|
|
|
8 |
user_bets = {}
|
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
load_database()
|
11 |
|
12 |
async def fetch_nhl_scores():
|
@@ -102,6 +120,29 @@ class BetModal(discord.ui.Modal, title="Place Your Bet"):
|
|
102 |
|
103 |
await asyncio.sleep(300)
|
104 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
105 |
@app_commands.command(name="sportbet", description="bet on sports game")
|
106 |
async def sportbet(interaction: discord.Interaction):
|
107 |
if interaction.user.id in user_bets and user_bets[interaction.user.id]:
|
@@ -118,29 +159,9 @@ async def sportbet(interaction: discord.Interaction):
|
|
118 |
await interaction.response.send_message("No games for betting.")
|
119 |
return
|
120 |
|
121 |
-
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 |
async def view_current_bets(interaction: discord.Interaction):
|
145 |
user_id = interaction.user.id
|
146 |
if user_id not in user_bets or not user_bets[user_id]:
|
|
|
3 |
import aiohttp
|
4 |
import asyncio
|
5 |
from datetime import datetime, timezone
|
|
|
6 |
|
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:
|
18 |
+
with open("database.txt", "r") as f:
|
19 |
+
for line in f:
|
20 |
+
parts = line.strip().split()
|
21 |
+
if len(parts) == 2 and parts[1].startswith("cash(") and parts[1].endswith(")"):
|
22 |
+
user_id = int(parts[0])
|
23 |
+
cash = int(parts[1][5:-1])
|
24 |
+
user_cash[user_id] = cash
|
25 |
+
except FileNotFoundError:
|
26 |
+
print("No database found. Creating a new one.")
|
27 |
+
|
28 |
load_database()
|
29 |
|
30 |
async def fetch_nhl_scores():
|
|
|
120 |
|
121 |
await asyncio.sleep(300)
|
122 |
|
123 |
+
class SportBetView(discord.ui.View):
|
124 |
+
def __init__(self, upcoming_games):
|
125 |
+
super().__init__()
|
126 |
+
self.upcoming_games = upcoming_games
|
127 |
+
self.add_item(GameSelect(upcoming_games))
|
128 |
+
|
129 |
+
@discord.ui.select(cls=GameSelect)
|
130 |
+
async def on_game_select(self, interaction: discord.Interaction, select: GameSelect):
|
131 |
+
selected_game = next(game for game in self.upcoming_games if f"{game['teams']['away']['abbreviation']}_{game['teams']['home']['abbreviation']}" == select.values[0])
|
132 |
+
|
133 |
+
team_view = discord.ui.View()
|
134 |
+
team_select = TeamSelect(selected_game['teams']['away'], selected_game['teams']['home'])
|
135 |
+
team_view.add_item(team_select)
|
136 |
+
|
137 |
+
@discord.ui.select(cls=TeamSelect)
|
138 |
+
async def on_team_select(interaction: discord.Interaction, select: TeamSelect):
|
139 |
+
selected_team = select.values[0]
|
140 |
+
await interaction.response.send_modal(BetModal(selected_team, interaction.user.id, selected_game))
|
141 |
+
|
142 |
+
team_view.on_team_select = on_team_select
|
143 |
+
|
144 |
+
await interaction.response.edit_message(content="team to bet on:", view=team_view)
|
145 |
+
|
146 |
@app_commands.command(name="sportbet", description="bet on sports game")
|
147 |
async def sportbet(interaction: discord.Interaction):
|
148 |
if interaction.user.id in user_bets and user_bets[interaction.user.id]:
|
|
|
159 |
await interaction.response.send_message("No games for betting.")
|
160 |
return
|
161 |
|
162 |
+
view = SportBetView(upcoming_games)
|
|
|
|
|
|
|
163 |
await interaction.response.send_message("game to bet on:", view=view)
|
164 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
165 |
async def view_current_bets(interaction: discord.Interaction):
|
166 |
user_id = interaction.user.id
|
167 |
if user_id not in user_bets or not user_bets[user_id]:
|