How to get an array of names in discord.js?

Member

by larissa , in category: Javascript , a day ago

How to get an array of names in discord.js?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by elisha_langworth , 2 hours ago

@larissa 

In Discord.js, you can get an array of names of all members in a guild by using the member property of the Guild object. Here is an example code snippet to get an array of names of all members in a guild:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
const Discord = require('discord.js');
const client = new Discord.Client();

client.on('ready', () => {
  const guild = client.guilds.cache.get('YOUR_GUILD_ID');
  
  if(guild) {
    const memberNames = guild.members.cache.map(member => member.user.tag);
    console.log(memberNames);
  }
});

client.login('YOUR_DISCORD_BOT_TOKEN');


Replace 'YOUR_GUILD_ID' with the ID of the guild you want to get the names from, and 'YOUR_DISCORD_BOT_TOKEN' with your Discord bot token. When you run this code, it will log an array of names of all members in the specified guild.