Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

TypeError: Cannot read properties of undefined (reading 'has')

I’m trying to do a clear command on Discord.js v14, but it keeps telling this error:

TypeError: Cannot read properties of undefined (reading 'has')

Here’s the code:

const { SlashCommandBuilder, Embed, EmbedBuilder } = require('discord.js');

module.exports = {
    data: new SlashCommandBuilder()
        .setName('clear')
        .setDescription("Delete messages")
        .addIntegerOption(option => option.setName('messages').setDescription('Messages to delete').setRequired(true)),
    async execute(interaction) {
        const messages = interaction.options.getInteger('messages');
        
        if(!interaction.user.id.permissions.has(PermissionsBitField.Flags.ManageMessages)){
            interaction.reply({ content: "You can't send messages!", ephemeral: true} )
        }
        
    }
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

interaction.user.id gives you a string containing the id of the user who ran the slash command. To check for the permissions, you need the GuildMember data of the user which you can access by using interaction.member. So, your fixed code would look like this:

const { SlashCommandBuilder, Embed, EmbedBuilder } = require('discord.js');

module.exports = {
    data: new SlashCommandBuilder()
        .setName('clear')
        .setDescription("Delete messages")
        .addIntegerOption(option => option.setName('messages').setDescription('Messages to delete').setRequired(true)),
    async execute(interaction) {
        const messages = interaction.options.getInteger('messages');
        
        if(!interaction.member.permissions.has(PermissionsBitField.Flags.ManageMessages)){
            interaction.reply({ content: "You can't send messages!", ephemeral: true} )
        }
        
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading