aboutsummaryrefslogtreecommitdiffstats
path: root/bot/cogs/error_handler.py
blob: 3f228b147b8ff2a721276c9c99a0cf54a6ac4dfc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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))