0
votes

I'm trying to implement a permission handler that I saw in a video, on my discord.js bot, but this error always happens (Cannot read property 'length' of undefined) and the bot stops. Can someone help me solve this problem?

    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

1 Answers

0
votes

"Cannot read property 'length' of undefined" means that Node JS can't find the object that you're trying to get the length property on (also known as undefined). In this case, it means that the permission property on your command object does not exist.

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

Looking in the documentation, I could not find anything referring to a command object.

Perhaps in another section of your code you need to define this permission property on your command object.

This question seems to have the issue as you. As per the accepted answer, did you export the permissions value through module.export?