Twitch Live Checker

Hello there,

so recently we started using a Bot (SinusBot) on our Root server. And i added a Script to the Bot that looks up every user i enter and checks their Live Status on twitch. If they are online he will send a command that assigns a Group on Teamspeak.

My Problem now is:

The Bot wants the Twitch UserID, if i am not wrong that’s just my name in Twitch.

And he wants my “Twitch Dev App Client ID” wich i got from “https://dev.twitch.tv/dashboard/apps

  1. I don’t know what to put in “OAuth Redirect URLs” so i just put “http://localhost

  2. If i then give my client id to the Bot i get this response every time he wants to check “HTTP-Error: 401 Unauthorized”

I’m completely new to Twitch integration etc.

so maybe its completely simple and im just to dumb for it :smiley:

Thanks in advance.

PS. Sorry for my bad English

The 401 error will also have returned a body message to describe the actual issue.

You have not generated an App Access Token, to make the request with.

You need to take your ClientID and ClientSecret and use them to generate an “App Access Token” used for Server to Server requests, that is documented here:

Where can i find that? Because in the log of the Bot the only thing i get is the 401 Error

I don’t know without looking at your code.

But the issue is that you didn’t include an oAuth token most liekly

This is everything, im sorry if I am annoying. But I know nothing about Oauth, API and stuff like that. :grimacing: :confounded:

Thats not code…

That looks like some builder, and doesn’t give you anything useful and probably has not been updated for the recent changes

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

});

This section needs updating for the changes to Twitch

oh okay thats Problem thank you a lot. i will see if i can contact the Owner of this Script.

Thanks for your time and have a nice day.