Velvet Star Monitor

Standout celebrity highlights with iconic style.

updates

Handling errors in discord.py (no permission)

Writer Mia Lopez

I'm trying to make a moderation bot with commands like kick, ban, prune, etc, and I have all of the commands, but I can't figure out how to handle the error you get when the sender doesn't have permission. This is my code so far:

@commands.command(name='kick')
@commands.guild_only()
@commands.has_permissions(kick_members=True)
async def kick(self, ctx, member : discord.Member, *, reason=None): await member.kick(reason=reason) await ctx.send(f'User {member.mention} has been kicked for: "{reason}".')
@kick.error
async def kick_error(self, error, ctx): if isinstance(error, error.MissingPermissions): await ctx.send(":redTick: You don't have permission to kick members.")

No error shows up in the console, but the message doesn't send.

4 Answers

The issue is that when a user with invalid permissions tries to use that command, the exception is caught in error, however, error is just the exception that is caught by discord.pysince the user did not have permission. Therefore, the function is catching the error, no problem, but your if-statement is failing because because error.MissingPermissions is not an instance of error. It doesn't even exist since error is just what the exception thrown by discord.py that your function caught. There is no MissingPermissions as a part of that. Therefore, there is no such thing as MissingPermissions from the error you caught because that's just an error. You need to see if error is an instance of MissingPermissions from the discord.py missing permissions class (which it is). Thus, in order to fix it, import the MissingPermissions and you'll be good to go. I've fixed the code for you below.

from discord.ext.commands import MissingPermissions
# Code
@kick.error
async def kick_error(self, ctx, error): if isinstance(error, MissingPermissions): await ctx.send(":redTick: You don't have permission to kick members.")

Sorry if the explanation is unclear. I can try to explain it again if you'd like me to or someone can edit this to make it clearer and more accurate.

1

commands.MissingPermissions not error.MissingPermissions

3

also if you faced an exception: ctx is a required attribute that is missing

it is because your bot isn't in a class. The solution:

from discord.ext.commands import MissingPermissions
# Code
@kick.error
async def kick_error(ctx, error): if isinstance(error, MissingPermissions): await ctx.send(":redTick: You don't have permission to kick members.")

in short, remove the self attribute. This answer may seem silly, but to only experts. i expect beginners will find this as helpful! :)

@client.event
async def on_command_error(ctx, error): if isinstance(error, MissingPermissions): await ctx.send("You are missing permission(s) to run this command.") else: raise error

in your error handler, always raise error

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy