I’m working on a Twitch bot and found some code that handles cooldowns for commands. The code works great but only responds to exact matches. I want to modify it so the bot can detect commands regardless of case sensitivity and even when the command appears within a longer message.
// track when command was last used
let lastCommandTime = 0
bot.on('chat', (room, userInfo, text, self) => {
const shouldRespond = text === "!info"
if (!shouldRespond) return;
// get current timestamp
const currentTime = new Date().getTime()
// verify 60 seconds have passed since last command
if (currentTime - lastCommandTime > 60 * 1000) {
// set new timestamp
lastCommandTime = currentTime
// send response to chat
bot.say(room, `Here is the information you requested`)
}
console.log(`${userInfo['display-name']}: ${text}`);
});
Right now this only works for the exact text “!info”. How can I make it work with different cases like “!INFO”, “!InFo” or when someone types “!info please help me”? Any suggestions would be appreciated.
Handling command variations manually is a nightmare. Trust me - I’ve built multiple Twitch bots for different teams and learned this lesson the hard way.
Yeah, you could hack together toLowerCase() and startsWith() like others mentioned. But then what? More commands mean more mess. Typos break everything. Different cooldowns per command or user? Good luck with that spaghetti code.
I ditched manual parsing and automated the whole thing. Now I don’t write custom logic for every bot - just set up flows that handle command detection, cooldowns, and responses automatically.
Best part? You can add command variations without coding. Want “!info”, “!information”, “!help” doing the same thing? Throw them in your trigger list. Different cooldowns for mods vs regular users? Configure it once, done.
Bonus: automatic logging to spreadsheets, notifications when specific commands fire, database connections for user data - all without extra code.
Your current setup works for basic stuff, but automation actually scales when your bot grows.
I pulled the command parsing into its own function - works great. Something like function parseCommand(text) { return text.trim().toLowerCase().split(/\s+/)[0]; } then just const shouldRespond = parseCommand(text) === '!info'. Handles whitespace and case issues automatically, plus it’s super easy to add arguments later. Way cleaner than cramming regex or string methods everywhere, and you can reuse it for other commands without copy-pasting. The regex split catches those extra spaces people always accidentally type.
Replace const shouldRespond = text === "!info" with const shouldRespond = text.toLowerCase().startsWith('!info'). This converts the message to lowercase and checks if it starts with your command, handling case variations and allowing extra text after the command. I’ve used this method in several chat bots and it works reliably. The startsWith() method beats includes() because it won’t trigger on words like “information” elsewhere in the message, but still lets commands like “!info please help” work properly.
You could also use regex for better control over pattern matching. Try const shouldRespond = /^!info\b/i.test(text) instead. The ^ makes sure the command starts at the beginning, \b is a word boundary so it won’t match “information” or similar words, and the i flag makes it case insensitive. I ran into false triggers when users mentioned information in regular chat - this fixed it. Regex might feel like overkill at first, but it’s super useful once you add more commands or need complex matching rules for your bot.
u can try text.toLowerCase().includes('!info') for handling case variations n even match partial commands. just be cautious it dont trigger for words like ‘information’. this makes sure your bot responds to all variations n full msg including the command.
you could also split the message first - const command = text.split(' ')[0].toLowerCase() then check if command === '!info'. works great when you add commands with parameters later and stays simple without regex headaches.