I'm very new to coding so it's probably horrible. Here's the code
let userCooldown = {};
client.on("message", message => {
if (message.content.includes('ping'))
if (userCooldown[message.author.id]) {
userCooldown[message.author.id] = false;
message.reply('Pong');
setTimeout(() => {
userCooldown[message.author.id] = true;
}, 5000) // 5 sec
}
})
the plan would be for the bot not to respond to the message for 5 seconds until it's written again
Not sure why you set the cooldown to false if it's true when someone sends a message and why you set it to true after five seconds.
If your userCooldown includes the users who currently can't execute the function, you need to check if they are already on that list. If they are, don't execute the function. If they are not, execute the function, add them to the list and use setTimeout to remove them after five seconds. Try to run the snippet below:
let userCooldown = {};
function onMessage(message) {
console.log(`onMessage fired with message: "${message.content}"`);
if (message.content.includes('ping')) {
// if user can't execute the fucntion, just exit
if (userCooldown[message.author.id]) return;
// if they can, add them to userCooldown
userCooldown[message.author.id] = true;
console.log('Pong!');
// and remove them after 5 seconds
setTimeout(() => {
userCooldown[message.author.id] = false;
}, 5000);
}
}
// run it every second to test it :)
let message = { content: '!ping', author: { id: 'authorID' } };
setInterval(() => onMessage(message), 2000)