简体   繁体   English

如何在#channel 中显示具有特定角色的成员? (使用discord.py)

[英]how can I display in the #channel, members with a certain role? (with discord.py)

I'm trying to write a command in my (first) bot that prints in the discord channel all members in a certain @role (in this it's called "Serf") this is my command/function我正在尝试在我的(第一个)机器人中编写一个命令,该命令在不和谐频道中打印某个@role 中的所有成员(在此称为“Serf”)这是我的命令/功能

@client.command()
async def snap(ctx):
    target = discord.Role.members("Serf")
    for person in target:
        await ctx.send(person)

but nothing happens and I get this error in the terminal但没有任何反应,我在终端中收到此错误

Ignoring exception in command snap:
Traceback (most recent call last):
  File "/home/thonkpad/.local/lib/python3.7/site-packages/discord/ext/commands/core.py", line 83, in wrapped
    ret = await coro(*args, **kwargs)
  File "Python/thanosBot/bot.py", line 28, in snap
    target = discord.Role.members("Serf")
TypeError: 'property' object is not callable

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/thonkpad/.local/lib/python3.7/site-packages/discord/ext/commands/bot.py", line 892, in invoke
    await ctx.command.invoke(ctx)
  File "/home/thonkpad/.local/lib/python3.7/site-packages/discord/ext/commands/core.py", line 797, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "/home/thonkpad/.local/lib/python3.7/site-packages/discord/ext/commands/core.py", line 92, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: 'property' object is not callable

The problem is you're calling the actual discord.Role object itself and not finding the Role object of the specific role you want.问题是您正在调用实际的 discord.Role 对象本身,而没有找到您想要的特定角色的 Role 对象。 You could do something like this:你可以这样做:

@client.command()
async def snap(ctx):
    role = discord.utils.get(ctx.message.guild.roles, name="Serf")
    target = role.members
    for person in target:
        await ctx.send(person.name)

target will be a list of discord.Member objects target将是一个 discord.Member 对象列表

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

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