Skip to content

Custom Script FollowAge

Perry edited this page Jan 5, 2018 · 10 revisions

This is a Custom FollowAge Script for Firebot. Simply save this text to a file and name it followage.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."
			},
			followerAgeMessageTemplate: {
				type: "string",
				description: "Follower message template",
				secondaryDescription: "The follower age message to show in chat. Here are some variables you can use in this message: ${user},${datestring}, ${days}, ${day},${month},${year}",
				default: "${user} have been following this channel since ${datestring} which is ${days} days ago."
			},
			followerAgeNotFollowingMessageTemplate: {
				type: "string",
				description: "Not a follower Message template",
				secondaryDescription: "This message show in chat when someone that isn't following uses the command/button.",
				default: "${user}, you are not following this channel!"
			}
		});
	});
}

exports.getScriptManifest = function() {
	return {
		name: "FollowAge",
		description: "Allows you to display followage of a user 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/"+ channelId +"/follow?where=username:eq:" + username;
		let followerAgeMsgTemplate = runRequest.parameters.followerAgeMessageTemplate;
		let followerAgeNotFollowingMsgTemplate = runRequest.parameters.followerAgeNotFollowingMessageTemplate;
		
		request(url, function (error, response, data) {
			var response = {};
			if (!error) {
				// Got response from Mixer.
				var data = JSON.parse(data);
				let message;
				if (data[0] == undefined) {
					message = followerAgeNotFollowingMsgTemplate.replace("${user}", username);
				}else{
					var followed = data[0].followed;
					// Selecting the followed date
					var followedAt = followed.createdAt;
					// Splitting the date into an array to pick it apart later
					var dataString = followedAt.split("T");
					var dateString = dataString[0].split("-");
					// Setting year, month and day variables.
					var followedYear = dateString[0];
					var followedMonth = dateString[1];
					var followedDay = dateString[2];
					// Calculating the difference between Now and then
					var difference = followAge(Date.UTC(followedYear, followedMonth-1, followedDay), Date.now());

					message = followerAgeMsgTemplate
						.replace("${user}", username)
						.replace("${days}", difference)
						.replace("${datestring}", dataString[0])
						.replace("${timestring}", dataString[1])
						.replace("${month}", followedMonth)
						.replace("${day}", followedDay)
						.replace("${year}", followedYear);
				}
				
				// 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 followAge (date1, date2) {
	return (new Date(date2) - new Date(date1))/(1000*3600*24) | 0;
}

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