Spaces:
Building
Building
Update sportbet.py
Browse files- sportbet.py +11 -26
sportbet.py
CHANGED
@@ -3,25 +3,10 @@ from discord import app_commands
|
|
3 |
import aiohttp
|
4 |
import asyncio
|
5 |
from datetime import datetime, timezone
|
|
|
6 |
|
7 |
-
user_cash = {}
|
8 |
user_bets = {}
|
9 |
|
10 |
-
def load_database():
|
11 |
-
global user_cash
|
12 |
-
try:
|
13 |
-
with open("database.txt", "r") as f:
|
14 |
-
for line in f:
|
15 |
-
parts = line.strip().split()
|
16 |
-
if len(parts) == 2 and parts[1].startswith("cash(") and parts[1].endswith(")"):
|
17 |
-
user_id = int(parts[0])
|
18 |
-
cash = int(parts[1][5:-1])
|
19 |
-
user_cash[user_id] = cash
|
20 |
-
except FileNotFoundError:
|
21 |
-
print("No database found. Creating a new one.")
|
22 |
-
|
23 |
-
load_database()
|
24 |
-
|
25 |
async def fetch_nhl_scores():
|
26 |
async with aiohttp.ClientSession() as session:
|
27 |
async with session.get("https://nhl-score-api.herokuapp.com/api/scores/latest") as response:
|
@@ -61,7 +46,7 @@ class BetModal(discord.ui.Modal, title="Place Your Bet"):
|
|
61 |
if bet_amount <= 0:
|
62 |
raise ValueError("Bet more than 0 dollars")
|
63 |
if bet_amount > user_cash.get(self.user_id, 0):
|
64 |
-
raise ValueError("
|
65 |
|
66 |
user_cash[self.user_id] -= bet_amount
|
67 |
await interaction.response.send_message(f"Bet placed on {self.team} for ${bet_amount}")
|
@@ -100,29 +85,29 @@ class BetModal(discord.ui.Modal, title="Place Your Bet"):
|
|
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"
|
104 |
else:
|
105 |
-
await interaction.user.send(f"Sorry, your team lost
|
106 |
|
107 |
user_bets[self.user_id] = [bet for bet in user_bets[self.user_id] if bet['game_data'] != self.game_data]
|
108 |
break
|
109 |
|
110 |
await asyncio.sleep(300)
|
111 |
|
112 |
-
@app_commands.command(name="sportbet", description="
|
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 |
|
117 |
if not upcoming_games:
|
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])
|
@@ -131,7 +116,7 @@ async def sportbet(interaction: discord.Interaction):
|
|
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]
|
@@ -141,11 +126,11 @@ async def sportbet(interaction: discord.Interaction):
|
|
141 |
|
142 |
game_select.callback = game_callback
|
143 |
|
144 |
-
@app_commands.command(name="currentbets", description="
|
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)
|
@@ -166,7 +151,7 @@ async def currentbets(interaction: discord.Interaction):
|
|
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 |
|
|
|
3 |
import aiohttp
|
4 |
import asyncio
|
5 |
from datetime import datetime, timezone
|
6 |
+
from cash import user_cash
|
7 |
|
|
|
8 |
user_bets = {}
|
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
async def fetch_nhl_scores():
|
11 |
async with aiohttp.ClientSession() as session:
|
12 |
async with session.get("https://nhl-score-api.herokuapp.com/api/scores/latest") as response:
|
|
|
46 |
if bet_amount <= 0:
|
47 |
raise ValueError("Bet more than 0 dollars")
|
48 |
if bet_amount > user_cash.get(self.user_id, 0):
|
49 |
+
raise ValueError("Insufficient funds")
|
50 |
|
51 |
user_cash[self.user_id] -= bet_amount
|
52 |
await interaction.response.send_message(f"Bet placed on {self.team} for ${bet_amount}")
|
|
|
85 |
if game['teams'][winner]['abbreviation'] == self.team:
|
86 |
winnings = bet_amount * 2
|
87 |
user_cash[self.user_id] += winnings
|
88 |
+
await interaction.user.send(f"Congratulations! Your team won. You won ${winnings}!")
|
89 |
else:
|
90 |
+
await interaction.user.send(f"Sorry, your team lost. Better luck next time!")
|
91 |
|
92 |
user_bets[self.user_id] = [bet for bet in user_bets[self.user_id] if bet['game_data'] != self.game_data]
|
93 |
break
|
94 |
|
95 |
await asyncio.sleep(300)
|
96 |
|
97 |
+
@app_commands.command(name="sportbet", description="Bet on sports games")
|
98 |
async def sportbet(interaction: discord.Interaction):
|
99 |
scores = await fetch_nhl_scores()
|
100 |
upcoming_games = [game for game in scores['games'] if game['status']['state'] == 'PREVIEW']
|
101 |
|
102 |
if not upcoming_games:
|
103 |
+
await interaction.response.send_message("No upcoming games available for betting.")
|
104 |
return
|
105 |
|
106 |
view = discord.ui.View()
|
107 |
game_select = GameSelect(upcoming_games)
|
108 |
view.add_item(game_select)
|
109 |
|
110 |
+
await interaction.response.send_message("Select a game to bet on:", view=view)
|
111 |
|
112 |
async def game_callback(interaction: discord.Interaction):
|
113 |
selected_game = next(game for game in upcoming_games if f"{game['teams']['away']['abbreviation']}_{game['teams']['home']['abbreviation']}" == game_select.values[0])
|
|
|
116 |
team_select = TeamSelect(selected_game['teams']['away'], selected_game['teams']['home'])
|
117 |
team_view.add_item(team_select)
|
118 |
|
119 |
+
await interaction.response.edit_message(content="Select a team to bet on:", view=team_view)
|
120 |
|
121 |
async def team_callback(interaction: discord.Interaction):
|
122 |
selected_team = team_select.values[0]
|
|
|
126 |
|
127 |
game_select.callback = game_callback
|
128 |
|
129 |
+
@app_commands.command(name="currentbets", description="View your current bets")
|
130 |
async def currentbets(interaction: discord.Interaction):
|
131 |
user_id = interaction.user.id
|
132 |
if user_id not in user_bets or not user_bets[user_id]:
|
133 |
+
await interaction.response.send_message("You have no active bets.")
|
134 |
return
|
135 |
|
136 |
embed = discord.Embed(title="Your Current Bets", color=0x787878)
|
|
|
151 |
bet_index = int(cancel_select.values[0])
|
152 |
cancelled_bet = user_bets[user_id].pop(bet_index)
|
153 |
user_cash[user_id] += cancelled_bet['amount']
|
154 |
+
await interaction.response.send_message(f"Bet cancelled. ${cancelled_bet['amount']} has been refunded to your balance.")
|
155 |
|
156 |
cancel_select.callback = cancel_callback
|
157 |
|