Skip to content

Commit 1eb61c7

Browse files
committed
Add a command error handler
1 parent 895e421 commit 1eb61c7

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

app/bot.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ async def load_cogs(self):
1616
await self.load_extension("app.commands.kms")
1717
await self.load_extension("app.commands.link")
1818
await self.load_extension("app.commands.bancho")
19+
await self.load_extension("app.commands.errors")
1920
await self.tree.sync()
2021

2122
def run():

app/commands/errors.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
from discord.ext.commands import *
3+
from app.cog import BaseCog
4+
5+
class ErrorHandler(BaseCog):
6+
@Cog.listener()
7+
async def on_command_error(self, ctx: Context, error: CommandError):
8+
if hasattr(ctx.command, "on_error"):
9+
# Ignore errors that already have a local handler
10+
return
11+
12+
if isinstance(error, CommandNotFound):
13+
return
14+
15+
elif isinstance(error, MissingRequiredArgument):
16+
return await ctx.send(f"Missing argument: `{error.param.name}`")
17+
18+
elif isinstance(error, MissingPermissions):
19+
return await ctx.send("You don't have permission to use this command.")
20+
21+
elif isinstance(error, CommandOnCooldown):
22+
return await ctx.send(
23+
f"This command is on cooldown. Try again in "
24+
f"{error.retry_after:.1f} seconds."
25+
)
26+
27+
# Log unexpected errors
28+
self.logger.error(f'Unexpected error: {error}', exc_info=True)
29+
await ctx.send("An unexpected error occurred.")
30+
31+
async def setup(bot: Bot):
32+
await bot.add_cog(ErrorHandler())

0 commit comments

Comments
 (0)