简体   繁体   English

Discord.js member.user 返回由 <> 包围的成员 ID

[英]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:我正在尝试为 discord 机器人创建一个命令,其中列出了输入角色的所有成员,我目前正在将成员映射到一个变量,如下所示:

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它在大多数情况下都有效,但一些成员输出为 <@320600141855981588> 而不是他们的名字,我不确定这是为什么,我不确定是否可以解决,任何帮助将不胜感激,我会留下下面代码的rest

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. <@ID>是代表用户提及的 Discord markdown。 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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