skip to Main Content

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

        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
                client.say(channel, "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.

2

Answers


  1. One concept that would implement a "cooldown" effect is called "debouncing."

    Here’s a simple debounce function:

    function debounce(func, delay) {
      let timeoutId;
    
      return function(...args) {
        clearTimeout(timeoutId);
    
        timeoutId = setTimeout(() => {
          func.apply(this, args);
        }, delay);
      };
    }
    

    In your case, you’d encapsulate your functionality into a method like maybeSayHello() and pass that to this debounce function, along with your cooldown in milliseconds.

    Login or Signup to reply.
  2. 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 - 12000))) {
            lastSchedule = currentMoment;
            f();
        } else {
            lastSchedule.setMinutes(lastSchedule.getMinutes() + 2);
            setTimeout(f, lastSchedule - currentMoment);
        }
    }
    
    F("a");F("b");F("c");F("d");
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search