简体   繁体   English

无法读取未定义的属性“角色”| 不和谐.js | javascript

[英]Cannot read property 'roles' of undefined | discord.js | javascript

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 + ")");
}
});

Running this comes up with 'TypeError: Cannot read property 'roles' of undefined' with an arrow under the word 'roles' in let verifiedRole = usera.member.roles.find(val => val.name === 'verified');运行它会出现 'TypeError: Cannot read property 'roles' of undefined' 在let verifiedRole = usera.member.roles.find(val => val.name === 'verified');

How do I fix this?我该如何解决?

If you want to work with roles, you can not use a user object , as the user object does not have roles.如果要使用角色,则不能使用user object ,因为user object没有角色。 You need to use Guild Member , which contains the roles property.您需要使用Guild Member ,其中包含角色属性。

To do this, you simply have to change your code to :为此,您只需将代码更改为:

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

as opposed to message.mentions.users.first() .而不是message.mentions.users.first()

Edit: Note that this means that for the user part of your code you'll have to put usera.user... in order to make it a user object.编辑:请注意,这意味着对于代码的用户部分,您必须放置usera.user...以使其成为用户对象。

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

相关问题 Discord.JS:TypeError:无法读取未定义的属性“角色” - Discord.JS:TypeError: Cannot read property 'roles' of undefined discord.js - 无法读取未定义的属性“角色” - discord.js - Cannot read property 'roles' of undefined 类型错误:无法读取未定义的属性“角色”|| Discord.js - TypeError: Cannot read property 'roles' of undefined || Discord.js Discord.JS TypeError:尝试添加角色时无法读取未定义的属性“角色” - Discord.JS TypeError: Cannot read property 'roles' of undefined while trying to add roles JavaScript(discord.js)TypeError:无法读取未定义的属性“startsWith” - JavaScript(discord.js) TypeError: Cannot read property 'startsWith' of undefined 类型错误:无法读取未定义 Discord.js javascript 的属性“添加” - TypeError: Cannot read property 'add' of undefined Discord.js javascript discord.js 如何赋予可调节的水平角色 - 错误无法读取未定义的属性“添加” - discord.js how to give adjustable leveled roles - Error Cannot read property 'add' of undefined discord.js 无法读取 null 的属性“角色” - discord.js cannot read property 'roles' of null Discord.js:Linkblocker TypeError:无法读取 null 的属性“角色” - Discord.js: Linkblocker TypeError: Cannot read property 'roles' of null discord.js Linkfilter TypeError:无法读取 null 的属性“角色” - discord.js Linkfilter TypeError: Cannot read property 'roles' of null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM