@bot.event
async def event_command_error(ctx, error):
"""Global error handler for all commands"""
# Ignore commands with local error handlers
if hasattr(ctx.command, 'error_handler'):
return
# Get the original exception if it's wrapped
error = getattr(error, 'original', error)
# Handle specific errors
if isinstance(error, commands.CommandNotFound):
return # Silently ignore
elif isinstance(error, commands.MissingRequiredArgument):
await ctx.send(f'Missing required argument: {error.param.name}')
elif isinstance(error, commands.BadArgument):
await ctx.send(f'Bad argument: {error}')
elif isinstance(error, commands.CheckFailure):
await ctx.send('You do not have permission to use this command.')
elif isinstance(error, commands.CommandOnCooldown):
await ctx.send(f'This command is on cooldown. Try again in {error.retry_after:.1f}s')
elif isinstance(error, commands.DisabledCommand):
await ctx.send('This command is disabled.')
else:
# Log unexpected errors
print(f'Unexpected error: {error}')
await ctx.send('An error occurred while executing the command.')
# Command-specific error handler
@bot.command()
async def divide(ctx, a: int, b: int):
"""Divides two numbers"""
result = a / b
await ctx.send(f'{a} / {b} = {result}')
@divide.error
async def divide_error(ctx, error):
if isinstance(error, commands.BadArgument):
await ctx.send('Please provide valid integers.')
elif isinstance(error, commands.CommandInvokeError):
if isinstance(error.original, ZeroDivisionError):
await ctx.send('Cannot divide by zero!')