[Tutorial] Timeouts on commands and messages including a specific item

Editing this to say, don’t just burst through just to get the code, but instead read everything carefully to better understand what we are doing.

Have you ever wanted to make a command that people can’t spam?
Or maybe allow someone to swear, but only once every hour? Or any other word in general?
If so, then this is a tutorial for you!

So first off, let’s start with a command with a 5 seconds timeout.

The first you got to do is to make new command file in your command handler and set up the base code.
In my case, that is

const Discord = require('discord.js');

module.exports.run = async(client, message, args) => {

}

Now we need to make the timeout.
To do that, we are going to add a new Set(); and let’s name it cooldownSet
Make sure to put it above the module.exports.run part

const Discord = require('discord.js');
const cooldownSet = new Set();

module.exports.run = async(client, message, args) => {

}

Now we want to add the UserID to the cooldownSet and remove them when the time is up.

To do so, we are going to do an if and else statement and use a setTimeout function.

const Discord = require('discord.js');
const cooldownSet = new Set();

module.exports.run = async(client, message, args) => {
//Checking if they are in the set.
if (cooldownSet.has(message.author.id)) {
//They are in the set, so we will return a reply to them and just not doing anything else.
return message.reply('Please wait 5 seconds between each time you do this command');
}
else {
//They are not in the set, so we will add them to it and remove them after 5 seconds
cooldownSet.add(message.author.id);
setTimeout(() => { //The (() may not seem necessary, but it is.
cooldownSet.delete(message.author.id);
}, 5000); //5000 mili seconds which is the same as 5 seconds.
}
//Put your code here

}

And that is how you do it for commands!
Now we are going to try it for normal messages, which actually is not too much different.
So first we got to make the new Set. Let’s call this one swearTimeout.
And since this is just a normal message, this has to go in a event in the main file.

const swearTimeout = new Set();

client.on('message', message => {

});

Now we got to make it know if the message includes a swear word.

const swearTimeout = new Set();

client.on('message', message => {
if (message.content.includes('Swear Word')) {

}
});

If you are going to do multiple swear words, or any other message you want, you can just do something like

const swearTimeout = new Set();

client.on('message', message => {
if (message.content.includes('Swear Word 1') || message.content.includes('Swear Word 2') || message.content.includes('Swear Word 3')) {

}
});

Anyways, now we are just going to do the same as with a command.

const swearTimeout = new Set();

client.on('message', message => {
if (message.content.includes('Swear Word')) {
if (swearTimeout.has(message.author.id) {
message.reply('You can only swear once every hour.');
message.delete();
}
else {
swearTimeout.add(message.author.id);
setTimeout(() => {
swearTimeout.delete(message.author.id);
}, 3600000);
}
}
});

So now we got a working timeout system set up!
If you find this helpful and maybe want some more tutorials, you can either reply to this thread or message me and I sure will take it into consideration!
Hope this helped you,

Thats pretty cool! For the swears I would suggest doing something like this:

let swears = ['swear1', 'swear 2'];

if (swears.some((word) => message.content.toLowerCase().indexOf(word.toLowerCase()) >= 0)) {
  if (swearTimeout.has(message.author.id)) {
    message.reply('You can only swear once every hour.');
    message.delete();
  } else {
    swearTimeout.add(message.author.id);
    setTimeout(() => {
      swearTimeout.delete(message.author.id);
    }, 3600000);
  }
}
1 Like

Yeah, I am currently in class, so I just took something that I quickly got into my mind.