javascript 如何列出来自特定服务器的所有成员?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50319939/
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 to list all members from a specific server?
提问by newbie
My code is
我的代码是
const list = client.guilds.find("id", "335507048017952771")
for (user of list.users){
console.log(user[1].username);
}
This does literally nothing. There is no error or anything.
这实际上没有任何作用。没有错误或任何东西。
I just want the bot to find a server and then log all members from said server.
我只是想让机器人找到一个服务器,然后从该服务器记录所有成员。
Displaying all connected users Discord.jsThe answers in this question didn't really help me at all. I did try using message.guild.usersbut that also did nothing. Can't seem to find anything on the Discord.js siteto help me either.
显示所有连接的用户 Discord.js这个问题的答案根本没有帮助我。我确实尝试使用,message.guild.users但也没有任何作用。在 Discord.js 网站上似乎也找不到任何可以帮助我的东西。
回答by newbie
Firstly, don't use .find("id", "335507048017952771"), you should be using .get("335507048017952771"), as it says on the discord.js documentation.
首先,不要使用.find("id", "335507048017952771"),你应该使用.get("335507048017952771"),正如它在 discord.js文档中所说的那样。
All collections used in Discord.js are mapped using their id property, and if you want to find by id you should use the get method. See MDNfor details.
Discord.js 中使用的所有集合都使用它们的 id 属性映射,如果你想通过 id 查找,你应该使用 get 方法。有关详细信息,请参阅MDN。
A Guilddoes not have a usersproperty, where as it has a membersproperty, which returns a Collectionof GuildMembers. Now to get the usernamefrom each member you can obtain that from the userproperty of the GuildMember. So, you will need to iterate through the collection of GuildMembers, and get the <GuildMember>.user.username.
一个公会没有users财产,在那里,因为它有一个members属性,它返回一个集合的GuildMember秒。现在username要从每个成员那里获取 ,您可以从userGuildMember的属性中获取。因此,您需要遍历 GuildMembers 的集合,并获取<GuildMember>.user.username.
There are several ways to do this, I will be using the forEach()method. Here's what that would look like as a result:
有几种方法可以做到这一点,我将使用该forEach()方法。结果如下:
// Get the Guild and store it under the variable "list"
const list = client.guilds.get("335507048017952771");
// Iterate through the collection of GuildMembers from the Guild getting the username property of each member
list.members.forEach(member => console.log(member.user.username));

