简体   繁体   中英

How to get an array of Names in Discord.js

I am new to discord.js and I am stuck getting the names of all the members of the server. This is the code:

**const List = bot.guilds.cache.get("88888952704"); // Getting the guild.
const Members = List.members.map((member) => {
    member.displayName;
});**

//Basic hello
bot.on("message", (msg) => {
    let args = msg.content.slice(Prefix.length).split(" ");

    switch (args[0]) {
        //For Testing
        case "ping":
            msg.channel.send("Pong!");
            break;
        //Now Serious
        case "info":
            if (args[1] == "version") {
                msg.reply("I am currently at version " + botVersion + "!");
            } else if (args[1] == null) {
                msg.channel.send(
                    "Please be specific about what info you want like: !info version "
                );
            }
            break;
        case "cleartill":
            if (!args[1]) {
                msg.reply("Please be specific: Error 01B%s@1");
            } else {
                msg.channel.bulkDelete(args[1]);
                break;
            }
        case "embed":
            const embed = new Discord.MessageEmbed()
                .setTitle("User Information:")
                .addField("Player Name:", msg.author.username)
                .setColor("#FFFF00")
                .addField("Version:", botVersion);
            msg.channel.send(embed);
            break;
        case "whoisa":
            if (args[1] == null) {
                msg.reply("Please enter the second argument.");
            } else {
                msg.channel.send(Members + " is a " + args[1]);
            }
    }
});

I want to get a random name of any person in the server and reply to the person who entered !whoisa [something] and give the answer [something] is a [args[1]] . Please help. Thanks in advance!

Once you have the guild, you can fetch all members using members.fetch() . It returns a collection which has a .map() method, so you can iterate over the collection and return their displayName like this:

const guild = bot.guilds.cache.get("824977762186952704");
const members = await guild.members.fetch();

const names = members.map((member) => member.displayName);

// names is an array of names
console.log(names);

Collections also have a .random() method that you can use to get a random member from that collection. The returned member is a GuildMember , so you can get its display name, send a DM to them, etc.

// randomMember is a guild member
const randomMember = members.random();

If you want to use it with your message event, you can even get the guild from the message. As I'm using await here, make sure you add the async keyword in bot.on("message", async (msg) => { .

case 'whoisa':
  if (!args[1]) 
    return msg.channel.send('Please enter an argument');

  const members = await msg.guild.members.fetch();

  msg.channel.send(`${members.random()} is a ${args.slice(1).join(' ')}`);

在此处输入图像描述

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