首页 > 解决方案 > 在 discord.py 中计算具有特定角色的成员

问题描述

所以我正在尝试创建一个命令来显示使用某个角色的成员的数量(角色应该由消息定义(所以我不必只为 1 个角色制作脚本,使用 roleid))但是它似乎根本不起作用,所以也许有一些建议?我是编程新手,使用 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))

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

也试图找到类似的案例,真的没有,提前谢谢!

标签: discord.py

解决方案


尝试这个:


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

推荐阅读