简体   繁体   English

如何将服务器中每个用户的 id 放入数组中? (Discord.js v12)

[英]How do I get every user's id in a server into an array? (Discord.js v12)

I need help with getting all users id in a server.我需要帮助来获取服务器中的所有用户 ID。 I already tried all solutions, apparently, it won't work for me.我已经尝试了所有解决方案,显然,它对我不起作用。

I already tried this (which is the most relevant to me)我已经尝试过了(这与我最相关)

// Get the Guild and store it under the variable "list" const list = client.guilds.cache.get("335507048017952771"); // 获取 Guild 并将其存储在变量“list”下 const list = client.guilds.cache.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)); // 遍历 Guild 的 GuildMembers 集合,获取每个成员的用户名属性 list.members.forEach(member => console.log(member.user.username));

The members of a guild are stored in a Collection.公会的成员存储在一个集合中。 You can use the map() method to create an array including only the IDs.您可以使用map()方法创建一个仅包含 ID 的数组。

const Guild = client.guilds.cache.get("335507048017952771"); // Getting the guild.
const Members = Guild.members.cache.map(member => member.id); // Getting the members and mapping them by ID.
console.log(Members);
// --> ["1234567890054356", "1323534709650967", "436567540796390"] etc...

Using map works fine but keys() is another solution, which returns a map iterator使用 map 工作正常,但 keys() 是另一种解决方案,它返回 map 迭代器

const list = <Guild>.members.cache.keys();
list.forEach(id => console.log(id));

If you want to make it into an array just use spread operator如果你想把它变成一个数组,只需使用扩展运算符

const arr = [...list];

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

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