mirror of
https://github.com/insertapp/mmolbbot.git
synced 2025-07-01 22:37:04 +00:00
feat: error handler
This commit is contained in:
parent
b330848ebe
commit
4ccc90fda8
2 changed files with 100 additions and 56 deletions
33
cogs/error.py
Normal file
33
cogs/error.py
Normal 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))
|
|
@ -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,66 +246,76 @@ class liveupdate(commands.Cog):
|
||||||
|
|
||||||
@tasks.loop(seconds=30.0)
|
@tasks.loop(seconds=30.0)
|
||||||
async def checkspotlightsubscriptions(self):
|
async def checkspotlightsubscriptions(self):
|
||||||
await self.bot.wait_until_ready()
|
try:
|
||||||
print("refreshing spotlight subscriptions")
|
await self.bot.wait_until_ready()
|
||||||
game = requests.get("https://mmolb.com/api/spotlight").json()
|
print("refreshing spotlight subscriptions")
|
||||||
gameid = game["game_id"]
|
game = requests.get("https://mmolb.com/api/spotlight").json()
|
||||||
res = await self.bot.db.execute("SELECT serverid,channelid FROM spotlightsubscriptions")
|
gameid = game["game_id"]
|
||||||
res = await res.fetchall()
|
res = await self.bot.db.execute("SELECT serverid,channelid FROM spotlightsubscriptions")
|
||||||
print(res)
|
res = await res.fetchall()
|
||||||
for [serverid,channelid] in res:
|
print(res)
|
||||||
check = await self.bot.db.execute("SELECT serverid,userid,channelid,messageid,gameid,offset FROM liveupdate WHERE channelid = ? AND gameid = ?", (channelid,gameid))
|
for [serverid,channelid] in res:
|
||||||
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
|
|
||||||
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()
|
||||||
if test is None: #no idea why it has to have two checks
|
print(test)
|
||||||
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"]}**")
|
if test is None:
|
||||||
await self.bot.db.execute(f"""
|
print("True")
|
||||||
INSERT INTO liveupdate VALUES
|
data = requests.get(f"https://mmolb.com/api/game/{gameid}").json()
|
||||||
({channel.guild.id}, {self.bot.application_id}, {channelid}, {message.id}, "{gameid}", {len(data["EventLog"])})
|
channel = self.bot.get_channel(channelid)
|
||||||
""")
|
if data["State"] == "Complete":
|
||||||
await self.bot.db.commit()
|
continue
|
||||||
else:
|
check = await self.bot.db.execute("SELECT serverid,userid,channelid,messageid,gameid,offset FROM liveupdate WHERE channelid = ? AND gameid = ?", (channelid,gameid))
|
||||||
print("false")
|
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)
|
@tasks.loop(seconds=35.0)
|
||||||
async def checkteamsubscriptions(self):
|
async def checkteamsubscriptions(self):
|
||||||
print("refreshing team subscriptions")
|
try:
|
||||||
await self.bot.wait_until_ready()
|
print("refreshing team subscriptions")
|
||||||
res = await self.bot.db.execute("SELECT serverid,channelid,teamid FROM teamsubscriptions")
|
await self.bot.wait_until_ready()
|
||||||
res = await res.fetchall()
|
res = await self.bot.db.execute("SELECT serverid,channelid,teamid FROM teamsubscriptions")
|
||||||
print(res)
|
res = await res.fetchall()
|
||||||
for [serverid,channelid,teamid] in res:
|
print(res)
|
||||||
game = requests.get(f"https://mmolb.com/api/game-by-team/{teamid}").json()
|
for [serverid,channelid,teamid] in res:
|
||||||
gameid = game["game_id"]
|
game = requests.get(f"https://mmolb.com/api/game-by-team/{teamid}").json()
|
||||||
check = await self.bot.db.execute("SELECT serverid,userid,channelid,messageid,gameid,offset FROM liveupdate WHERE channelid = ? AND gameid = ?", (channelid,gameid))
|
gameid = game["game_id"]
|
||||||
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
|
|
||||||
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()
|
||||||
if test is None: #no idea why it has to have two checks
|
print(test)
|
||||||
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"]}**")
|
if test is None:
|
||||||
await self.bot.db.execute(f"""
|
print("True")
|
||||||
INSERT INTO liveupdate VALUES
|
data = requests.get(f"https://mmolb.com/api/game/{gameid}").json()
|
||||||
({channel.guild.id}, {self.bot.application_id}, {channelid}, {message.id}, "{gameid}", {len(data["EventLog"])})
|
channel = self.bot.get_channel(channelid)
|
||||||
""")
|
if data["State"] == "Complete":
|
||||||
await self.bot.db.commit()
|
continue
|
||||||
else:
|
check = await self.bot.db.execute("SELECT serverid,userid,channelid,messageid,gameid,offset FROM liveupdate WHERE channelid = ? AND gameid = ?", (channelid,gameid))
|
||||||
print("false")
|
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):
|
def setup(bot: commands.Bot):
|
||||||
bot.add_cog(liveupdate(bot))
|
bot.add_cog(liveupdate(bot))
|
Loading…
Reference in a new issue