Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

78
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda