Discord.py: how to see if a member has a role with having to ping themselves when using the cmd

code i tried: did actually run, but it always said You don’t have perms even when i did have the role.

@client.command(pass_context=True, aliases = ['4v4 ping'])
async def ping4v4(author):
    channel = client.get_channel(870969338679660591)
    guild = client.get_guild(870969337945669722)
    role = guild.get_role(870969337958240260)
    if author in role.members:
        await channel.send(f" 4v4 ping hehe ")
    else:
        await channel.send(f" You don't have perms! ")

is there another way to see if a member has a role or a role has a member?

Answer

As already mentioned you should always add ctx as an “argument”, this is actually standard and makes many things easier for you here. You don’t need author as an argument and can shorten the code.

Querying the guild/role is very easy via ctx.guild.get_role – Here it is checked if the role is available on the server where the command was executed.

To check if the author of the message has this role we have to check the other way around and not if the author is in the role, loosely translated. So this snippet would be revised as follows: if role in ctx.author.roles:

The revised code:

@client.command(pass_context=True, aliases = ['4v4 ping'])
async def ping4v4(ctx): # Only ctx as an "argument"
    channel = client.get_channel(ChannelID)
    role = ctx.guild.get_role(RoleID) # Get the guild via ctx.guild
    if role in ctx.author.roles: # Check if the ctx.author has the role
        await channel.send("4v4 ping hehe") # No f-strings needed
    else:
        await channel.send("You don't have perms!")