Skip to content

Custom Script HostViewerCount

Perry edited this page May 13, 2018 · 2 revisions

This is a Custom Host viewer count Script for Firebot to use for Host Events so you can show of how many viewers joined in on a host. Simply save this text to a file and name it hostviewer-count.js.

Place that file into Firebots script folder and add the script to any button or command with the Custom Script effect:

exports.getDefaultParameters = function() {
	return new Promise((resolve, reject) => {
		resolve({
			chatter: {
				type: "enum",
				options: ["Streamer", "Bot"],
				default: "Bot",
				description: "Send From",
				secondaryDescription: "Which account to send the messages from."
			},
			hostViewerCountMessageTemplate: {
				type: "string",
				description: "Hostcount message template",
				secondaryDescription: "The message to show in chat. Here are some variables you can use in this message: ${user}, $(numViewers)",
				default: "${user} just hosted us with ${numViewers} viewers."
			}
		});
	});
}

exports.getScriptManifest = function() {
	return {
		name: "Host viewer count",
		description: "Allows you to announce how many viewers joined on the host",
		author: "ThePerry",
		version: "0.1"	
	}
}

function run(runRequest) {
	var username = runRequest.user.name;

	var shouldWhisper = runRequest.parameters.shouldWhisper;
	
	const request = runRequest.modules.request;
	
	// Return a Promise object
	return new Promise((resolve, reject) => {
		var url = "https://mixer.com/api/v1/channels/"+ username +"?fields=viewersCurrent";
		let hostViewerCountMsgTemplate = runRequest.parameters.hostViewerCountMessageTemplate;
		
		request(url, function (error, response, data) {
			var response = {};
			if (!error) {
				// Got response from Mixer.
				var data = JSON.parse(data);
				let message;
				if (data.viewersCurrent == undefined) {
					message = "An error occured...";
					
				}else{
					message = hostViewerCountMsgTemplate
						.replace("${user}", username)
						.replace("${numViewers}", data.viewersCurrent);
				}
				
				// Create a success response 
				response = {
					success: true,
					effects:[
						{
							type: EffectType.CHAT,
							message: message,
							chatter: runRequest.parameters.chatter
						}
					]
				}
			} else {
				// We had an error with the mixer request. So, create an error popup in Firebot.
				// Create a failed response
				response = {
					success: false,
					errorMessage: 'There was an error retrieving data from the Mixer API.'
				}
			}
		// Resolve Promise with the response object
		resolve(response);
		})
	});
}

// Export 'run' function so it is visible to Node
exports.run = run;

Add this as a Custom Effect on a Host Event, remember to turn on Custom Scripts in Settings if you haven't done so already.

Keep in mind that this viewer count will most likely not be 100% right if the hoster has viewers that have been hosted from another channel, these viewers will not be sent along on the host unless they join your channel on their own after the host has been done. This is a limitation of how Mixer works in regards of hosts which we can't bypass.

Future plans:

  • Make filter options so you can choose when to trigger, like only trigger if the count of the hosted viewers are higher than 1.
  • Make filter options to make it possible to give different host messages depending on the amount of hosted viewers.
Clone this wiki locally