Sudip Roy
Sudip Roy

Reputation: 11

How to get all needed permissions to use a command on discord.py?

Like how Dank memer bot does in help command I also want that feature in my custom help command enter image description here

I used this Code but it failed for custom decorators

import traceback
#This is the command
@bot.command()
@commands.has_permissions(administrator=True, manage_messages=True)
async def func(ctx):
    pass

# In your whatever file or error handler.
command = bot.get_command("func") # Get the Command object
try:
    # You can pretty much loop these to get all checks from the command.
    check = command.checks[0] # get the first check
    check(0) # This would raise an error, because `0` is passed as ctx
except Exception as e:
    frames = [*traceback.walk_tb(e.__traceback__)] # Iterate through the generator
    last_trace = frames[-1] # get the last trace
    frame = last_trace[0] # get the first element to get the trace
    print(frame.f_locals['perms']) # Output: {'administrator': True, 'manage_messages': True}

Upvotes: 1

Views: 125

Answers (1)

Łukasz Kwieciński
Łukasz Kwieciński

Reputation: 15689

If you have the command in the error handler you can simply use the missing_perms attribute

@bot.event
async def on_command_error(ctx, error):
    if isinstance(error, commands.MissingPermissions):
        missing_perms = error.missing_perms
        await ctx.send(f"You are missing: {missing_perms} to run this command")

Reference:

Upvotes: 1

Related Questions