discord.py - 在 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))
但这似乎不起作用,当机器人在等待消息时,他没有任何回应,只是坐在那里,我做错了什么?
也试图找到类似的案例,真的没有,提前谢谢!
解决方案
尝试这个:
@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}")
推荐阅读
- python - 第 26 行,在
press = pygame.key.get_pressed() pygame.error: 视频系统未初始化 - android - 如何将数据 Firebase Auth 数据发送到 android 中片段的多个活动 | 科特林
- laravel - 在 laravel 中通过命令运行辅助函数
- c++ - date::locate_zone 与现有时区的异常
- azure - 我们可以使用 Azure 数据工厂将整个 csv 数据从 blob 存储传递到本地 SQL 数据库吗?
- gtk - 如何为 glib-compile-resources 设置自动工具?
- aws-chime-sdk - 适用于 .NET 的 Amazon Chime 开发工具包
- java - 如何在 Openshift 上实现 Apache Camel Master 组件
- html - 单击按钮会忽略 z-index - 为什么?
- byte - Raku 中的 `bytes.fromhex` 和 `to_bytes` 方法?