0

I'm trying to build a bot that will send a message to a certain channel whenever a Twitch stream goes live. However, I've been having trouble with this array of streams that I have. I want the message to send ONCE when the stream comes online and then not send again until the stream has been marked as offline and comes online again. I can't for the life of me figure out why the below code won't do it.

var timer = setInterval(function(){
    var url = "https://api.twitch.tv/kraken/streams/";
    for(var i = 0; i < myStreams.length; i++){
        var currentStream = myStreams[i];   
        request({
        url: url + currentStream.urlsuffix,
        json: true
    }, function (error, response, body) {
        if(!error && response.statusCode === 200) {      
            if(body.stream != null){
                if(!currentStream.isOnline){
                    myBot.sendMessage(globalChannel, currentStream.name + " is going live! " + body["stream"]["channel"].url);  
                    currentStream.isOnline = true;
                    }
                }
            else{                           
                currentStream.isOnline = false;
                }
            }
        });
    }
}
// Change the 1 to how many seconds you want in between the requests for Twitch
, 1 * 1000);

I apologize in advance if my question is poorly formatted or if I've done anything wrong, this is my first time on Stack Overflow.

Thanks in advance!

EDIT: Fixed code syntax. The setInterval is needed to perform the request at a certain interval to Twitch because there is currently no streaming functionality for the Twitch API

Drew Hill
  • 71
  • 3
  • The closure in the loop will be the primary breaking point, where `currentStream` won't be what is expected. Aside from that, your syntax is broken, since you don't have `, 1000);` at the end, where 1000 is the interval. Not sure why the `setInterval` is needed in your code. –  Mar 01 '16 at 00:27
  • In my original code I do have the correct syntax for setInterval. I'll edit that and include it. Apologies! – Drew Hill Mar 01 '16 at 00:32

0 Answers0