-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.py
83 lines (63 loc) · 2.72 KB
/
errors.py
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import discord
import traceback
import sys
from discord.ext import commands
class CommandErrorHandler(commands.Cog):
def __init__(self, bot):
self.bot = bot
@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.
"""
# This prevents any commands with local handlers being handled here in on_command_error.
if hasattr(ctx.command, "on_error"):
return
cog = ctx.cog
if cog:
if cog._get_overridden_method(cog.cog_command_error) is not None:
return
ignored = (commands.CommandNotFound,)
sending_err_errors = (commands.BadArgument, commands.ArgumentParsingError)
error = getattr(error, "original", error)
if isinstance(error, ignored):
return
elif 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.PrivateMessageOnly):
await ctx.send("This command can only be used in Private Messages.")
elif isinstance(error, sending_err_errors):
await ctx.send(embed=discord.Embed(description=error))
elif isinstance(error, commands.MissingPermissions):
perms = ", ".join(
f"`{perm.replace('_', ' ').title()}`" for perm in error.missing_perms
)
await ctx.send(f"You're missing the permissions: {perms}")
elif isinstance(error, commands.BotMissingPermissions):
perms = ", ".join(
f"`{perm.replace('_', ' ').title()}`" for perm in error.missing_perms
)
await ctx.send(f"I'm missing the permissions: {perms}")
elif isinstance(error, commands.MaxConcurrencyReached):
await ctx.send(
f"`{ctx.command.qualified_name}` can only be "
f"used {error.number} command(s) at a time under {str(error.per)}"
)
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))