Home > database >  Discord.js block users with an ID from using certain commands
Discord.js block users with an ID from using certain commands

Time:07-31

I'm trying to make it when someone's user ID matches with a variable then it'll react with ⛔. This is working for all of my other commands but whenever I use it for this one it reacts with it but then let's them run the command. Any ideas? Also sometimes the message is sent twice.

client.on('message', async message => {
    // Check if the user who sent the message is in the list 
    if ((message.author.id) === bannedid) {
        // If user is in list, then check if message matches a command
             if (message.content === 'hi') {
           await message.react('⛔');
             } 
        } else if (message.content === 'hi') {
           himessage(message);

            function himessage(message) {
                var messages = ['hi uwu', 'owo', 'no', 'stinky', 'amongus', 'The FitnessGram PACER Test is a multistage aerobic capacity test that progressively gets more difficult as it continues. The test is used to measure a students aerobic capacity as part of the FitnessGram assessment. Students run back and forth as many times as they can, each lap signaled by a beep sound. The test get progressively faster as it continues until the student reaches their max lap score. The PACER Test score is combined in the FitnessGram software with scores for muscular strength, endurance, flexibility and body composition to determine whether a student is in the Healthy Fitness Zone™ or the Needs Improvement Zone™.'];
                var cheese = Math.floor(Math.random() * messages.length)

                message.channel.send(messages[cheese]);
        }
    }
});

CodePudding user response:

EDIT:

To answer your problem, the reason why your code keeps running is because your if(message.content === 'hi') and else if(message.content === 'hi') will both return true.

To prevent that, you can stop the code execution after reacting:

return message.react('⛔');

or modify your else if statement:

else if((message.author.id) != bannedid && message.content === 'hi') {
// your code here.
}

This here was my original comment, where I mistakenly understood that your code wasn't working, I will let it here as it is still a viable way of doing it:

Assuming bannedid is an Array of IDs, you can use Array.prototype.some() to check all the elements of the array.

if(bannedid.some(element => message.author.id === element) && message.content === 'hi') {
// use return here so that the code will stop executing after reacting with the emoji.
return message.react('⛔');
} else {
// rest of your code
}
  • Related