'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
Solution 1:[1]
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)
Solution 2:[2]
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)
async def members(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
break
else:
await ctx.send("Role doesn't exist")
return
for member in server.members:
if role_id in member.roles:
await ctx.send(f"{member.display_name} - {member.id}")
Solution 3:[3]
Hopefully a faster and more readable solution than the one before
@bot.command(pass_context=True)
async def getuser(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
break
else:
await bot.say("Role doesn't exist")
return
for member in server.members:
if role_id in member.roles:
await bot.say(f"{role_name} - {member.name}")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Ibex |
Solution 3 |