简体   繁体   中英

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

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}`)
    }
}

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

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}`)

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()

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

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`)
        }
    }
   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM