admin管理员组

文章数量:1399788

I saw the post on how to add a cooldown for a twitch bot (cooldown for twitch bot) and the response given by @Ma3x is just what I needed (pasting it here for reference):

// 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}`);
});

However, it detects the exact word only. How do I make it detect any variation of the word "!help" like "!HELP", "!HeLp" or when it's part of the message like "!help bot chat"?

Thank you!

I saw the post on how to add a cooldown for a twitch bot (cooldown for twitch bot) and the response given by @Ma3x is just what I needed (pasting it here for reference):

// 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}`);
});

However, it detects the exact word only. How do I make it detect any variation of the word "!help" like "!HELP", "!HeLp" or when it's part of the message like "!help bot chat"?

Thank you!

Share Improve this question asked Mar 24 at 21:19 user26604864user26604864 54 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Use the JavaScript .toLowerCase() function to convert everything in the message to lower-case and .includes() to check for the matching string in the message when comparing the message to your desired text (read more toLowerCase and includes):

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

client.on('message', (channel, tags, message, self) => {
    const send = message.toLowerCase().includes("!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}`);
});

Example:

console.log("!HELP".toLowerCase().includes("!help"))
console.log("!HeLp".toLowerCase().includes("!help"))
console.log("!help".toLowerCase().includes("!help"))
console.log("!hElp mE Bot".toLowerCase().includes("!help"))

Note: This only checks to see if "!help" is part of the message, and not the -first- part of the message, that would require different logic.

本文标签: htmlAdding variation on trigger word for twitch botStack Overflow