无法读取未定义的属性“角色” discord.js | JavaScript的

时间:2017-12-10 11:32:55

标签: javascript discord discord.js

bot.on("message", (message) => {
if(message.author.bot) return;
  if(message.content.startsWith(config.prefix + "userinfo") || message.content.startsWith(config.prefix + "ui")) {

    let usera = message.mentions.users.first();
    if(!usera) return message.channel.send("Must specify user");
    let verifiedRole = usera.member.roles.find(val => val.name === 'verified');
    let value = verifiedRole ? "true" : "false";
    let gameName = usera.presence.game ? usera.presence.game.name : "None";


    let embed = new Discord.RichEmbed()
    .setAuthor(usera.tag, usera.avatarURL)
    .addField("ID", usera.id, true)
    .addField("Username", usera.username, true)
    .addField("Status", usera.presence.status, true)
    .addField("Game", gameName, true)
    .addField("Joined Server", usera.joinedAt, true)
    .addField("Created", usera.createdAt, true)
    .addField("Bot", usera.bot, true)
    addField("Verified", value, true)
    .setTimestamp()
    .setColor(0x0f7fa6)
    .setThumbnail(usera.avatarURL);
    message.channel.send({embed});

    console.log("'UI' has been executed in the guild '" + message.guild.name + "' by " + message.author.username + " (" + message.author.id + ")");
}
});

运行此操作会出现'TypeError:无法读取未定义的属性'角色',并在let verifiedRole = usera.member.roles.find(val => val.name === 'verified');

中的'roles'一词下面带有箭头

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

如果您想使用角色,则无法使用user object,因为user object没有角色。您需要使用包含roles属性的Guild Member

要执行此操作,您只需将代码更改为:

let usera = message.mentions.members.first();

而不是message.mentions.users.first()

编辑:请注意,这意味着对于代码的用户部分,您必须放置usera.user...才能使其成为用户对象。