Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

77
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda