计算discord.py中具有特定角色的成员

时间:2021-03-16 17:30:00

标签: discord.py

所以我正在尝试创建一个命令来显示使用某个角色的成员的数量(角色应该由消息定义(所以我不必只为 1 个角色制作脚本,使用角色 ID) ) 但它似乎根本不起作用,所以也许有一些建议?我是编程新手,使用 API 并没有给我带来太多好处:) 理想的设置是拥有一个系统,该系统可以显示有关所有角色的信息,或者仅显示 1 个角色(您通过消息定义)。

所以我试过的是:

async def rolescount(ctx):
  def check(message):
    return message.author == ctx.author and message.channel == ctx.channel
    
  await ctx.send('Waiting for role')
  
  message = await bot.wait_for('message', check=check)
  role = ctx.get_role_name(message.content)
  role = ctx.guild.get_role(role.content)
  await ctx.send(len(role.members))

但这似乎不起作用,当机器人在等待消息时,他不接受任何回应,只是坐在那里,我做错了什么?

也试着找过类似的案例,真的没什么,先谢谢了!

1 个答案:

答案 0 :(得分:0)

试试这个:


@bot.command()
async def rolescount(ctx, role: discord.Role):
    await ctx.send(len(role.members))

现在你已经像这样使用这个命令了:<PREXIX>rolescount @ROLE 如果要打印具有此角色的所有成员,请执行以下操作:

@bot.command()
async def rolescount(ctx, role: discord.Role):
    for member in role.members:
        print(f"{member.name}#{member.discriminator}")