instafluff / ComfyJS

Comfiest Twitch Chat Library for JavaScript | NodeJS + Browser Support
MIT License
405 stars 44 forks source link

Say-ing multiple times in onCommand not working #33

Closed Eric-Hacker closed 3 years ago

Eric-Hacker commented 3 years ago

I would like a bot to be able to reply multiple lines to a command. That does not seem to work. In the below example code, only the first reply to test goes to the channel.

Example code (sanitized):

<html>
  <head>
    <script src="https://cdn.jsdelivr.net/npm/comfy.js@latest/dist/comfy.min.js"></script>
  </head>
  <body>
    <script type="text/javascript">
      var irc = "BroadcasterAccount";

      ComfyJS.onCommand = ( user, command, message, flags, extra ) => {
        if( (flags.broadcaster || flags.mod) && command === "test" ) {
          console.log( "!test was typed in chat" );
          ComfyJS.Say( "replying to !test 1" , extra.channel );
          ComfyJS.Say( "replying to !test 2" , extra.channel );
        }
      } // End OnCommand

      ComfyJS.Init( "BotAccount" , "oauth:thisisnottheoathtoken", ["BotAccount", irc ] );

      </script>
  </body>
</html>
instafluff commented 3 years ago

Hi Eric, this is actually a limitation on Twitch's IRC chat rate limit. If you put a delay between the Say() or combine the messages into one Say() call, it should work.

Eric-Hacker commented 3 years ago

Thanks instafluff for the quick reply and thanks for the great library that makes getting started writing my own bot much easier for this JavaScript newbie. You are right. I have modified the code as follows and it works.

<html>
  <head>
    <script src="https://cdn.jsdelivr.net/npm/comfy.js@latest/dist/comfy.min.js"></script>
  </head>
  <body>
    <script type="text/javascript">
      var irc = "BroadcasterAccount";
      var users = ["Larry","Moe","Curly"]

      ComfyJS.onCommand = ( user, command, message, flags, extra ) => {
        if( (flags.broadcaster || flags.mod) && command === "test" ) {
            console.log( "!test was typed in chat" );
            for(u in users) {
                setTimeout( ComfyJS.Say , u * 2000, "Great job " + users[u], extra.channel );
            }
        }
      } // End OnCommand

      ComfyJS.Init( "BotAccount" , "oauth:thisisnottheoathtoken", ["BotAccount", irc ] );

      </script>
  </body>
</html>