Skip to content

Instantly share code, notes, and snippets.

@Vexs
Last active May 13, 2021 03:17
Show Gist options
  • Save Vexs/daa1dcc92ff80fad7ca020d0f7bb4f75 to your computer and use it in GitHub Desktop.
Save Vexs/daa1dcc92ff80fad7ca020d0f7bb4f75 to your computer and use it in GitHub Desktop.
Simple Error Handling ext.commands - for the async branch of discord.py. Backport of https://gist.github.com/EvieePy/7822af90858ef65012ea500bcecf1612
import traceback
import sys
from discord.ext import commands
import discord
class CommandErrorHandler:
def __init__(self, bot):
self.bot = bot
async def on_command_error(self, error: Exception, ctx: commands.Context):
"""The event triggered when an error is raised while invoking a command.
ctx : Context
error : Exception"""
if hasattr(ctx.command, 'on_error'):
return
ignored = (commands.CommandNotFound, commands.UserInputError)
error = getattr(error, 'original', error)
if isinstance(error, ignored):
return
elif isinstance(error, commands.DisabledCommand):
await self.bot.send_message(ctx.message.channel, '{} has been disabled.'.format(ctx.command))
return
elif isinstance(error, commands.NoPrivateMessage):
try:
await self.bot.send_message(ctx.message.author, '{} can not be used in Private Messages.'.format(ctx.command))
return
except discord.Forbidden:
pass
elif isinstance(error, commands.BadArgument):
if ctx.command.qualified_name == 'tag list':
await self.bot.send_message(ctx.message.channel, 'I could not find that member. Please try again.')
return
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))
@AstaProgammer
Copy link

nice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment