简体   繁体   中英

Discord.js member.user returns the member's ID surrounded by <>

I'm trying to create a command for a discord bot which lists all the members of the inputted role, I'm currently mapping the members to a variable as shown below:

let roleMembs = message.guild.roles.cache.get(roleId).members.map(m=>m.user)

It works for the most part but some of the members are outputted as <@320600141855981588> rather than their name, I'm not sure why this is and I'm not sure of a work around, any help would be appreciated, I will leave the rest of the code below

const Discord = require('discord.js')

module.exports.run = async (client, message, args) => {
    if (!message.member.roles.cache.find(r => r.name === "-------------- SERVER STAFF --------------")) return message.reply("You must be part of the moderation team to use this command!")
    let roleReq = message.content.split(" ").slice(1)
    let role = message.guild.roles.cache.find(r => r.name === roleReq.join(" "))

    if(typeof role === "undefined") return message.channel.send("You have input an invalid role! Please try again!")
    
    let roleId = role.id
    let roleMembs = message.guild.roles.cache.get(roleId).members.map(m=>m.user)

    let roleList = ""

    if(roleMembs.length > 25) {
        let embed = new Discord.MessageEmbed()
       for(let i = 0; i < 25; i++){
          roleList = roleList.concat(`${roleMembs[i]}\n`)
        }

       let roleList2 = ""
       let roleList3 = ""

       let x = 0
       let y = 0

       while(x < 25){
          roleList2 = roleList2.concat(`${roleMembs[x+25]}\n`)
          x++
          if (typeof roleMembs[x+25] === "undefined"){
              break;
            }
        }
        if(roleMembs.length > 50){
            while(y < 25){
                roleList3 = roleList3.concat(`${roleMembs[y+50]}\n`)
                y++
                if (typeof roleMembs[y+50] === "undefined"){
                    break;
                }
            }
        }
        embed.setAuthor(client.user.username, client.user.displayAvatarURL())
        embed.setTimestamp()
        embed.setTitle(`${roleMembs.length} Members with the ${roleReq.join(" ")} role:`)
        embed.setColor("#add8e6")
        embed.addField("‏‏‎ ‎", roleList, true)
        embed.addField("‏‏‎ ‎", roleList2, true)
        if(roleList3.length > 0) {
            embed.addField("‏‏‎ ‎", roleList3, true)
        }

        message.channel.send(embed)
    } else {
        roleMembs.forEach(member => {
            roleList = roleList.concat(`${member}\n`)  
        })
        let embed = new Discord.MessageEmbed()
        embed.setAuthor(client.user.username, client.user.displayAvatarURL())
        embed.setTimestamp()
        embed.setTitle(`${roleMembs.length} Members with the ${roleReq.join(" ")} role:`)
        embed.setColor("#add8e6")
        embed.addField(" ", roleList)
        message.channel.send(embed)
    }
}

<@ID> is Discord markdown representing a user mention. In a regular message, they would show up as the clickable blue link you see when you mention a user. Sometimes in embeds they break.

If you want their name, then specify that by mapping to their name. Right now you're creating an array of just user objects and expecting it to spit out their name.

let roleMembs = message.guild.roles.cache.get(roleId).members.map(m=>m.user.username)

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