简体   繁体   English

“TypeError:无法读取未定义的属性‘id’”Discord.js 错误

[英]"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.

相关问题 类型错误:无法读取未定义 Discord.js 的属性“id” - TypeError: Cannot read property 'id' of undefined Discord.js TypeError:无法读取 Discord.js 中未定义的属性“id” - TypeError: Cannot read property 'id' of undefined in Discord.js Discord.js 错误 - 类型错误:无法读取未定义的属性“执行” - Discord.js Error - TypeError: Cannot read property 'execute' of undefined discord.js错误TypeError:无法读取未定义的属性“ voiceChannel” - discord.js Error TypeError: Cannot read property 'voiceChannel' of undefined 类型错误:无法读取未定义的 Discord.js 错误的属性“get” - TypeError: Cannot read property 'get' of undefined Discord.js Error Discord.js TypeError:无法读取未定义的属性“id”-channel.id - Discord.js TypeError: Cannot read property 'id' of undefined - channel.id 类型错误:无法读取未定义 Discord.js javascript 的属性“添加” - TypeError: Cannot read property 'add' of undefined Discord.js javascript 类型错误:无法读取未定义的属性 'has' // Discord.js - TypeError: Cannot read property 'has' of undefined // Discord.js 遇到 TypeError:无法读取 Discord.JS 中未定义的属性“0” - Encountering TypeError: Cannot read property '0' of undefined in Discord.JS Discord.JS UnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性“startsWith” - Discord.JS UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'startsWith' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM