简体   繁体   中英

how do I check if a user has a specific role with role id

i want to see if someone has a role but i want to check with role id not role name

Example:

@client.command()
@guild_only()
@commands.has_role(role_id='2374623645')
async def test(ctx):
    await ctx.send('test')

I guess if you have a role id, you could convert it into a role name.

@client.command()
@guild_only()
role_id='2374623645'
role_name = my_server.get_role(role_id)
@commands.has_role(role_name)
async def test(ctx):
    await ctx.send('test')

This would be a full example of finding out, if someone has the target role.

Syntax: [prefix]test <Member mention/ID>

It will delete the invocation message and send an error if member is not provided. Otherwise it will check if member has the role obtained with discord.utils.get() and provide the result.

@client.command()
@guild.only()
async def test(ctx, member: discord.Member=None):
    await ctx.message.delete()
    if member is None:
        await ctx.send("You need to provide a member ID/Mention.")
        return
    else:
        pass
        
    target_role = discord.utils.get(ctx.guild.roles, id=123456789012345678)
    if target_role in member.roles:
        await ctx.send(f"{member} does have the target role.")
    else:
        await ctx.send(f"{member} does not have the target role.")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM