定义变量后无法读取未定义的属性“查找”

时间:2019-05-14 05:30:22

标签: node.js discord.js

我收到一个TypeError,但它不应该是因为我认为我已经正确定义了变量(请纠正我!)

已经四处寻找并找到它;但是,即使有了“修复程序”,它仍然给了我错误。对于我肯定犯的错误,我深表歉意!

var parts = message.content.split(" ");
if (message.content.startsWith(prefix + "sr")) {
  const servguild = message.guild.id;
  const member = message.mentions.members.first();
  const role = servguild.roles.find('name', parts[2]);
  member.addRole(role);
}

1 个答案:

答案 0 :(得分:3)

您已将servguild定义为message.guild.id;,并试图访问其roles属性,因此尝试访问message.guild.id.roles。但是,roles属性仅存在于guild属性上,而不存在于guild.id属性上。

Discord Developer Portal Docs for Guild中,您可以看到公会的定义如下:

{
    "id": "41771983423143937",
    "application_id": null,
    "name": "Discord Developers",
    "icon": "86e39f7ae3307e811784e2ffd11a7310",
    "splash": null,
    "owner_id": "80351110224678912",
    "region": "us-east",
    "afk_channel_id": "42072017402331136",
    "afk_timeout": 300,
    "embed_enabled": true,
    "embed_channel_id": "41771983444115456",
    "verification_level": 1,
    "default_message_notifications": 0,
    "explicit_content_filter": 0,
    "mfa_level": 0,
    "widget_enabled": false,
    "widget_channel_id": "41771983423143937",
    "roles": [],
    "emojis": [],
    "features": ["INVITE_SPLASH"],
    "unavailable": false
}

要修复您的代码,请将const servguild = message.guild.id;更改为const servguild = message.guild;