Repository navigation
Custom Script FollowAge
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}, ${period}", default: "${user} have been following this channel since ${datestring} which is ${days} days ago, or {period}." }, 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 in chat.", author: "ThePerry", version: "0.2" } } 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()); var periodDays = periodOfDays(followAge(Date.UTC(followedYear, followedMonth-1, followedDay), Date.now())); message = followerAgeMsgTemplate .replace("${user}", username) .replace("${days}", difference) .replace("${period}", periodDays) .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; } function periodOfDays (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;
I've added some variables so you can change the way how the messages is shown in the chat:
- ${user} - Gives the username of the one pushing the button or using the chat command.
- ${days} - Gives the difference in days between the date the user followed you and today.
- ${datestring} - Gives the entire date string directly from the API, year-month-day (US).
- ${timestring} - Gives the Zulu time for when the user followed the channel.
- ${month} - Shows the month, numeric.
- ${day} - Shows the day, numeric.
- ${year} - Shows the year.
Pr default the chat followage message is:
"${user} have been following this channel since ${datestring} which is ${days} days ago."
You can easily change that into:
"${user} have been following this channel since ${day}-${month}-${year} which is ${days} days ago."
"${user} followed ${days} ago."
"${user} became rawesome at ${datestring} which is ${days} days ago. Thank you for the support.";
Still need help? Come chat with us in the #help channel of our Discord server.