Home > other >  DISCORD.JS V13 TypeError: Cannot read property 'has' of undefined
DISCORD.JS V13 TypeError: Cannot read property 'has' of undefined

Time:11-22

I'm doing a bot discord, I was trying to check if member mentioned has permissions but I have this problem:

if(member.permissions.has("ADMINISTRATOR")) return message.reply({content: ["no"]})
                      ^

TypeError: Cannot read property 'has' of undefined

this is my code:

const { Client, Message } = require("discord.js");

module.exports = {
    name: "user",
    description: "comando a risposta",
    aliases: ["boop", "test"],
    /** 
     * @param {Client} client 
     * @param {Message} message 
     * @param {String[]} args 
     */
    run: async (client, message, args) => {
        const member = message.mentions.users.first();
        if(member.permissions.has("ADMINISTRATOR")) return message.reply({content: ["he has the admin"]})
        message.reply(`${member.tag}`)
    }
}

CodePudding user response:

You are getting a User object which doesn't have .permissions. Get the GuildMember object instead with message.mentions.members.first()

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

Users are not the same as GuildMembers: See What is the difference between a User and a GuildMember in discord.js?

CodePudding user response:

somehow member.permissions is undefined , maybe you mispelled it wrong , log member to console

CodePudding user response:

Okay so essentially this message is saying that discord.js is not recognising that a user has been mentioned in the message. The .first() methods returns undefined if the users collection is empty. So I recommend handling member possibly being undefined if no one has been mentioned in the message.

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

if (member === undefined) return message.reply({content: ["no user mentioned"]})

if(member.permissions.has("ADMINISTRATOR")) return message.reply({content: ["he has the admin"]})
message.reply(`${member.tag}`)

CodePudding user response:

const { Client, Message } = require("discord.js");

module.exports = {
    name: "user",
    description: "comando a risposta",
    aliases: ["boop", "test"],
    /** 
     * @param {Client} client 
     * @param {Message} message 
     * @param {String[]} args 
     */
    run: async (client, message, args) => {
        const member = message.mentions.members.first()
        if(member.permissions.has("ADMINISTRATOR")) {
            message.reply(`${member.tag} ha l'admin`)
        }
        if(!member.permissions.has("ADMINISTRATOR")) {
            message.reply(`${member.tag} non ha l'admin`)
        }
    }
   
  • Related