简体   繁体   中英

how to get all members id in discord.js

i am trying to a make a list of all the members id of a given guild id. I am trying various things but all are giving collections. how can i only retrieve the ids.

console.log(msg.guild.name)
console.log(msg.guild.id)
console.log(msg.guild.members.fetch());

the log is

 '690958932406960249' => GuildMember {
 guild: [Guild],
 user: [User],
 joinedTimestamp: 1586525303612,
 lastMessageID: null,
 lastMessageChannelID: null,
 premiumSinceTimestamp: null,
 deleted: false,
 _roles: []
 },
'690988332653608980' => GuildMember {
guild: [Guild],
user: [User],
joinedTimestamp: 1591198472051,
lastMessageID: null,
lastMessageChannelID: null,
premiumSinceTimestamp: null,
deleted: false,
 _roles: []
 },

You can map the members' IDs using Array.prototype.map() . Example:

const Members = client.guilds.cache.get("GuildID").members.map(member => member.id);
// Returns Array: ["4634643643262345", "684369346943609235", "4683496834643653543"] etc...

From the collection, you can use .keys() method to get the keys (ID of the members).

let memberIds = Array.from(collection.keys())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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