How to implement a cooldown for my Twitch bot's commands

I’m developing a Twitch bot and I need assistance with adding a delay between its responses. Specifically, I want the bot to reply to the “!help” command that users type in the chat, but I want to set it so that it only replies once every 60 seconds to avoid flooding the chat.

Here’s what I have so far:

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) => {
    const isHelpCommand = message === "!help";

    if (!isHelpCommand) return;
    {
        client.say(channel, `This message should be sent only once every 60 seconds`);
    }

    console.log(`${tags['display-name']}: ${message}`);
});

The bot currently responds every time the command is called. How do I implement a cooldown of 60 seconds?

Consider using a Map to manage cooldowns for commands. This approach is more scalable should you wish to add more commands in the future. Here’s an example modification for your code:

const commandCooldowns = new Map();

client.on('message', (channel, tags, message) => {
    const isHelpCommand = message === "!help";
    
    if (!isHelpCommand) return;
    
    const now = Date.now();
    const cooldownAmount = 60 * 1000; // 60 seconds
    
    if (commandCooldowns.has('help')) {
        const expirationTime = commandCooldowns.get('help') + cooldownAmount;
        
        if (now < expirationTime) {
            return; // Still on cooldown
        }
    }
    
    commandCooldowns.set('help', now);
    client.say(channel, `This message should be sent only once every 60 seconds`);
    
    console.log(`${tags['display-name']}: ${message}`);
});

This method makes it easier to manage multiple commands with varying cooldowns in the future.

quick fix - add a timestamp variable outside your event listener and compare it each time the command runs. something like let lastUsed = 0 then check if(Date.now() - lastUsed > 60000) before responding. Don’t forget to update lastUsed after sending the message though.

To add a cooldown to your Twitch bot commands, store when the command was last used and check if enough time has passed before responding again. Here’s how to modify your code:

let lastHelpTime = 0;
const cooldownMs = 60000; // 60 seconds in milliseconds

client.on('message', (channel, tags, message) => {
    const isHelpCommand = message === "!help";
    
    if (!isHelpCommand) return;
    
    const currentTime = Date.now();
    
    if (currentTime - lastHelpTime >= cooldownMs) {
        client.say(channel, `This message should be sent only once every 60 seconds`);
        lastHelpTime = currentTime;
    }
    
    console.log(`${tags['display-name']}: ${message}`);
});

This uses Date.now() to get the current timestamp and checks if enough time has passed since the last command execution.