[英]"TypeError: Cannot read property 'id' of undefined" Discord.js error
I am coding a Discord bot with Discord.js. I have the code below, but when I run the command I get the error: "person.roles.remove(mainrole.id); TypeError: Cannot read property 'id' of undefined"
我正在用 Discord.js 编码一个 Discord 机器人。我有下面的代码,但是当我运行命令时我得到错误:
"person.roles.remove(mainrole.id); TypeError: Cannot read property 'id' of undefined"
I don't know how or what exactly to define.我不知道如何定义或定义什么。 Any help would be appreciated, Thanks in advance.
任何帮助将不胜感激,在此先感谢。
bot.on('message', msg => {
let args = msg.content.substring(PREFIX.length).split(" ");
switch (args[0]) {
case 'mute':
let person = msg.guild.member(msg.mentions.users.first() || msg.guild.member.cache.find(args[1]))
if (!person) {
return msg.reply("Couldn't find that user");
}
let mainrole = msg.guild.roles.cache.find(role => role.name == "Member");
let muterole = msg.guild.roles.cache.find(role => role.name === "muted");
if (!muterole) {
return msg.reply("Couldn't find mute role");
}
let time = args[2];
if (!time){
return msg.reply("Please specify a time")
}
person.roles.remove(mainrole.id);
person.roles.add(muterole.id);
msg.channel.send(`@${person.user.tag} has been muted for @${ms(ms(time))}`);
setTimeout(function (){
person.roles.add(mainrole.id);
person.roles.remove(muterole.id);
msg.channel.send(`@${person.user.tag} has been unmuted`)
}, ms(time));
break;
}
})
Seems like you are not getting this condition fulfilled, role => role.name == "Member"
that's why mainrole
is undefined .似乎您没有满足此条件,
role => role.name == "Member"
这就是mainrole
未定义的原因。
You can possibly do to avoid error is:您可以做的是避免错误:
if(mainrole && mainrole.id) {
person.roles.remove(mainrole.id);
person.roles.add(muterole.id);
msg.channel.send(`@${person.user.tag} has been muted for @${ms(ms(time))}`);
setTimeout(function(){
person.roles.add(mainrole.id);
person.roles.remove(muterole.id);
msg.channel.send(`@${person.user.tag} has been unmuted`)
}, ms(time));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.