Search code examples
node.jsdiscorddiscord.js

Check if value is equal to another value


What i am after:

I am developing a discord.js (V14) bot at the moment. The idea is the following. If a member executes the following / command:

/mute target:@member time:1d reason:spamming

Check if target:@member is equal to the bot it self.

My issue:

The below code should in theory (to my knownledge) be enough to check if the target:@member is equal to discord bot. However, my code completely skips that step. Even if i verify via console.log that both values are equal.

The Code:

10258xxxx - console.log(user.id);

10258xxxx - console.log(interaction.guild.members.me.id);

module.exports = {
    data: new SlashCommandBuilder()
        .setName("mute")
        .setDescription("Mute a member.")
        .addUserOption(option =>
            option.setName("target")
                .setDescription("Select the user you wish to mute.")
        )
        .addStringOption(option =>
            option.setName("time")
                .setDescription("How long should the mute last?")
        )
        .addStringOption(option =>
            option.setName("reason")
                .setDescription("What is the reason of the mute?")
        ),

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

        if (user.id === interaction.guild.members.me.id){
            return interaction.reply("I can't mute my self.");
        }

        return interaction.reply("Success");
    }

Solution

  • Not completely familiar with discord, but it looks like you might need to add the await keyword

    return await interaction.reply("I can't mute my self.");
    

    Without going into detail of how async await works, what you could test is the following to see that the line is running:

    if (user.id === interaction.guild.members.me.id){
        console.log("Can you see me?");
        return interaction.reply("I can't mute my self.");
    }
    

    If you can see the log, what is happening is the code stops running before waiting for the async function to finish. If you want to understand this better, learn more about async await!