Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

85
Views
Discord.js Ping Pong command with cooldown

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

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!