如何使用 discord.py 列出不和谐服务器中的所有成员?

2022-01-14 00:00:00 python discord discord.py server

问题描述

我正在编写一个不和谐的机器人,在尝试使用命令 !members in on_message 事件从服务器中提取所有成员时遇到此错误:

I am writing a discord bot and I came across this error when trying to pull all the members from a server with the command !members in on_message event:

elif message.content.startswith('!members'):
    x = server.Server.members
    for member in x:
        print(member)

我希望这个命令拉出所有成员并在控制台中打印出来,但我得到了错误

I want this command to pull all members and print them out in the console but I get the error

TypeError: 'property' 对象不可迭代

TypeError: 'property' object is not iterable

当我在不和谐频道中输入命令时.谁能帮我列出频道中所有成员的列表以供进一步使用?

when I type the command in the discord channel. Could anyone help me make a list of all members in the channel that I can have for further use?


解决方案

你需要一个服务器实例来从中获取成员列表.

You need an instance of a server to get the members list from it.

假设此代码出现在 on_message(message) 中,您应该可以更改您的

Assuming this code appears in on_message(message), you should be able to change your

x = server.Server.members

x = message.server.members

请注意,使用带有大写 S 的 Server 将返回类定义,而使用消息中的 server 属性(小写 s)将检索 Server 的实例.

Note that using Server with a capital S will return the class definition, whereas using the server property (lowercase s) from the message will retrieve an instance of Server.

如果您使用的版本 >= 1.0.0,则为

If you're using a version >= 1.0.0, this will be

x = message.guild.members

改为.

相关文章