Twitch Live Checker

This is the whole Code of the Script if you mean that

registerPlugin({
name: ‘Twitch Live Checker’,
version: ‘1.0.0’,
description: ‘Checks twitch status and assigns server group, so that users can see if a client is currently streaming.’,
author: ‘Jaywalker’,
requiredModules: [“http”],
vars: [{
name: ‘mode’,
title: ‘How twitch streamers in your Teamspeak will be identified’,
type: ‘select’,
options: [
‘List’,
‘Description’
]
},{
name: ‘userChannels’,
title: ‘Each line represents TS3 Ids and Twitch UserIDs (tsID:twitchID)’,
type: ‘multiline’,
conditions: [{
field: ‘mode’,
value: 0,
}]
},{
name: ‘twitchNameHint’,
title: ‘The hint in the description that comes before the twitch name (HINTtwitchID)’,
type: ‘string’,
placeholder: ‘twitch.tv/’,
conditions: [{
field: ‘mode’,
value: 1,
}]
},{
name: ‘interval’,
title: ‘Interval (in seconds) Best case not under 30 seconds…’,
type: ‘number’,
placeholder: ‘30’
},{
name: ‘twitchLiveGroup’,
title: ‘Group-Number of your Twitch Live Server Group’,
type: ‘number’,
placeholder: ‘100’
},{
name: ‘clientIdTwitch’,
title: ‘Your Twitch Dev-App Client ID ( https://dev.twitch.tv/dashboard/apps )’,
type: ‘string’
},{
name: ‘outputType’,
title: ‘Logging Output-Type’,
type: ‘select’,
options: [
‘Log Only’,
‘Channel-Chat’
]
},{
name: ‘outputVerbosity’,
title: ‘Logging Verbosity’,
type: ‘select’,
options: [
‘Errors’,
‘Debug’
]
}]
}, function (sinusbot, config) {
//for ts-specific stuff
var backend = require(‘backend’);
//for logging stuff
var engine = require(‘engine’);
//for web stuff
var http = require(“http”)
function logToOutput(s, isError){
//checks the set outputType and either logs to chat or only to the sinus Console
if (isError || config.outputVerbosity == 1){
if (config.outputType == 1){
backend.getCurrentChannel().chat(s);
}
engine.log(s);
}
}
//when loading the plugin, we split the user info, each line represents the global TS-ID and the twitch username
if (config.mode == 0) {
try {
var tsTwitch = (config && config.userChannels) ? config.userChannels.split(’\n’).map(function (e) {
return e.trim().replace(/\r/g, ‘’);
}) : [];
} catch(err) {
logToOutput('Config error (TS-Twitch: ’ + err.message, true);
}
}

//function to check if client cl is currently a member of the twitch-live server group
function isInLiveGroup(cl){
	var groups = cl.getServerGroups();
	var found = false;
	for(var i = 0; i < groups.length; i++) {
		if (groups[i].id() == config.twitchLiveGroup) {
			found = true;
			break;
		}
	}
	return found;
}

function checkLiveStatus(cl, twitchName){
	//sending a request to twitch helix api with the corresponding twitch username to check if the channel is online
	http.simpleRequest({
		method: 'GET',
		url: 'https://api.twitch.tv/helix/streams?user_login=' + twitchName,
		headers: {'client-id': config.clientIdTwitch},
		timeout: 10 * 1000
	}, function(error, response) {
		if (error) {
			logToOutput('Error: ' + error, true);
		}else if (response.statusCode != 200) {
			logToOutput('HTTP-Error: ' + response.status, true);
		}else {
			logToOutput('API-GET success', false);
			var res;
			try {
				res = JSON.parse(response.data.toString());
			} catch (err) {
				logToOutput('JSONparse-Error: ' + err.message, true);
			}							
			if (res === undefined) {
				logToOutput('Invalid JSON.', true);
			}else {
				logToOutput('JSON parse success', false);
				if (res.data.length == 0){
					//stream is offline
					logToOutput(cl.name() + '\'s Stream is currently offline', false);
						if (isInLiveGroup(cl)){
							logToOutput('Live group is being removed...', false);
							cl.removeFromServerGroup(config.twitchLiveGroup);
						}else {
							logToOutput('Live group was already removed.', false);
						}
				} else {
					//stream is online ... probably
					if (res.data[0].type == 'live'){
						logToOutput(cl.name() + '\'s Stream is currently live!', false);
						//stream is definitely online
						if (!isInLiveGroup(cl)){
							logToOutput('Live group is being applied...', false);
							cl.addToServerGroup(config.twitchLiveGroup);
						}else {
							logToOutput('Live group was already applied.', false);
						}
					}
				}
			}
		}
	});
}
//this is called every (interval) seconds
setInterval(function () {
	if (config.mode == 0) {
		logToOutput('Checking Twitch Live Status for ' + tsTwitch.length + ' users', false);
		//check for all users that are configurated
		for(i = 0; i < tsTwitch.length; i++){
			//usr looks like this:  globalTwitchID63274gs82=:myChannelTV
			var usr = tsTwitch[i];
			//we split with the delimeter
			var tmp = usr.split(':');
			//check if split worked
			if (tmp.length == 2){
				var tsID = tmp[0];
				logToOutput('TS-ID: ' + tsID, false);
				var twitchID = tmp[1];
				logToOutput('Twitch-ID: ' + twitchID, false);
				//we search for the client and check if he is online
				var client = backend.getClientByUniqueID(tsID);
				if (client != null){
					//client is found and is online
					logToOutput(client.name() + ' is online in Teamspeak', false);
					//and we check for the client's live status
					checkLiveStatus(client, twitchID);
				} else {
					logToOutput('Client not found', false);
				}
			}
		}
	} else {
		//check for all online users
		var streamers = [];
		var onlineClients = backend.getClients();
		logToOutput('Checking Twitch Live Status for ' + onlineClients.length + ' users', false);
		onlineClients.forEach(function(client) {
			//get the description of the client
			var desc = client.description();
			//now we search and extract the twitch name from the description
			var twitchNamePos = desc.search(config.twitchNameHint);
			if (twitchNamePos != -1) {
				twitchNamePos = twitchNamePos  + config.twitchNameHint.length;					
				var twitchNameAndRest = desc.substr(twitchNamePos);
				var twitchNameEnd = twitchNameAndRest.search(' ');
				if (twitchNameEnd == -1) {
					twitchNameEnd = twitchNameAndRest.search('\n');
				}
				//MAGIC NUMBEEEERRRRS
				if (twitchNameEnd == -1) {
					twitchNameEnd = twitchNameAndRest.length;
				}
				if (twitchNameEnd != -1){
					//Okay we have found the twitch name start and end position and can now extract it from the substring
					var twitchName = twitchNameAndRest.substr(0, twitchNameEnd);
					logToOutput('Found the twitch Name: ' + twitchName, false);
					//and check for the client's live status
					checkLiveStatus(client, twitchName);
				} else {
					logToOutput('Could not find end of twitch Name: ' + twitchNameAndRest, true);
				}
			} else {
				logToOutput(client.name() + ' has no twitch name in description', false);
			}
		});
	}
}, config.interval * 1000);

});