Discord.JS Checking if user has role via ID strange issue

Viewed 299

I am trying to only allow someone to do things if their permissions are correct, and currently it is just giving me this error: TypeError: Cannot read properties of undefined (reading 'has')

Here is my code:

// @ts-check

const { SlashCommandBuilder } = require('@discordjs/builders');

const userParam = "user"

module.exports = {
    data: new SlashCommandBuilder()
        .setName('checkuserskins')
        .setDescription(`Check's the selected user's custom skin inventory.`)
        .addUserOption(option =>
            option.setName("user")
                .setDescription('The user to check')
                .setRequired(true)
        ),

    async execute(interaction) {
        const user = interaction.options.getUser('user');

        if (!user) {
            return interaction.reply({ content: `Could not find user ${user}`, ephemeral: true });
        }

        if (!interaction.user.roles.has("937401230316159076") || interaction.user.id !== user.id) {
            return interaction.reply({ content: `You don't have permission to do that!`, ephemeral: true });
        }

        return interaction.reply();
    },
};

Thanks.

1 Answers

A user has no guild information. You want to get the interaction.member which has all the member's informations related to the guild.

Also, the GuildMemberRoleManager has no has method, but its cache property does.

So you need to use it like so:

interaction.member.roles.cache.has("937401230316159076")
Related