📝 Better functioning error handling

This commit is contained in:
NikolajDanger
2021-04-03 22:35:11 +02:00
parent 0fb7887745
commit 1a2ead8448
2 changed files with 8 additions and 6 deletions

View File

@ -4,6 +4,7 @@ from discord.ext import commands
class EventCog(commands.Cog):
def __init__(self, bot):
self.bot = bot
self.bot.on_error = self.on_error
# Syncs commands, sets the game, and logs when the bot logs in
@commands.Cog.listener()
@ -20,9 +21,8 @@ class EventCog(commands.Cog):
async def on_slash_command_error(self, ctx, error):
await self.bot.errorHandler.on_slash_command_error(ctx, error)
# Logs if an error occurs
@commands.Cog.listener()
async def on_error(self, method):
# Logs if on error occurs
async def on_error(self, method, *args, **kwargs):
await self.bot.errorHandler.on_error(method)
def setup(bot):

View File

@ -1,4 +1,4 @@
import discord, traceback
import discord, traceback, discord_slash
from discord.ext import commands
class EventHandler():
@ -28,9 +28,11 @@ class ErrorHandler():
async def on_slash_command_error(self, ctx, error):
if isinstance(error, commands.CommandNotFound):
await ctx.send("That's not a command (error code 001)")
elif isinstance(error,commands.errors.MissingRequiredArgument):
elif isinstance(error, commands.errors.MissingRequiredArgument):
self.bot.log(f"{error}",str(ctx.channel_id))
await ctx.send("Missing command parameters (error code 002). Try using `!help [command]` to find out how to use the command.")
elif isinstance(error, discord_slash.error.AlreadyResponded):
self.bot.log("Defer failed")
else:
exception = traceback.format_exception(type(error), error, error.__traceback__)
stopAt = "\nThe above exception was the direct cause of the following exception:\n\n"
@ -53,4 +55,4 @@ class ErrorHandler():
exception = exception[:index]
exceptionString = "".join(exception)
self.bot.log([f"exception in /{method}", f"{exceptionString}"], level = 40)
self.bot.log([f"exception in {method}", f"{exceptionString}"], level = 40)