|
| 1 | +import logging |
| 2 | +from discord.ext import commands |
| 3 | + |
| 4 | +log = logging.getLogger('position5.error_handler') |
| 5 | + |
| 6 | + |
| 7 | +class ErrorHandler(commands.Cog): |
| 8 | + def __init__(self, bot): |
| 9 | + self.bot = bot |
| 10 | + |
| 11 | + @commands.Cog.listener() |
| 12 | + async def on_command_error(self, ctx, error): |
| 13 | + """The event triggered when an error is raised while invoking a command. |
| 14 | + Parameters |
| 15 | + ------------ |
| 16 | + ctx: commands.Context |
| 17 | + The context used for command invocation. |
| 18 | + error: commands.CommandError |
| 19 | + The Exception raised. |
| 20 | + """ |
| 21 | + |
| 22 | + # This prevents any commands with local handlers being handled here in on_command_error. |
| 23 | + if hasattr(ctx.command, 'on_error'): |
| 24 | + return |
| 25 | + |
| 26 | + # Allows us to check for original exceptions raised and sent to CommandInvokeError. |
| 27 | + # If nothing is found. We keep the exception passed to on_command_error. |
| 28 | + error = getattr(error, 'original', error) |
| 29 | + |
| 30 | + if isinstance(error, commands.CommandNotFound): |
| 31 | + log.info( |
| 32 | + 'Command Not Found | Message: %s | Author: %s', |
| 33 | + ctx.message.content, |
| 34 | + ctx.author.name, |
| 35 | + ) |
| 36 | + |
| 37 | + # elif isinstance(error, commands.DisabledCommand): |
| 38 | + # await ctx.send(f'{ctx.command} has been disabled.') |
| 39 | + |
| 40 | + # elif isinstance(error, commands.NoPrivateMessage): |
| 41 | + # try: |
| 42 | + # await ctx.author.send(f'{ctx.command} can not be used in Private Messages.') |
| 43 | + # except discord.HTTPException: |
| 44 | + # pass |
| 45 | + |
| 46 | + # elif isinstance(error, commands.BadArgument): |
| 47 | + # if ctx.command.qualified_name == 'tag list': |
| 48 | + # await ctx.send('I could not find that member. Please try again.') |
| 49 | + |
| 50 | + else: |
| 51 | + log.info('Exception in command %s', ctx.command) |
| 52 | + log.error('Type %s | Error: %s', type(error), error) |
| 53 | + |
| 54 | + |
| 55 | +def setup(bot): |
| 56 | + bot.add_cog(ErrorHandler(bot)) |
0 commit comments