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, 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""" 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 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) @tasks.loop(seconds=10.0)
async def updatelivegames(self): async def updatelivegames(self):
@ -245,6 +246,7 @@ class liveupdate(commands.Cog):
@tasks.loop(seconds=30.0) @tasks.loop(seconds=30.0)
async def checkspotlightsubscriptions(self): async def checkspotlightsubscriptions(self):
try:
await self.bot.wait_until_ready() await self.bot.wait_until_ready()
print("refreshing spotlight subscriptions") print("refreshing spotlight subscriptions")
game = requests.get("https://mmolb.com/api/spotlight").json() game = requests.get("https://mmolb.com/api/spotlight").json()
@ -256,7 +258,7 @@ class liveupdate(commands.Cog):
check = await self.bot.db.execute("SELECT serverid,userid,channelid,messageid,gameid,offset FROM liveupdate WHERE channelid = ? AND gameid = ?", (channelid,gameid)) 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() test = await check.fetchone()
print(test) print(test)
if await check.fetchone() is None: if test is None:
print("True") print("True")
data = requests.get(f"https://mmolb.com/api/game/{gameid}").json() data = requests.get(f"https://mmolb.com/api/game/{gameid}").json()
channel = self.bot.get_channel(channelid) channel = self.bot.get_channel(channelid)
@ -273,10 +275,15 @@ class liveupdate(commands.Cog):
await self.bot.db.commit() await self.bot.db.commit()
else: else:
print("false") 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) @tasks.loop(seconds=35.0)
async def checkteamsubscriptions(self): async def checkteamsubscriptions(self):
try:
print("refreshing team subscriptions") print("refreshing team subscriptions")
await self.bot.wait_until_ready() await self.bot.wait_until_ready()
res = await self.bot.db.execute("SELECT serverid,channelid,teamid FROM teamsubscriptions") res = await self.bot.db.execute("SELECT serverid,channelid,teamid FROM teamsubscriptions")
@ -288,7 +295,7 @@ class liveupdate(commands.Cog):
check = await self.bot.db.execute("SELECT serverid,userid,channelid,messageid,gameid,offset FROM liveupdate WHERE channelid = ? AND gameid = ?", (channelid,gameid)) 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() test = await check.fetchone()
print(test) print(test)
if await check.fetchone() is None: if test is None:
print("True") print("True")
data = requests.get(f"https://mmolb.com/api/game/{gameid}").json() data = requests.get(f"https://mmolb.com/api/game/{gameid}").json()
channel = self.bot.get_channel(channelid) channel = self.bot.get_channel(channelid)
@ -305,6 +312,10 @@ class liveupdate(commands.Cog):
await self.bot.db.commit() await self.bot.db.commit()
else: else:
print("false") 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): def setup(bot: commands.Bot):
bot.add_cog(liveupdate(bot)) bot.add_cog(liveupdate(bot))