[Q] Get broadcaster / users in chat

Hi, I’m new(ish) to Nightbot and a not so very popular streamer, but anyways I was looking into adding “fun” commands to Nightbot, and I read about !kill, and thought that sounded like a stellar idea, but I wanted more flexibility, and a few customized things added. Couldn’t find a posted code so I could even figure out where to begin. Turned up PHP was a rather simple solution, 2 hours of reading up on PHP and I have the php generating random methods of death, and getting the GET data from the URL of the killer and the killed person. (Can I add code on here, or just snippets?) I have an if statement checking to see if someone is trying to kill me (the broadcaster) or the bot(s). But I was wondering if there’s a more universal method to get the broadcaster, say I wanted to share the API with a friend without modifying the code. I’d also like to get the list of current user(s) in chat and compare that to whoever your trying to kill, so you can’t type in "!kill random_stuff_or_name. I was also trying to get the API to split up messages, like the roulette game where it says something, than a few seconds later it says something else. I tried to mess with the sleep() function, but it caused the page to wait, and not just in-between the echos. I know this was a long winded question, and went in multiple directions, I just hope it’s clear what I’m looking for. Thanks for reading!

I honestly have no idea what you’re actually asking here. If you need help with PHP this is probably the wrong place to ask. I suggest going to Stack Overflow or some place similar for advice.

I thought about going over to SO, but wasn’t entirely sure if that’d be the place to goto, I’m not 100% sure what I’m asking help for I guess.
My goals
A) Get the stream broadcaster via the API - to check if someone is trying to kill me, or the broadcaster in general.
B) Get the stream active users/ chatters -to check for a valid kill target.
C) Split up messages on new lines, for some reason, the “Conventional” methods of echoing a new line aren’t working for me, it all prints on the same line. I’d like to be able to have the bot say 2 things, in 2 separate messages with the 1 function in the API.
I’m not looking for a spoon fed answer, I got this far by trial and error. I just don’t know where to look to accomplish what I want to do. I tried various google searches that turned me around.

You can enter the broadcaster name as GET parameter, you’ll only have to edit this once when you add the command to nightbot. For example. $(customapi http://example.com?channel=YOURCHANNEL). In your PHP script you can read the channel now with $_GET[‘channel’].

For the viewerlist take a look at:
https://tmi.twitch.tv/group/user/CHANNELNAME/chatters

Yes, I stated that I currently have my name in there being read from the GET data. And with that link for the active chatters, is there no way to have it all wrapped up into 1 API? Am I suppose to call one API from another? Wouldn’t it be faster all wrapped up into 1 API?

I’m not sure what you mean. From your PHP script you can retrieve the content from the viewerlist with that link and process it to a text response for Nightbot. That’s just one request, I don’t see a problem.

And I’m reading your start post again, don’t use sleep functions, Nightbot will time out eventually. If you want to accomplish random responses, just fill an array with multiple responses and let PHP pick a random one.

Yeah, I realized sleep function wasn’t going to do me any good because it causes the entire page to wait to load, something about the sever sided setup or something. But I was trying to echo ‘don’t kill the host’ than a few seconds later echo ‘this is a warning’ or something like that. I read on SO that JavaScript could do this than pass it to plain text, not sure since I read that nightbot can’t parse JS. The actual kill function works fine, I’m wanting to add more logic. So in order to get the active chat users I send a request from the API, is that straight up PHP or would it be better to use JSON? I have no idea what JSON is, but hear it used frequently with API

Alright I see what you mean, but I dont think you’re understanding how the custom API works. Nighbot will just display the content of the link you’ll give in your $(customapi link) command. This result has to be plain text, max 400 characters I believe and it has to be fully loaded in ~4 secs, or the request will time out.
So every single time someone uses the command, it will only show 1 response message in the chat, you have to make sure that everything you want to display is in that 1 message.
You can use JSON in PHP, use the function json_decode().

Okay, I just see boys that print multiple messages for 1 function, so I thought I could do that with an API as well. You’re absolutely correct, I have no idea how all this stuff works, what I have thus far is Frankensteined together. So would it be best to use 2 separate APIs? One for checking users, than my kill. Can I call one API from another to compare? Sorry I’m on my phone and can’t do a lot of research.

It’s not possible to “kill” (timeout) users in chat with Nightbot. We do this to prevent abuse.

If you want random responses, you can use PHP code to accomplish this:

<?php
    $input = array("Neo", "Morpheus", "Trinity", "Cypher", "Tank");
    $randKey = array_rand($input);
    echo $input[$randKey];
?>

We are considering adding a random response system to the next Nightbot at some point in the future, but I do not have a timeframe of when.

Yes, I’m aware Nightbot can’t execute functions for abuse reasons. I have the kill responses, such as “Owyn has just killed the Nightbot community by forcing them to swallow razor blades!”, and I have the viewer list, but I’m not sure how I’d do the check to see IF (Kill == user && user_is_viewing == true). I have the ideas in my head, just not sure how to execute in PHP.

If you random pick a user from the viewing list, user_is_viewing is always true?
Not sure what you mean with Kill == user.

I was thinking about combining a few smaller APIs into one, with Kill being one of the GETs, and I don’t want to randomally pick a user, I was just wanting to make sure that the user someone’s trying to kill is in the chat. but once I figure out how to do what I want to do, user_is_viewing will be set to true

<?php
$bViewing = false; // default false
$aViewers = array(); // fetch your viewers from somewhere;

if(in_array($_GET['user'], $aViewers)){
	// user is viewing
	$bViewing = true;
}

// switch your different actions;
switch($_GET['action']){
	
	case 'kill':
		if($bViewing === true){
			// user is viewing + action = kill
		}
	break;
}
?>

I’ve spent an embarrassing amount of time trying to get this to work. Mostly getting the JSON data into the API, numerous attempts, but I couldn’t echo the array, which leads me to believe the array isn’t loading in the first place. I’ll abandon my thoughts and leave the API as is since it works fine. Thank you for your help xgerhard.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.