How to check if a user has permission to use a command? Discordpy

Viewed 58

I am creating a custom help command and I have a problem. The default help command can somehow hide commands that are not available for a user that invoked the command. I was looking everywhere and I can't find any solution. I tried using hidden attribute of a command object but it is not working. The best solution would be a method like Member.can_use(command) that would return True or False but there is nothing like that in docs. Or at least I cannot find it. Help would be appreciated.

2 Answers

Use the Command.can_run method, an example would be:

@bot.command()
async def can_it_run(ctx, command_name: str):
    command = bot.get_command(command_name)
    can_use = await command.can_run(ctx)
    if can_use:
        await ctx.send(f"You can use {command_name}") 
    else:
        await ctx.send(f"You can't use {command_name}") 

Sadly if a check raises an exception (like commands.guild_only) the exception(s) won't be suppressed, also if you want to check if someone else (not the invoker) can use the command you have to overwrite Context.author, a handy function would be:

async def can_run(ctx, command_name: str, member: discord.Member=None) -> bool:
    command = bot.get_command(command_name)
    if command is None: # Command doesn't exist / invalid command name
        return False # Or raise an exception

    if member is not None: # If a member is passed overwrite the attribute
        ctx.author = member

    try:
        return await command.can_run(ctx)
    except Exception:
        return False

@bot.command()
async def can_it_run(ctx, command_name: str, member: discord.Member=None):
    resp = await can_run(ctx, command_name, member)
    await ctx.send(resp)

You could just check if the user countains a certain id by doing the following:

if (message.author.id == "WHoever ID1" || message.author.id == "Whoever ID2"):
   #Do Something

If you want to put a bunch of ID's in then just do the following:

bool = false;
arr = ["ID1", "ID2", "ID3"]

for x in arr:
    if(x == message.author.id):
        bool = true;

if(bool):
  #Do stuff
  bool = false;

This is very vague code but it should be something similar to this

Related