Home > other >  TypeError: Cannot read properties of undefined (reading 'hasPermission')
TypeError: Cannot read properties of undefined (reading 'hasPermission')

Time:05-26

I am back again with again my ticket function. This time I am working on a close command. Whatever I try I always come back at the same errors:

TypeError: message.member.hasPermission is not a function

Anyone who can look at the code?

    const { MessageEmbed, Collection, Permissions } = require ('discord.js');

module.exports = {
    name: 'close',
    description: "closes the ticket",
    execute(message, args, client){
        if(!message.member.hasPermission("MANAGE_CHANNELS")) return message.channel.send("Only a moderator can end a ticket!")

        if(message.member.hasPermission("MANAGE_CHANNELS")) message.channel.delete()
    }
}

CodePudding user response:

Firstly, you should try reading the docs and see what properties GuildMember actually has, just a quick look through it, you should be able to find a .permissions, following from that, you'll get to this doc on the Permissions class. From there, you can see you can use the .has function. So your final code should be

message.member.permissions.has(Permissions.FLAGS.MANAGE_MESSAGES)

CodePudding user response:

Following your answer to my comment, it seems that you just put the arguments in the wrong order.

Your function is defined as

execute(client, message, args)

So you should call it with the arguments in the same order:

try {
  command.execute(client, message, args); // `client` is first, not last
} catch (error) {
  console.error(error);
  message.channel.send('There was an error trying to execute that command!');
} 

Following your comment on this answer, it fixed the initial issue. Now message.member has a value!

But now member.hasPermission does not seem to be a function. And indeed it is not: when you look at the documentation, there is no hasPermission method on a GuildMember.

So you have to figure out what exactly you need and look at the documentation to understand how to do it.

CodePudding user response:

this code will check if the member has the permission to MANAGE_CHANNELS and since .permissions.has() returns a boolean, it will either return true or false, if the user doesn't have such permission it will return a message that they cannot use the command, else if it will delete the channel...

if(!message.member.permissions.has(Permissions.FLAGS.MANAGE_CHANNELS)) { 
  return message.channel.send("Only a moderator can end a ticket!")
} else {
  return message.channel.delete()
}
  • Related