From 4ccc90fda82cb95b8c65ad792607e258833bc1e9 Mon Sep 17 00:00:00 2001 From: insert Date: Sat, 26 Apr 2025 10:19:46 -0400 Subject: [PATCH] feat: error handler --- cogs/error.py | 33 ++++++++++++ cogs/liveupdate.py | 123 ++++++++++++++++++++++++--------------------- 2 files changed, 100 insertions(+), 56 deletions(-) create mode 100644 cogs/error.py diff --git a/cogs/error.py b/cogs/error.py new file mode 100644 index 0000000..121de0e --- /dev/null +++ b/cogs/error.py @@ -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)) \ No newline at end of file diff --git a/cogs/liveupdate.py b/cogs/liveupdate.py index 44508a9..b29015d 100644 --- a/cogs/liveupdate.py +++ b/cogs/liveupdate.py @@ -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)) \ No newline at end of file