提及,createdAt字段

时间:2019-11-04 11:30:46

标签: discord.js

我无法解决提及第三方用户的错误,错误始于“ Roles”和服务器登录日期(即帐户创建日期)。

请帮助。

    ... [cut]

    } else if (command === 'user') {
    let user = message.mentions.users.first() || message.author;
    const embed = new Discord.RichEmbed()
        .setAuthor(`Akirabot. Find of user.`)
        .setColor(0x348fcd)
        .setTitle('')
        .addField(`Username`,`${user.username}`, true)
        .addField(`User id`,`${user.id}`, true)
        .addField(`Status`,`${user.presence.status}`, true)
        .addField(`Date of create account`, `${moment(user.createdAt).toString().substr(0, 15)}\n(${moment(user.createdAt).fromNow()})`, true)
        .addField("Currently Playing", user.presence.game || "Nothing.", true)
        .addField(`Join date to server`, `${moment(user.joinedAt).toString().substr(0, 15)}\n(${moment(user.joinedAt).fromNow()})`, true)
        .addField('Roles', `${user.members.roles}.`, true)
        .setFooter(`— Akirabot, made with love by setosh, 2019.`)
        .setTimestamp('')
        .setThumbnail(`${user.displayAvatarURL}`)
        message.channel.send({ embed });

我收到此错误消息

TypeError: Cannot read property 'roles' of undefined

1 个答案:

答案 0 :(得分:0)

User class(这是message.mentions.users.first()message.author的返回值)没有members属性,因此您试图访问的内容不存在”不存在。

如果您想找出某人的角色,则必须获得该人的GuildMember representation。一种方法是这样的:

const guildMember = message.guild.members.find(val => val.id === user.id);

然后您可以执行guildMember.roles并随意使用它。


示例:

let user = message.mentions.users.first() || message.author;
const guildMember = message.guild.members.find(val => val.id === user.id);
const embed = new Discord.RichEmbed()
  // Do whatever you want with the roles:
  .addField('Roles', `${guildMember.roles}.`, true)