Skip to content

Commit 9a42aa6

Browse files
+ Added Error manager
1 parent ffc119c commit 9a42aa6

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

src/cogs/error_manager.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import math
2+
import traceback
3+
import discord
4+
from discord.ext import commands
5+
6+
from src.utils import Error, Embeds
7+
from src.config import Config
8+
9+
10+
class ErrorManager(commands.Cog):
11+
"""hidden"""
12+
def __init__(self, client):
13+
self.client = client
14+
15+
@commands.Cog.listener()
16+
async def on_ready(self):
17+
print('Error manager online')
18+
19+
@commands.Cog.listener()
20+
async def on_command_error(self, ctx, error):
21+
prefix = Config.COMMAND_PREFIX
22+
embed = Embeds()
23+
embed.color = discord.Color.dark_red()
24+
25+
if isinstance(error, Error):
26+
embed.description = error.description
27+
for field in error.fields.values():
28+
field['inline'] = ('inline' in field) and field['inline'] or False
29+
embed.add_field(**field)
30+
31+
elif isinstance(error, commands.CommandNotFound):
32+
embed.description= f'Command not found! `{prefix}help` for a list of commands!'
33+
34+
elif isinstance(error, commands.BotMissingPermissions):
35+
missing = [perm.replace('_', ' ').replace('guild', 'server').title() for perm in error.missing_perms]
36+
if len(missing) > 2:
37+
fmt = '{}, and {}'.format("**, **".join(missing[:-1]), missing[-1])
38+
else:
39+
fmt = ' and '.join(missing)
40+
embed.description = 'I need the **{}** permission(s) to run this command.'.format(fmt)
41+
42+
elif isinstance(error, commands.DisabledCommand):
43+
embed.description = 'The command has been disabled!'
44+
45+
elif isinstance(error, commands.CommandOnCooldown):
46+
embed.description = 'This command is on cooldown, please retry in {}s.'.format(math.ceil(error.retry_after))
47+
48+
elif isinstance(error, commands.MissingPermissions):
49+
missing = [perm.replace('_', ' ').replace('guild', 'server').title() for perm in error.missing_perms]
50+
if len(missing) > 2:
51+
fmt = '{}, and {}'.format("**, **".join(missing[:-1]), missing[-1])
52+
else:
53+
fmt = ' and '.join(missing)
54+
embed.description = 'You need the **{}** permission(s) to use this command.'.format(fmt)
55+
56+
elif isinstance(error, commands.NoPrivateMessage):
57+
try:
58+
await ctx.author.send('This command cannot be used in direct messages.')
59+
except discord.Forbidden:
60+
pass
61+
62+
elif isinstance(error, commands.CheckFailure):
63+
embed.description = 'You do not have permission to use this command!'
64+
65+
elif isinstance(error, commands.UserInputError):
66+
embed.description = f'Invalid arguments! `{prefix}help` for a list of commands!'
67+
embed.add_field(
68+
name = 'Invalid Arguments',
69+
value = error,
70+
inline = False
71+
)
72+
73+
elif isinstance(error, commands.CommandError): # Custom Error
74+
embed.description = str(error)
75+
76+
await ctx.reply(ctx.author.mention, embed = embed, ephemeral = True)
77+
print(error)
78+
traceback.print_exc()
79+
80+
81+
82+
async def setup(client):
83+
await client.add_cog(ErrorManager(client))

0 commit comments

Comments
 (0)