简体   繁体   English

Discord.JS:TypeError:无法读取未定义的属性“角色”

[英]Discord.JS:TypeError: Cannot read property 'roles' of undefined

This is the code.这是代码。

case 'updatesrole' :
    msg.react("✔️")
    msg.reply("✔️ Added updates role!, to remove, do !removeupdatesrole")
    let role = msg.guild.roles.cache.find(r => r.name === "Updates");

    var member1 = msg.author
    member1.roles.add(role)

case 'removeupdatesrole' :
    member1.roles.remove(role)
    }
})

I get this error.我收到这个错误。

TypeError: Cannot read property 'roles' of undefined类型错误:无法读取未定义的属性“角色”

Case statements are separate blocks of code Case 语句是单独的代码块

If I'm reading the code correctly (the formatting is a little weird) you have a switch statement and define a variable in one of the cases.如果我正确阅读代码(格式有点奇怪),您将拥有一个 switch 语句并在其中一种情况下定义一个变量。 If different case is triggered (in this case (haha), "removeupdatesrole") then that variable is never created and the error is thrown.如果触发了不同的情况(在这种情况下(哈哈),“removeupdatesrole”),则永远不会创建该变量并抛出错误。 In order to use the variable you either need to create it within the case like so:为了使用该变量,您需要在 case 中创建它,如下所示:

case 'removeupdatesrole' :
  var member1 = msg.member
  member1.roles.remove(role)
}

Or create it before the entire switch statement if multiple cases use it.或者,如果多个情况使用它,则在整个 switch 语句之前创建它。

Alternatively, you could simplify your code to this:或者,您可以将代码简化为:

case 'removeupdatesrole' :
  msg.member.roles.remove(role)
}

and never have to deal with the variable at all.并且根本不必处理变量。

PS: After you fix this, you'll have to do the same with the role variable. PS:修复此问题后,您必须对角色变量执行相同操作。 Declare it in the 'removeupdatesrole' case statement too.也在'removeupdatesrole' case 语句中声明它。

Edit: As Lioness100 points out in the comments, msg.member should be used instead of msg.author in order to get the user in the context of the Guild instead of Discord at large.编辑:正如 Lioness100 在评论中指出的那样,应该使用 msg.member 而不是 msg.author 以使用户处于 Guild 而不是 Discord 的上下文中。 I have updated the code snippets accordingly.我已经相应地更新了代码片段。

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

相关问题 类型错误:无法读取未定义的属性“角色”|| 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 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 discord.js - 无法读取未定义的属性“角色” - discord.js - Cannot read property 'roles' of undefined 无法读取未定义的属性“角色”| 不和谐.js | javascript - Cannot read property 'roles' of undefined | discord.js | javascript discord.js错误TypeError:无法读取未定义的属性“ voiceChannel” - discord.js Error TypeError: Cannot read property 'voiceChannel' of undefined discord.js TypeError:无法读取未定义的属性“名称” - discord.js TypeError: Cannot read property 'name' of undefined “ TypeError:无法读取未定义的属性'push'” discord.js - “TypeError: Cannot read property 'push' of undefined” discord.js 类型错误:无法使用 Discord.js 读取未定义的属性“集” - TypeError: Cannot read property 'set' of undefined with Discord.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM