From 509aba4941068ef6626935928e6908f4572cd506 Mon Sep 17 00:00:00 2001 From: DerpDays <34582078+DerpDays@users.noreply.github.com> Date: Sun, 14 Oct 2018 14:39:19 +0100 Subject: Added a error handler! --- bot/cogs/error_handler.py | 51 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 bot/cogs/error_handler.py (limited to 'bot/cogs/error_handler.py') diff --git a/bot/cogs/error_handler.py b/bot/cogs/error_handler.py new file mode 100644 index 00000000..3f228b14 --- /dev/null +++ b/bot/cogs/error_handler.py @@ -0,0 +1,51 @@ +import discord +from discord.ext import commands +import asyncio + +import sys +import traceback +import math + +class CommandErrorHandler: + def __init__(self, bot): + self.bot = bot + + async def on_command_error(self, ctx, error): + if hasattr(ctx.command, 'on_error'): + return + error = getattr(error, 'original', error) + + if isinstance(error, commands.CommandNotFound): + return + + if isinstance(error, commands.UserInputError): + return await ctx.send(':no_entry: The command you specified failed to run because the arguments you provided were invalid.') + + if isinstance(error, commands.CommandOnCooldown): + return await ctx.send("This command is on cooldown, please retry in {}s.".format(math.ceil(error.retry_after))) + + if isinstance(error, commands.DisabledCommand): + return await ctx.send(':no_entry: This command has been disabled.') + + if isinstance(error, commands.NoPrivateMessage): + try: + return await ctx.author.send(':no_entry: This command can only be used inside a server.') + except: + pass + + if isinstance(error, commands.BadArgument): + if ctx.command.qualified_name == 'tag list': + return await ctx.send('I could not find that member. Please try again.') + else: + return await ctx.send("The argument you provided was invalid.") + + if isinstance(error, commands.CheckFailure): + await ctx.send(":no_entry: You do not have permission to use this command.") + return + + 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(CommandErrorHandler(bot)) -- cgit v1.2.3