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

163
Vistas
cooldown for twitch bot

I wan't my bot to answer the "!help" command that people will type on chat but only once every 60 seconds

require('dotenv').config();

const tmi = require('tmi.js');

const client = new tmi.Client({
    channels: [ 'test' ],
    identity: {
        username: process.env.TWITCH_BOT_USERNAME,
        password: process.env.TWITCH_OATH_TOKEN
    }
});

client.connect();

client.on('message', (channel, tags, message, self) => {
    const send = message === "!help"

        if ( !send ) return; 
         {
            client.say(channel, `This message shouldn't send more then once eveyr 60 seconds`)
         }

    console.log(`${tags['display-name']}: ${message}`);
}); ```
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can use new Date() to get the current datetime. And new Date().getTime() gives you a number of milliseconds since epoch (since 1970-01-01 00:00:00 UTC).

// get the current time
const timeNow = new Date().getTime()

Every time you send a '!help' response you save the current time.

// the time of the last help message
let lastHelpTime = 0

// ... later when sending the message
// update the last help message time
lastHelpTime = timeNow

On every '!help' command you compare timeNow against the saved value lastHelpTime to avoid sending again within a minute. To allow a new response the time difference timeNow - lastHelpTime must be greater than 60 seconds, but because we have to compare milliseconds, it means the difference must be greater than 60 * 1000 milliseconds.

// check if at least 1 minute has elapsed since the last help message
if (timeNow - lastHelpTime > 60 * 1000) //...

Combined all together in your original code

// the time of the last help message
let lastHelpTime = 0

client.on('message', (channel, tags, message, self) => {
    const send = message === "!help"

    if ( !send ) return;

    // get the current time
    const timeNow = new Date().getTime()
    // check if at least 1 minute has elapsed since the last help message
    if (timeNow - lastHelpTime > 60 * 1000) {
        // update the last help message time
        lastHelpTime = timeNow
        // post the help message in chat
        client.say(channel, `This is the help message`)
    }

    console.log(`${tags['display-name']}: ${message}`);
});
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