feat: error handler

This commit is contained in:
insert 2025-04-26 10:19:46 -04:00
parent b330848ebe
commit 4ccc90fda8
Signed by: insert
GPG key ID: A70775C389ACF105
2 changed files with 100 additions and 56 deletions

33
cogs/error.py Normal file
View file

@ -0,0 +1,33 @@
import nextcord
from nextcord.ext import commands, application_checks
from nextcord import TextInputStyle
import traceback
import random
import aiosqlite as sqlite3
import aiohttp
from urllib.parse import urlparse
import os
import sys
errormsg = ["Well maybe this won't happen next time"]
class error(commands.Cog):
def __init__(self, bot: commands.Bot):
self.bot = bot
@commands.Cog.listener('on_application_command_error')
async def errorhandler(self, interaction: nextcord.Interaction, error: nextcord.DiscordException):
error = traceback.format_exception(type(error), error, error.__traceback__)
random.shuffle(errormsg)
strerror = "".join(error)
print(strerror, file=sys.stderr)
message = f"{errormsg[0]}\n```py\n{strerror[-1800:]}\n```\n Contact <@{self.bot.owner_id}> if the error persists"
try:
await interaction.response.send_message(message, ephemeral=True)
except:
await interaction.followup.send(message, ephemeral=True)
def setup(bot: commands.Bot):
bot.add_cog(error(bot))

View file

@ -188,12 +188,13 @@ class liveupdate(commands.Cog):
],
force_global=True,
)
async def liveupdatedelete(self, interaction: nextcord.Interaction, messageid: float):
async def liveupdatedelete(self, interaction: nextcord.Interaction, messageid: str):
await interaction.response.defer(ephemeral=True)
await self.bot.db.execute(f"""
DELETE from liveupdate WHERE messageid = {messageid}
""")
DELETE from liveupdate WHERE messageid = ?
""", (int(messageid),))
await self.bot.db.commit()
await interaction.response.send_message("stopped updates for message") #TODO This will be a button
await interaction.edit_original_message(content="stopped updates for message") #TODO This will be a button
@tasks.loop(seconds=10.0)
async def updatelivegames(self):
@ -245,66 +246,76 @@ class liveupdate(commands.Cog):
@tasks.loop(seconds=30.0)
async def checkspotlightsubscriptions(self):
await self.bot.wait_until_ready()
print("refreshing spotlight subscriptions")
game = requests.get("https://mmolb.com/api/spotlight").json()
gameid = game["game_id"]
res = await self.bot.db.execute("SELECT serverid,channelid FROM spotlightsubscriptions")
res = await res.fetchall()
print(res)
for [serverid,channelid] in res:
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)
if await check.fetchone() is None:
print("True")
data = requests.get(f"https://mmolb.com/api/game/{gameid}").json()
channel = self.bot.get_channel(channelid)
if data["State"] == "Complete":
continue
try:
await self.bot.wait_until_ready()
print("refreshing spotlight subscriptions")
game = requests.get("https://mmolb.com/api/spotlight").json()
gameid = game["game_id"]
res = await self.bot.db.execute("SELECT serverid,channelid FROM spotlightsubscriptions")
res = await res.fetchall()
print(res)
for [serverid,channelid] in res:
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()
if test is None: #no idea why it has to have two checks
message = await channel.send(content=f"{data["AwayTeamName"]} {data["AwayTeamEmoji"]} **{data["EventLog"][-1]["away_score"]}** vs {data["HomeTeamName"]} {data["HomeTeamEmoji"]} **{data["EventLog"][-1]["home_score"]}**")
await self.bot.db.execute(f"""
INSERT INTO liveupdate VALUES
({channel.guild.id}, {self.bot.application_id}, {channelid}, {message.id}, "{gameid}", {len(data["EventLog"])})
""")
await self.bot.db.commit()
else:
print("false")
print(test)
if test is None:
print("True")
data = requests.get(f"https://mmolb.com/api/game/{gameid}").json()
channel = self.bot.get_channel(channelid)
if data["State"] == "Complete":
continue
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()
if test is None: #no idea why it has to have two checks
message = await channel.send(content=f"{data["AwayTeamName"]} {data["AwayTeamEmoji"]} **{data["EventLog"][-1]["away_score"]}** vs {data["HomeTeamName"]} {data["HomeTeamEmoji"]} **{data["EventLog"][-1]["home_score"]}**")
await self.bot.db.execute(f"""
INSERT INTO liveupdate VALUES
({channel.guild.id}, {self.bot.application_id}, {channelid}, {message.id}, "{gameid}", {len(data["EventLog"])})
""")
await self.bot.db.commit()
else:
print("false")
except Exception as e:
#I know this is bad practice but these loops must be running
print(e)
return
@tasks.loop(seconds=35.0)
async def checkteamsubscriptions(self):
print("refreshing team subscriptions")
await self.bot.wait_until_ready()
res = await self.bot.db.execute("SELECT serverid,channelid,teamid FROM teamsubscriptions")
res = await res.fetchall()
print(res)
for [serverid,channelid,teamid] in res:
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))
test = await check.fetchone()
print(test)
if await check.fetchone() is None:
print("True")
data = requests.get(f"https://mmolb.com/api/game/{gameid}").json()
channel = self.bot.get_channel(channelid)
if data["State"] == "Complete":
continue
try:
print("refreshing team subscriptions")
await self.bot.wait_until_ready()
res = await self.bot.db.execute("SELECT serverid,channelid,teamid FROM teamsubscriptions")
res = await res.fetchall()
print(res)
for [serverid,channelid,teamid] in res:
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))
test = await check.fetchone()
if test is None: #no idea why it has to have two checks
message = await channel.send(content=f"{data["AwayTeamName"]} {data["AwayTeamEmoji"]} **{data["EventLog"][-1]["away_score"]}** vs {data["HomeTeamName"]} {data["HomeTeamEmoji"]} **{data["EventLog"][-1]["home_score"]}**")
await self.bot.db.execute(f"""
INSERT INTO liveupdate VALUES
({channel.guild.id}, {self.bot.application_id}, {channelid}, {message.id}, "{gameid}", {len(data["EventLog"])})
""")
await self.bot.db.commit()
else:
print("false")
print(test)
if test is None:
print("True")
data = requests.get(f"https://mmolb.com/api/game/{gameid}").json()
channel = self.bot.get_channel(channelid)
if data["State"] == "Complete":
continue
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()
if test is None: #no idea why it has to have two checks
message = await channel.send(content=f"{data["AwayTeamName"]} {data["AwayTeamEmoji"]} **{data["EventLog"][-1]["away_score"]}** vs {data["HomeTeamName"]} {data["HomeTeamEmoji"]} **{data["EventLog"][-1]["home_score"]}**")
await self.bot.db.execute(f"""
INSERT INTO liveupdate VALUES
({channel.guild.id}, {self.bot.application_id}, {channelid}, {message.id}, "{gameid}", {len(data["EventLog"])})
""")
await self.bot.db.commit()
else:
print("false")
except Exception as e:
#I know this is bad practice but these loops must be running
print(e)
return
def setup(bot: commands.Bot):
bot.add_cog(liveupdate(bot))