Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from discord.ext import commands
- import discord
- import asyncio
- class CommandErrorHandler(commands.Cog):
- def __init__(self, bot):
- self.bot = bot
- @commands.Cog.listener()
- async def on_command_error(self, ctx, error):
- """The event triggered when an error is raised while invoking a command.
- ctx : Context
- error : Exception"""
- error = getattr(error, 'original', error)
- # if isinstance(error, ignored):
- # return
- msg = None
- if isinstance(error, commands.CommandNotFound):
- msg = await self.em(ctx, title="Command Not Found", description=str(error), bot=self.bot)
- elif isinstance(error, commands.DisabledCommand):
- msg = await self.em(ctx, title="Disabled Command", description=str(error), bot=self.bot)
- elif isinstance(error, commands.NoPrivateMessage):
- try:
- em = discord.Embed(title="No Private Message", description=str(error))
- em.set_author(name=self.bot.user.display_name, icon_url=self.bot.user.avatar_url)
- msg = await ctx.author.send(embed=em)
- except Exception:
- pass
- elif isinstance(error, commands.BadArgument):
- msg = await self.em(ctx, title='Bad Argument', description=str(error), bot=self.bot)
- elif isinstance(error, commands.BotMissingPermissions):
- return await ctx.send('I am missing these permissions to do this command:'
- f'\n{self.lts(error.missing_perms)}')
- elif isinstance(error, commands.MissingPermissions):
- return await ctx.send('You are missing these permissions to do this command:'
- f'\n{self.lts(error.missing_perms)}')
- elif isinstance(error, (commands.BotMissingAnyRole, commands.BotMissingRole)):
- return await ctx.send(f'I am missing these roles to do this command:'
- f'\n{self.lts(error.missing_roles or [error.missing_role])}')
- elif isinstance(error, (commands.MissingRole, commands.MissingAnyRole)):
- return await ctx.send(f'You are missing these roles to do this command:'
- f'\n{self.lts(error.missing_roles or [error.missing_role])}')
- elif isinstance(error, commands.CommandInvokeError):
- return await self.em(ctx, title="Command Invoke Error", description=str(error), bot=self.bot)
- elif isinstance(error, commands.CommandOnCooldown):
- after = error.retry_after
- sec = divmod(after, 60)[1]
- min_ = divmod(divmod(after, 60)[0], 60)[1]
- hours = divmod(divmod(after, 60)[0], 60)[0]
- msg = await self.em(ctx, title="Error", description=f'Please wait another `{hours}` hours and `{min_}` minutes and `{sec}` secs', bot=self.bot)
- else:
- await self.em(ctx, title=str(error.__class__.__name__), description=str(error), bot=self.bot)
- if type(msg) == discord.Message:
- await asyncio.sleep(5.0)
- await msg.delete()
- @staticmethod
- def lts(list_: list):
- """List to string.
- For use in `self.on_command_error`"""
- return ', '.join([obj.name if isinstance(obj, discord.Role) else str(obj).replace('_', ' ') for obj in list_])
- @staticmethod
- async def em(self, ctx, *, title, description=None, bot=None, **kwargs):
- """Quick embed sending."""
- kwargs.pop('title', None)
- kwargs.pop('description', None)
- color = kwargs.pop('color', None)
- em = discord.Embed(title=title, description=description, color=color if color else discord.Color.blue())
- if bot is not None:
- em.set_author(name=bot.user.display_name, icon_url=bot.user.avatar_url)
- return await ctx.send(embed=em, **kwargs)
- def setup(bot):
- bot.add_cog(CommandErrorHandler(bot))
Add Comment
Please, Sign In to add comment