Coleman Ethington
Coleman Ethington

Reputation: 162

Making a different error handler for each command in cog file discord.py

I'm trying to make a seperate error handler for each command in my cog file. For example, i want the bot to say Sorry, you do not have permission to do this! Required Permission: Administrator for one command and Sorry, you do not have permission to do this! Required Permission: Manage_Roles for another. How can i do this in a cog file? I already have

@commands.Cog.listener()
    async def on_command_error(self, ctx, error):
      if isinstance(error, commands.MissingRequiredArgument):
        await ctx.send(f"{ctx.author.mention}, Sorry, you need to tell me what the new prefix is!")
      if isinstance(error, commands.MissingPermissions):
        await ctx.send(f"{ctx.author.mention}, Sorry, you do not have permission to do this! `Required Permission: Administrator`")

but this is for all of my commands. I know how to do this in a main.py file but I know its different in a cog file. Thanks

Upvotes: 0

Views: 216

Answers (1)

Ceres
Ceres

Reputation: 2658

You can get the list of missing permissions from the error.missing_perms attribute .

if isinstance(error, commands.MissingPermissions):
        await ctx.send(f"{ctx.author.mention}, Sorry, you do not have permission to do this! `Required Permission: {*error.missing_perms,}`")

Edit:
using str.join

{", ".join(error.missing_perms)}

References:

note: MissingPermissions.missing_perms returns a list, so we use *list, to format it, you could also use str.join if you don't want the parenthesis that comes with using *list,

Upvotes: 2

Related Questions