Skip to content

Custom Script MixerAge

Perry edited this page Apr 17, 2018 · 1 revision

This is a Custom MixerAge Script for Firebot. Simply save this text to a file and name it mixerage.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."
			},
			mixerAgeMessageTemplate: {
				type: "string",
				description: "Mixerage message template",
				secondaryDescription: "The Mixerage message to show in chat. Here are some variables you can use in this message: ${user}, ${datestring}, ${days}, ${day}, ${month}, ${year}, ${period}",
				default: "${user} have been on Mixer since ${datestring} which is ${period}."
			}
		});
	});
}

exports.getScriptManifest = function() {
	return {
		name: "MixerAge",
		description: "Allows you to display mixerage of a user in chat.",
		author: "ThePerry",
		version: "0.1"	
	}
}

function run(runRequest) {
	var username = runRequest.user.name;
	var shouldWhisper = runRequest.parameters.shouldWhisper;
	const fs = runRequest.modules.fs;	
	const authFile = JSON.parse(fs.readFileSync(SCRIPTS_DIR + "../auth.json", 'utf8'));
	const channelId = authFile.streamer.channelId;
	
	const request = runRequest.modules.request;
	
	// Return a Promise object
	return new Promise((resolve, reject) => {
		var url = "https://mixer.com/api/v1/channels/"+ username +"?fields=createdAt";
		let mixerAgeMsgTemplate = runRequest.parameters.mixerAgeMessageTemplate;
		
		request(url, function (error, response, data) {
			var response = {};
			if (!error) {
				// Got response from Mixer.
				var data = JSON.parse(data);
				let message;
				if (data == undefined) {
					message = "Can't find the user";
				}else{
					var created = data;
					// Selecting the created date
					var createdAt = created.createdAt;
					// Splitting the date into an array to pick it apart later
					var dataString = createdAt.split("T");
					console.log("dataString: "+ dataString);
					var dateString = dataString[0].split("-");
					// Setting year, month and day variables.
					var createdYear = dateString[0];
					var createdMonth = dateString[1];
					var createdDay = dateString[2];
					// Calculating the difference between Now and then
					var difference = 
						mixerPeriod(mixerAge(Date.UTC(createdYear, createdMonth-1, createdDay), Date.now()));

					message = mixerAgeMsgTemplate
						.replace("${user}", username)
						.replace("${period}", difference)
						.replace("${datestring}", dataString[0])
						.replace("${timestring}", dataString[1])
						.replace("${month}", createdMonth)
						.replace("${day}", createdDay)
						.replace("${year}", createdYear);
				}
				
				// 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);
		})
	});
}

function mixerAge (date1, date2) {
	return (new Date(date2) - new Date(date1))/(1000*3600*24) | 0;
}

function mixerPeriod (days) {
	var years = parseInt(days/365);
	days = days - years * 365;
	var months = parseInt(days / 30);
	days = days - months * 30;
	var weeks = parseInt(days / 7);
	days = days - weeks * 7;
	return (years > 0 ? years + " year" + (years > 1 ? "s, " : ", ") : "") + (months > 0 ? months + " month" + (months > 1 ? "s, " : ", ") : "") + (weeks > 0 ? weeks + " week" + (weeks > 1 ? "s, " : ", ") : "") + (days > 0 ? days + " day" + (days > 1 ? "s" : "") : "");
}

// Export 'run' function so it is visible to Node
exports.run = run;
Clone this wiki locally