Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python get all members list from a specific role

How to get a members list from a specific role with !getuser command in discord channel.

@bot.command(pass_context=True)  
async def getuser(ctx):

bot replys with their ID

 1. @user1#123
 2. @user2#123
like image 843
Demotry Avatar asked Aug 20 '26 20:08

Demotry


2 Answers

All these solutions are too inefficient when you can just do

@bot.command()
async def getuser(ctx, role: discord.Role):
    await ctx.send("\n".join(str(member) for member in role.members)
like image 170
Wasi Master Avatar answered Aug 22 '26 09:08

Wasi Master


The rewrite branch provides an attribute Role.members.

On the async branch, you'll have to loop through all the members of the server and check their roles.

@bot.command(pass_context=True)  
async def getuser(ctx, role: discord.Role):
    role = discord.utils.get(ctx.message.server.roles, name="mod")
    if role is None:
        await bot.say('There is no "mod" role on this server!')
        return
    empty = True
    for member in ctx.message.server.members:
        if role in member.roles:
            await bot.say("{0.name}: {0.id}".format(member))
            empty = False
    if empty:
        await bot.say("Nobody has the role {}".format(role.mention))
like image 43
Patrick Haugh Avatar answered Aug 22 '26 09:08

Patrick Haugh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!