我正在尝试构建一个机器人,只要Twitch流上线,它就会向某个频道发送消息。但是,我一直遇到这种流数组的问题。我希望邮件在流上线时发送ONCE,然后再次发送,直到流被标记为脱机并再次联机。我不能为我的生活弄清楚为什么下面的代码不会这样做。
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);
如果我的问题格式不正确或者我做错了什么,我提前道歉,这是我第一次使用Stack Overflow。
提前致谢!
编辑:固定代码语法。需要setInterval以特定间隔对Twitch执行请求,因为Twitch API当前没有流功能