Search code examples
htmltwitch

Adding a Cooldown feature in Auto-Reply Script For Twitch


I saw this post on reddit (Auto-Reply Script on other Stream) and I find the solution of @puerdon very useful, but I wonder if it's possible to add a cooldown for the auto-reply in the script/html code (You can check the original html code in the reddit post above).

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title></title>
</head>

<body>
    <script src="https://github.com/tmijs/tmi.js/releases/download/v1.8.5/tmi.min.js"></script>

    <script> 
        const client = new tmi.Client({
            options: { 
                debug: false,
                skipMembership: true, // 不接收 JOIN/PART 訊息
                skipUpdatingEmotesets: true,
            },
            connection: {
                reconnect: true,
                secure: true
            },
            identity: {
                username: 'your_twitch_username',            // [TODO]: input your Twitch username 
                password: 'genereated_oath_password'         // [TODO]: input the genereated oath password
                                                             // 1. go to https://twitchapps.com/tmi/
                                                             // 2. click "Connect"
                                                             // 3. You will get a password beginning with "oath..."
                                                             // 4. Copy the whole password and paste inside the 'genereated_oath_password'
            },
            channels: [ 'type_the_channel_you_want_to_listen_to' ] // [TODO]: input the channel name you want to listen to
        });

        let lastSchedule = undefined;
        function F(client, channel, message) {
            let currentMoment = new Date();
            function f() {
                client.say(channel, message);
            }
            if ((!lastSchedule) || (lastSchedule < new Date(lastSchedule - 120000))) {
                lastSchedule = currentMoment;
                f();
            } else {
                lastSchedule.setMinutes(lastSchedule.getMinutes() + 2);
                setTimeout(f, lastSchedule - currentMoment);
            }
        }
        client.connect().catch(console.error);

        client.on('message', (channel, tags, message, self) => {
         
            // this part is to skip the message sent by you,
            // or it will be prone to cause infinite loop
            if (self) {
                console.log(self);
                return;
            }

            // Here is the example of detecting a "hello" in a message
            // I first turn message to lower case and then check if the message includes "hello" in it
            // This can then detect "Hello", "hELLO", "HELLO" ... etc. variation of capitalization in the message
            if (message.toLowerCase().includes("hello")) {
                
                // if the condition above is met, then send "Hi" in the chat
                F(channel, client, "Hi!");
 
            }

        });


    </script>
</body>

</html>

Let's say, after sending the first "hello" by a chatter, I want to make the script to send the auto-reply "hi" once and will only reply again in the next 2 or 3 mins. In this way, it won't spam the same message if there's too many hellos in such a short period of time.

PS. I sent @puerdon a DM to help me with my request since I'm not a coder but he hasn't replied to me for a long while (maybe an abandoned account by now) so I thought of sharing it here if anyone knows how to add this feature.

Any helpful insights will be highly appreciated. Thank you very much!

Add some lines of code that will allow me to add a cooldown or buffer time between the automated messages.


Solution

  • If the auto-reply is the "hi" sent in response, this is how you can delay it:

                if (message.toLowerCase().includes("hello")) {
                    
                    // if the condition above is met, then send "hello" in the chat
                    setTimeout(function() {client.say(channel, "Hi!")}, 1000 * 2 * 60);
                }
    

    Or, if you want to make sure that a function, let's call it f cannot be executed twice in 2 minutes and postpone its call, then you can do something like this:

    let lastSchedule = undefined;
    function F(message) {
        let currentMoment = new Date();
        function f() {
            console.log(message);
        }
        if ((!lastSchedule) || (lastSchedule < new Date(lastSchedule - 120000))) {
            lastSchedule = currentMoment;
            f();
        } else {
            lastSchedule.setMinutes(lastSchedule.getMinutes() + 2);
            setTimeout(f, lastSchedule - currentMoment);
        }
    }
    
    F("a");F("b");F("c");F("d");

    Adding the code to yours:

    let lastSchedule = undefined;
    function F(channel, client, message) {
        let currentMoment = new Date();
        function f() {
            client.say(channel, message);
        }
        if ((!lastSchedule) || (lastSchedule < new Date(lastSchedule - 120000))) {
            lastSchedule = currentMoment;
            f();
        } else {
            lastSchedule.setMinutes(lastSchedule.getMinutes() + 2);
            setTimeout(f, lastSchedule - currentMoment);
        }
    }
    

    you can define F just before

    client.connect().catch(console.error);
    

    for example.

    You can call F by replacing:

    client.say(channel, "Hi!");
    

    with

    F(channel, client, "Hi!");