diff options
author | aninternettroll <lucafulger@gmail.com> | 2020-09-27 10:50:50 +0000 |
---|---|---|
committer | aninternettroll <lucafulger@gmail.com> | 2020-09-27 10:50:50 +0000 |
commit | d439a851657beea76516d76b129ad2002bce9e55 (patch) | |
tree | 925946523588ce9d991ea5ba27f95c37ad3426a0 /cogs/errorhandler.py | |
parent | 74cc1bcbab8bf54d4d6be420c87a82e06a707b3a (diff) | |
download | steve-bot-d439a851657beea76516d76b129ad2002bce9e55.tar steve-bot-d439a851657beea76516d76b129ad2002bce9e55.tar.gz steve-bot-d439a851657beea76516d76b129ad2002bce9e55.tar.bz2 steve-bot-d439a851657beea76516d76b129ad2002bce9e55.tar.lz steve-bot-d439a851657beea76516d76b129ad2002bce9e55.tar.xz steve-bot-d439a851657beea76516d76b129ad2002bce9e55.tar.zst steve-bot-d439a851657beea76516d76b129ad2002bce9e55.zip |
Better Error Handler
Diffstat (limited to 'cogs/errorhandler.py')
-rw-r--r-- | cogs/errorhandler.py | 75 |
1 files changed, 65 insertions, 10 deletions
diff --git a/cogs/errorhandler.py b/cogs/errorhandler.py index 8533a61..7b7c0ec 100644 --- a/cogs/errorhandler.py +++ b/cogs/errorhandler.py @@ -1,20 +1,75 @@ +import traceback +import sys from discord.ext import commands class Errorhandler(commands.Cog): - def __init__(self, bot): - self.bot = bot + def __init__(self, bot): + self.bot = bot - @commands.Cog.listener() - async def on_command_error(self, ctx, error): - if isinstance(error, commands.CommandNotFound): - return + @commands.Cog.listener() + async def on_command_error(self, ctx, error): + """The event triggered when an error is raised while invoking a command. + Parameters + ------------ + ctx: commands.Context + The context used for command invocation. + error: commands.CommandError + The Exception raised. + """ - if isinstance(error, commands.CommandOnCooldown): - return await ctx.send(f'{ctx.author.mention}, you have to wait {round(error.retry_after, 2)} seconds before using this again') + # This prevents any commands with local handlers being handled here in on_command_error. + if hasattr(ctx.command, "on_error"): + return - raise error + # This prevents any cogs with an overwritten cog_command_error being handled here. + cog = ctx.cog + if cog: + if cog._get_overridden_method(cog.cog_command_error) is not None: + return + + ignored = (commands.CommandNotFound,) + + # Allows us to check for original exceptions raised and sent to CommandInvokeError. + # If nothing is found. We keep the exception passed to on_command_error. + error = getattr(error, "original", error) + + # Anything in ignored will return and prevent anything happening. + if isinstance(error, ignored): + return + + if isinstance(error, commands.DisabledCommand): + await ctx.send(f"{ctx.command} has been disabled.") + + elif isinstance(error, commands.NoPrivateMessage): + try: + await ctx.author.send( + f"{ctx.command} can not be used in Private Messages." + ) + except discord.HTTPException: + pass + + elif isinstance(error, commands.CommandOnCooldown): + await ctx.send( + f"{ctx.author.mention}, you have to wait {round(error.retry_after, 2)} seconds before using this again" + ) + + # For this error example we check to see where it came from... + elif isinstance(error, commands.BadArgument): + if ( + ctx.command.qualified_name == "tag list" + ): # Check if the command being invoked is 'tag list' + await ctx.send("I could not find that member. Please try again.") + + else: + # All other Errors not returned come here. And we can just print the default TraceBack. + print( + "Ignoring exception in command {}:".format(ctx.command), file=sys.stderr + ) + traceback.print_exception( + type(error), error, error.__traceback__, file=sys.stderr + ) def setup(bot): - bot.add_cog(Errorhandler(bot)) + bot.add_cog(Errorhandler(bot)) |