-3

im adding moderation commands into my bot, but I dont want people to be able to kick admins, so i added some code, but it did not work well, when i do ,kick it tell me Please specify a member to kick, perfect, when i do ,kick @[user] it kicks the user, but when i try kicking an admin it gives me an error message saying Cannot read property 'has' of undefined here is the code

module.exports = {
    name: 'kick',
    description: '[DESCRIPTION]',
    async execute(Client, message, args, Discord) {
        const member = message.mentions.users.first();
        if(message.member.permissions.has("ADMINISTRATOR") || message.member.permissions.has("KICK_MEMBERS") || Client.users.cache.find(u => u.tag === 'Mr Tophat#3864').id){
            if(!member){
                message.channel.send("Please specify a member to kick");
            }
            else if(member.permissions.has("ADMINISTRATOR")){
                message.channel.send("You can't an admin");
            }
            else{
                if(member){
                    const memberTarget = message.guild.members.cache.get(member.id);
                    memberTarget.kick();
                    message.channel.send("User has been kicked!");
                }
            }
        }
    }
}
Mohid Mushtaq
  • 31
  • 1
  • 7
  • 1
    You're trying to access `permissions` off a `User`. [What's the difference between a User and a GuildMember](https://stackoverflow.com/questions/63979076/what-is-the-difference-between-a-user-and-a-guildmember-in-discord-js) – Lioness100 Apr 28 '21 at 12:43
  • i just did a simple fix, since that is the only time i get an error, i just made it so when i do get that error it sends a message saying you cant kick an admin – Mohid Mushtaq Apr 28 '21 at 12:48
  • 2
    The simple fix is to change `message.mentions.users.first()` to `message.mentions.members.first()` as mentioned in the post I linked. This way `member` will *actually* be a `GuildMember` and you won't get any error. – Lioness100 Apr 28 '21 at 12:50

1 Answers1

2

You are getting this error because you are trying to access permissions of a user instead of a guildMember. Declare your member like this:

const member = message.mentions.members.first();

The difference is, that a user is the global discord user and a guildMember is the user on a specific server


Reference

Toasty
  • 1,850
  • 1
  • 6
  • 21