diff options
| author | 2018-11-20 09:02:35 +0100 | |
|---|---|---|
| committer | 2018-11-20 09:02:35 +0100 | |
| commit | b9581ae1467bab49bff06a20cb6eae1f8e7a6a48 (patch) | |
| tree | d0b268c042b3755f100d9c502daa91b4135f3fa8 /bot/cogs | |
| parent | Add badges to README (diff) | |
| parent | Litterally got no idea why it failed checks last time (diff) | |
Merge branch 'master' of https://github.com/DerpDays/hacktoberbot into DerpDays-master
Diffstat (limited to 'bot/cogs')
| -rw-r--r-- | bot/cogs/error_handler.py | 105 | 
1 files changed, 105 insertions, 0 deletions
| diff --git a/bot/cogs/error_handler.py b/bot/cogs/error_handler.py new file mode 100644 index 00000000..9f0da2e0 --- /dev/null +++ b/bot/cogs/error_handler.py @@ -0,0 +1,105 @@ +import sys
 +import traceback
 +import math
 +import logging
 +from discord.ext import commands
 +
 +
 +class CommandErrorHandler:
 +    """A error handler for the PythonDiscord server!"""
 +
 +    def __init__(self, bot):
 +        self.bot = bot
 +
 +    async def on_command_error(self, ctx, error):
 +        """Activates when a command opens an error"""
 +
 +        if hasattr(ctx.command, 'on_error'):
 +            return logging.debug(
 +                "A command error occured but " +
 +                "the command had it's own error handler"
 +            )
 +        error = getattr(error, 'original', error)
 +        if isinstance(error, commands.CommandNotFound):
 +            return logging.debug(
 +                f"{ctx.author} called '{ctx.message.content}' " +
 +                "but no command was found"
 +            )
 +        if isinstance(error, commands.UserInputError):
 +            logging.debug(
 +                f"{ctx.author} called the command '{ctx.command}' " +
 +                "but entered invalid input!"
 +            )
 +            return await ctx.send(
 +                ":no_entry: The command you specified failed to run." +
 +                "This is because the arguments you provided were invalid."
 +            )
 +        if isinstance(error, commands.CommandOnCooldown):
 +            logging.debug(
 +                f"{ctx.author} called the command '{ctx.command}' " +
 +                "but they were on cooldown!"
 +            )
 +            return await ctx.send(
 +                "This command is on cooldown," +
 +                " please retry in {}s.".format(math.ceil(error.retry_after))
 +            )
 +        if isinstance(error, commands.DisabledCommand):
 +            logging.debug(
 +                f"{ctx.author} called the command '{ctx.command}' " +
 +                "but the command was disabled!"
 +            )
 +            return await ctx.send(
 +                ":no_entry: This command has been disabled."
 +            )
 +        if isinstance(error, commands.NoPrivateMessage):
 +            logging.debug(
 +                f"{ctx.author} called the command '{ctx.command}' " +
 +                "in a private message however the command was guild only!"
 +            )
 +            return await ctx.author.send(
 +                ":no_entry: This command can only be used inside a server."
 +            )
 +        if isinstance(error, commands.BadArgument):
 +            if ctx.command.qualified_name == 'tag list':
 +                logging.debug(
 +                    f"{ctx.author} called the command '{ctx.command}' " +
 +                    "but entered an invalid user!"
 +                )
 +                return await ctx.send(
 +                    "I could not find that member. Please try again."
 +                )
 +            else:
 +                logging.debug(
 +                    f"{ctx.author} called the command '{ctx.command}' " +
 +                    "but entered a bad argument!"
 +                )
 +                return await ctx.send(
 +                    "The argument you provided was invalid."
 +                )
 +        if isinstance(error, commands.CheckFailure):
 +            logging.debug(
 +                f"{ctx.author} called the command '{ctx.command}' " +
 +                "but the checks failed!"
 +            )
 +            return await ctx.send(
 +                ":no_entry: You are not authorized to use this command."
 +            )
 +        print(
 +            "Ignoring exception in command {}:".format(ctx.command),
 +            file=sys.stderr
 +        )
 +        logging.warning(
 +            f"{ctx.author} called the command '{ctx.command}' " +
 +            "however the command failed to run with the error:" +
 +            f"-------------\n{error}"
 +        )
 +        traceback.print_exception(
 +            type(error),
 +            error,
 +            error.__traceback__,
 +            file=sys.stderr
 +        )
 +
 +
 +def setup(bot):
 +    bot.add_cog(CommandErrorHandler(bot))
 | 
