Skip to content Skip to sidebar Skip to footer

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. @us

Solution 1:

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)  asyncdefgetuser(ctx, role: discord.Role):
    role = discord.utils.get(ctx.message.server.roles, name="mod")
    if role isNone:
        await bot.say('There is no "mod" role on this server!')
        return
    empty = Truefor member in ctx.message.server.members:
        if role in member.roles:
            await bot.say("{0.name}: {0.id}".format(member))
            empty = Falseif empty:
        await bot.say("Nobody has the role {}".format(role.mention))

Solution 2:

All these solutions are too inefficient when you can just do

@bot.command()asyncdefgetuser(ctx, role: discord.Role):
    await ctx.send("\n".join(str(role) for role in role.members)

Solution 3:

Patrick's answer doesn't work at all, Tristo's answer is better, but I tweaked a few things to make it work with rewrite:

@bot.command(pass_context=True)@commands.has_permissions(manage_messages=True)asyncdefmembers(ctx,*args):
    server = ctx.message.guild
    role_name = (' '.join(args))
    role_id = server.roles[0]
    for role in server.roles:
        if role_name == role.name:
            role_id = role
            breakelse:
        await ctx.send("Role doesn't exist")
        returnfor member in server.members:
        if role_id in member.roles:
            await ctx.send(f"{member.display_name} - {member.id}")

Solution 4:

Hopefully a faster and more readable solution than the one before

@bot.command(pass_context=True)  asyncdefgetuser(ctx,*args):
  server = ctx.message.server
  role_name = (' '.join(args))
  role_id = server.roles[0]
  for role in server.roles:
    if role_name == role.name:
      role_id = role
      breakelse:
    await bot.say("Role doesn't exist")
    returnfor member in server.members:
    if role_id in member.roles:
      await bot.say(f"{role_name} - {member.name}")

Post a Comment for "Python Get All Members List From A Specific Role"