Listing every user in a guild regardless of their Discord Presence.
client.on("message", (message) => {
// if (message.content == "hi") {
// if (message.author.id !== "ID") return;
const guild = client.guilds.cache.get('ID')
const users = client.users.cache.map((u) => u.id);
message.guild.members.cache.forEach(members => console.log(users && `Amount of users:`, users.length))
console.log(`Listing user ids from ${guild.name}:`);
console.log(users);
console.log(users.length)
// }
});
My current code does not list users in the guild ID I provided, the bot is in the server. Is there something I am doing wrong?
CodePudding user response:
Maps don’t use length
but size
instead. So in order to list everyone in a guild, you could do something like this:
client.on("message", async message => {
if(message.author.bot === true) return;
if(message.channel.type === "dm") return;
// Trigger with 'list all members'
if(message.content.toLowerCase() === "list all members") {
// Get the guild from Cache using the GuildID from the message
// Or manually put in the ID from the server your bot is in.
const guild = client.guilds.cache.get(message.guild.id);
// Force member cache to be cleared and renewed.
await guild.members.fetch({ force: true });
// Map all users.
console.log(`Listing ${guild.members.cache.size} members from ${guild.name}.`);
guild.members.cache.forEach(member => {
console.log(`${member.user.tag} (ID: ${member.id})`);
});
}
});