Freddie Robinson
Freddie Robinson

Reputation: 11

"Cannot read property 'user' of undefined" discord.js issue

I'm having an issue with my code for my discord bot at the moment I am trying to make my bot have the ability to kick users from the server.

Every time I try my code, the error shows:

Cannot read property 'user' of undefined

Here is the code:

bot.on('message', msg => {

    let args = msg.content.substring(PREFIX.length).split(" ")

    switch (args[0]) {
        case 'kick':

            const user = msg.mention.user.first();

            if (user) {
                const member = member.guild.member(user);

                if (member){
                    member.kick('You have been kicked from the server').then(() =>{
                        msg.reply(`The user ${user.tag} has been kick from the server`);
                    }).catch(err => {
                        msg.reply(`I was unable to kick this user`);
                        console.log(err);
                    });
                } else {
                    msg.reply(`This user isn\'t in the server`)
                }
            } else {
                msg.reply(`You need to specify a person`)
            }

            break;

    }
})

Upvotes: 1

Views: 196

Answers (1)

Ahmad Habib
Ahmad Habib

Reputation: 2384

The reason is that it supports mentions not mention

bot.on('message', msg => {

let args = msg.content.substring(PREFIX.length).split(" ")

switch (args[0]) {
    case 'kick':

        const user = msg.mentions.user.first();

        if (user) {
            const member = member.guild.member(user);

            if (member){
                member.kick('You have been kicked from the server').then(() =>{
                    msg.reply(`The user ${user.tag} has been kick from the server`);
                }).catch(err => {
                    msg.reply(`I was unable to kick this user`);
                    console.log(err);
                });
            } else {
                msg.reply(`This user isn\'t in the server`)
            }
        } else {
            msg.reply(`You need to specify a person`)
        }

        break;

}
})

Discord documentation

Also see: Discord official documentation

Upvotes: 1

Related Questions