chore: better handle database issues and teams not playing

This commit is contained in:
insert 2025-06-12 17:04:33 -04:00
parent d5c2597593
commit 78ded6d7b7
Signed by: insert
GPG key ID: A70775C389ACF105
2 changed files with 109 additions and 76 deletions

7
bot.py
View file

@ -11,6 +11,8 @@ import os
import logging
import sys
firstrun = False
class Bot(commands.Bot):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
@ -28,9 +30,12 @@ bot = Bot()
@bot.event
async def on_ready():
global firstrun
if not firstrun:
firstrun = True
global db
global cur
db = await sqlite3.connect(os.getenv("DB_PATH"))
db = await sqlite3.connect(os.getenv("DB_PATH"),timeout=30)
cur = await db.cursor()
res = await cur.execute("SELECT name FROM sqlite_master WHERE name='liveupdate'")
if await res.fetchone() is None:

View file

@ -108,11 +108,8 @@ class liveupdate(commands.Cog):
INSERT INTO liveupdate VALUES
({interaction.guild_id}, {interaction.user.id}, {interaction.channel_id}, {message.id}, "{gameid}", {len(data["EventLog"])})
""")
except Exception as e:
except KeyError:
await interaction.edit_original_message(content="This channel is now subscribed to updates")
warning = self.bot.get_channel(1365478368555827270)
await warning.send(e)
print(e)
await self.bot.db.execute(f"""
INSERT INTO teamsubscriptions VALUES
({interaction.guild_id}, {interaction.channel_id}, "{teamid}")
@ -306,6 +303,7 @@ class liveupdate(commands.Cog):
res = await res.fetchall()
print(res)
for [serverid,channelid] in res:
try:
check = await self.bot.db.execute("SELECT serverid,userid,channelid,messageid,gameid,offset FROM liveupdate WHERE channelid = ? AND gameid = ?", (channelid,gameid))
test = await check.fetchone()
print(test)
@ -326,9 +324,25 @@ class liveupdate(commands.Cog):
await self.bot.db.commit()
else:
print("false")
except sqlite3.OperationalError:
await self.bot.db.close()
self.checkspotlightsubscriptions.cancel()
self.checkteamsubscriptions.cancel()
warning = self.bot.get_channel(1365478368555827270)
await warning.send(f"<@{self.bot.owner_id}> database is locked!")
except Exception as e:
warning = self.bot.get_channel(1365478368555827270)
await warning.send(f"Ignoring exception in spotlight check: {e}")
print(e)
continue
except KeyError:
pass
except Exception as e:
#I know this is bad practice but these loops must be running
print(e)
warning = self.bot.get_channel(1365478368555827270)
await warning.send(e)
print(e)
return
@ -341,6 +355,7 @@ class liveupdate(commands.Cog):
res = await res.fetchall()
print(res)
for [serverid,channelid,teamid] in res:
try:
game = requests.get(f"https://mmolb.com/api/game-by-team/{teamid}").json()
gameid = game["game_id"]
check = await self.bot.db.execute("SELECT serverid,userid,channelid,messageid,gameid,offset FROM liveupdate WHERE channelid = ? AND gameid = ?", (channelid,gameid))
@ -361,10 +376,23 @@ class liveupdate(commands.Cog):
({channel.guild.id}, {self.bot.application_id}, {channelid}, {message.id}, "{gameid}", {len(data["EventLog"])})
""")
await self.bot.db.commit()
else:
print("false")
except KeyError:
continue
except sqlite3.OperationalError:
await self.bot.db.close()
self.checkspotlightsubscriptions.cancel()
self.checkteamsubscriptions.cancel()
warning = self.bot.get_channel(1365478368555827270)
await warning.send(f"<@{self.bot.owner_id}> database is locked!")
except Exception as e:
warning = self.bot.get_channel(1365478368555827270)
await warning.send(f"Ignoring exception in {teamid}: {e}")
print(e)
continue
except Exception as e:
#I know this is bad practice but these loops must be running
warning = self.bot.get_channel(1365478368555827270)
await warning.send(e)
print(e)
return