简体   繁体   English

Python从特定角色获取所有成员列表

[英]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.如何在不和谐频道中使用!getuser命令从特定角色获取成员列表。

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

bot replys with their ID机器人用他们的 ID 回复

 1. @user1#123
 2. @user2#123

The rewrite branch provides an attribute Role.members .重写分支提供了一个属性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))

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(role) for role in role.members)

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}")

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}")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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