Skip to content

Commit 4a83abf

Browse files
authored
Merge pull request #196 from practical-python-org/feature/Error-Handler
minor: Created a working Error Handler
2 parents d9dfbb8 + 3d326f9 commit 4a83abf

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

src/zorak/cogs/admin/error_handler.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import logging
2+
3+
import discord
4+
from discord.ext import commands
5+
from datetime import datetime
6+
7+
8+
logger = logging.getLogger(__name__)
9+
10+
11+
class error_handler(commands.Cog):
12+
def __init__(self, bot):
13+
self.bot = bot
14+
15+
@commands.Cog.listener()
16+
async def on_application_command_error(self, ctx, error):
17+
18+
# # This is just an interesting way of handling errors PER ERROR.
19+
# # For now, let's just catch all and redirect to logs and channel
20+
# if isinstance(error, commands.CommandOnCooldown):
21+
# await ctx.respond("This command is currently on cooldown!")
22+
# else:
23+
# raise error # Here we raise other errors to ensure they aren't ignored
24+
25+
error_channel = self.bot.get_channel(self.bot.server_settings.log_channel['zorak_log'])
26+
27+
embed = discord.Embed(title=f':red_circle: Zorak error!'
28+
, description=f'{ctx.author} used /{ctx.command} in <#{ctx.channel}>'
29+
, color=discord.Color.dark_red()
30+
, timestamp=datetime.utcnow())
31+
embed.add_field(name='Traceback (most recent call last): '
32+
, value=f'{error}')
33+
34+
await error_channel.send(ctx.author.mention)
35+
await error_channel.send(embed=embed)
36+
logger.critical(error)
37+
38+
39+
def setup(bot):
40+
bot.add_cog(error_handler(bot))

0 commit comments

Comments
 (0)