Python 如何使用 discord.py 列出不和谐服务器中的所有成员?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47733376/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I make a list of all members in a discord server using discord.py?
提问by Dragomir Aki
Hi guys 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:
大家好,我正在编写一个不和谐的机器人,当我尝试使用 on_message 事件中的命令 !members 从服务器中拉出所有成员时遇到了这个错误:
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' object is not iterable
我希望此命令可以拉出所有成员并在控制台中将它们打印出来,但出现错误 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?
当我在不和谐频道中输入命令时。任何人都可以帮我列出频道中我可以拥有的所有成员以供进一步使用吗?
回答by Sam Rockett
You need an instance of a server to get the members list from it.
您需要一个服务器实例来从中获取成员列表。
Assuming this code appears in on_message(message)
, you should be able to change your
假设此代码出现在 中on_message(message)
,您应该能够更改您的
x = server.Server.members
to
到
x = message.server.members
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.
请注意,使用Server
大写 S 将返回类定义,而使用消息中的server
属性(小写 s)将检索 Server 的实例。
If you're using a version >= 1.0.0, this will be
如果您使用的版本 >= 1.0.0,这将是
x = message.guild.members
instead.
反而。
回答by JeydinNewWon
elif message.content.startswith('!members'):
x = message.server.members
for member in x:
print(member.name) # you'll just print out Member objects your way.
回答by crousap
I think you can do it so that the code is shorter
我认为你可以这样做,以便代码更短
elif message.content.startswith('!members'):
print(", ".join([member.name for member in message.server.members])) #You do the list with names of roles and print this without cycle