mansverychill
mansverychill

Reputation: 3

Cannot read property 'hasPermission' of null

Before i start I want to say this, I am in NO WAY a developer/scripter/coder or whatever you wanna call it. Anyways, i am trying to make a kick command and it works but it says: Cannot read property 'hasPermission' of null. And I'm worried it's letting anyone kick people, even though it hasn't happened yet, that's what it seems like. Here is the code:

client.on('message', message => {
    if(message.member.hasPermission("KICK_MEMBERS"))
        if(message.content.startsWith(`${prefix}kick`)) {
            let member = message.mentions.members.first();
            member.kick().then(member)
                message.channel.send(member.displayName + " was kicked.")
        }
})

Upvotes: 0

Views: 797

Answers (1)

Herms
Herms

Reputation: 38798

You're calling hasPermission on message.member. This suggests that message.member is null, at least some of the times this is called.

You can try changing to:

    if(message.member && message.member.hasPermission("KICK_MEMBERS")) {

and that should make sure message.member has a value before you try to dereference it. If there's no member then it won't do anything.

I'm not sure how the discord APIs work so I don't know if this is an expected state things could be in (or if member is the right field to be looking at), so if this is always happening then you should make sure that's correct.

Also, I suspect this bit is wrong:

            member.kick().then(member)
                message.channel.send(member.displayName + " was kicked.")

and should probably be:

            member.kick().then(member => 
                message.channel.send(member.displayName + " was kicked."));

Upvotes: 1

Related Questions