尝试使用 java 脚本制作不和谐机器人时出错

时间:2021-07-10 00:12:09

标签: discord.js bots

我正在尝试在我的 discord.js 机器人上实现我在视频中看到的权限处理程序,但此错误总是发生(无法读取未定义的属性“长度”)并且机器人停止。有人可以帮我解决这个问题吗?

    const cmd = args.shift().toLowerCase();

    const command = client.commands.get(cmd) || client.commands.find((a) => a.aliases && a.aliases.includes(cmd));

    const validPermissions = [
      "CREATE_INSTANT_INVITE",
      "KICK_MEMBERS",
      "BAN_MEMBERS",
      "ADMINISTRATOR",
      "MANAGE_CHANNELS",
      "MANAGE_GUILD",
      "ADD_REACTIONS",
      "VIEW_AUDIT_LOG",
      "PRIORITY_SPEAKER",
      "STREAM",
      "VIEW_CHANNEL",
      "SEND_MESSAGES",
      "SEND_TTS_MESSAGES",
      "MANAGE_MESSAGES",
      "EMBED_LINKS",
      "ATTACH_FILES",
      "READ_MESSAGE_HISTORY",
      "MENTION_EVERYONE",
      "USE_EXTERNAL_EMOJIS",
      "VIEW_GUILD_INSIGHTS",
      "CONNECT",
      "SPEAK",
      "MUTE_MEMBERS",
      "DEAFEN_MEMBERS",
      "MOVE_MEMBERS",
      "USE_VAD",
      "CHANGE_NICKNAME",
      "MANAGE_NICKNAMES",
      "MANAGE_ROLES",
      "MANAGE_WEBHOOKS",
      "MANAGE_EMOJIS",
    ]
  
    if(command.permission.length){
      let invalidPerms = []
      for(const perm of command.permissions){
        if(!validPermissions.includes(perm)){
          return message.channel.send(`Invalid Permissions \`${perm}\``);
        }
        if(!message.member.hasPermission(perm)){
          invalidPerms.push(perm);
        }
      }
      if (invalidPerms.length){
        return message.channel.send(`Missing Permissions: \`${invalidPerms}\``);
      }
    }```

1 个答案:

答案 0 :(得分:1)

“Cannot read property 'length' of undefined”意味着 Node JS 无法找到您试图获取 length 属性的对象(也称为 undefined)。在这种情况下,这意味着您的 permission 对象上的 command 属性不存在。

command 
     permission <- but this object doesn't exist! Node is now "lost", hence the error.
          length <- Node trying to find this property on above object

查看 documentation,我找不到任何涉及 command 对象的内容。

也许在您代码的另一部分中,您需要在 permission 对象上定义此 command 属性。

This question 似乎和您一样有问题。根据接受的答案,您是否通过 permissions 导出了 module.export 值?