Bots Home
|
Create an App
Sarah Utilities
Author:
sarahsprinkles
Description
Source Code
Launch Bot
Current Users
Created by:
Sarahsprinkles
/** * This source code carries no license by itself. You cannot use, modify, or copy unless you hold a * license to this Software. If you do not hold a license directly with Uetin, LLC you must destroy * this source code. Copyright (c) 2015 Uetin, LLC. * * Author sarahsprinkles */ (function Sarah_Utils() { var version = '1.0.1beta'; var author = 'sarahsprinkles'; function howLongAgo(time) { switch (typeof time) { case 'number': if (time === 0) { return "Never"; } break; case 'string': time = +new Date(time); break; case 'object': if (time.constructor === Date) { time = time.getTime(); } break; default: time = +new Date(); } var time_formats = [ [60, 'seconds', 1], [120, '1 minute ago', '1 minute from now'], [3600, 'minutes', 60], [7200, '1 hour ago', '1 hour from now'], [86400, 'hours', 3600], [172800, 'Yesterday', 'Tomorrow'], [604800, 'days', 86400], [1209600, 'Last week', 'Next week'], [2419200, 'weeks', 604800], [4838400, 'Last month', 'Next month'], [29030400, 'months', 2419200], [58060800, 'Last year', 'Next year'], [2903040000, 'years', 29030400], [5806080000, 'Last century', 'Next century'], [58060800000, 'centuries', 2903040000] ]; var seconds = (+new Date() - time) / 1000, token = 'ago', list_choice = 1; if (seconds == 0) { return 'Just now' } if (seconds < 0) { seconds = Math.abs(seconds); token = 'from now'; list_choice = 2; } var i = 0, format; while (format = time_formats[i++]) { if (seconds < format[0]) { if (typeof format[2] == 'string') { return format[list_choice]; } else { return Math.floor(seconds / format[2]) + ' ' + format[1] + ' ' + token; } } } return time; } function profit(tokens) { if (typeof tokens != 'number') { return '0'; } var calc = tokens * 5; var dollars = Math.floor(calc / 100); var cents = Math.floor(calc % 100); if (cents < 10) { cents = '0' + cents; } return '$' + dollars + '.' + cents; } /** * Gender of the user. * @type {{unknown: number, female: number, male: number, couple: number, trans: number}} */ var Gender = { values: ['unknown', 'female', 'male', 'couple', 'trans'], unknown: 0, female: 1, male: 2, couple: 3, trans: 4 }; /** * Color of the user. * @type {{grey: number, light_blue: number, dark_blue: number, purple: number, dark_purple: * number}} */ var Color = { values: ['grey', 'light_blue', 'dark_blue', 'purple', 'dark_purple'], grey: 0, light_blue: 1, dark_blue: 2, purple: 3, dark_purple: 4 }; /** * How many messages to store in history. * @type {number} */ var MAX_MESSAGE_COUNT = 3; /** * The message sent by a user * * @param message The message as a string. * @constructor */ function Message(message) { this.message = message; this.timestamp = new Date().getTime(); } /** * To track the user's sessions, time spent inside the chat before exiting. * @constructor */ function Session() { this.opened = new Date().getTime(); this.closed = 0; this.isOpen = function () { return !this.closed; }; this.close = function () { if (this.isOpen()) { this.closed = new Date().getTime(); } }; this.bounched = function () { return this.closed != 0 && this.closed - this.opened < 30000; }; } /** * The Chaturbate user, standardized with relevant information. * @param name The username as a string. * * @constructor */ function User(name) { this.name = name; this.gender = 0; this.color = -1; this.tipped = 0; this.tipped_timestamp = 0; this.messages = []; this.sessions = []; /** * @param value the amount tipped by this user. */ this.newTip = function (value) { this.tipped += value; this.tipped_timestamp = new Date().getTime(); }; /** * logs a new message, truncates the eldest entry if the MAX_MESSAGE_COUNT was reached. * @param message the message as a string. */ this.newMessage = function (message) { var m = new Message(message); this.messages.push(m); if (this.messages.length > MAX_MESSAGE_COUNT) { this.messages.shift(); } }; this.openSession = function () { if (this.sessions.length == 0 || !this.sessions[this.sessions.length - 1].isOpen()) { cb.log("Start session for: " + this.name); this.sessions.push(new Session()); } }; this.closeSession = function () { if (this.sessions.length != 0 && this.sessions[this.sessions.length - 1].isOpen()) { cb.log("Closing session for: " + this.name); this.sessions[this.sessions.length - 1].close(); } }; } var admin = String(cb.room_slug); var admin_usr = new User(admin); var hidden = {}; hidden[admin] = admin_usr; var mods = {}; mods[admin] = admin_usr; var users = {}; users[admin] = admin_usr; function updateUser(user_obj) { var user_name = user_obj['user']; if (!user_name) { user_name = user_obj['from_user']; } var u = cacheUser(user_name); var gender = user_obj['gender']; if (!gender) { gender = user_obj['from_user_gender']; } var tip_amount = user_obj['amount']; if (tip_amount) { u.newTip(tip_amount); } switch (gender) { case 'm': u.gender = Gender.male; break; case 'f': u.gender = Gender.female; break; case 's': u.gender = Gender.trans; break; case 'c': u.gender = Gender.couple; break; } if (user_obj['has_tokens'] || user_obj['from_user_has_tokens']) { var tokens_initializing = false; if (u.color == -1) { u.color = Color.light_blue; tokens_initializing = true; } if (u.color === Color.grey) { cb.sendNotice("Awwww " + u.name + " did you get tokens for me?!", u.name, "", "", "bold"); u.color = Color.light_blue; } // Dark blue if (u.color < Color.dark_blue) { if (user_obj['tipped_recently']) { u.color = Color.dark_blue; } else if (user_obj['from_user_tipped_recently']) { if (u.color == Color.light_blue && !tokens_initializing) { cb.sendNotice("Congratulations " + u.name + " on becoming dark blue.", "", "", "", "bold"); } u.color = Color.dark_blue; } } // Purple if (u.color < Color.purple) { if (user_obj['tipped_alot_recently']) { u.color = Color.purple; } else if (user_obj['from_user_tipped_alot_recently']) { if (u.color == Color.dark_blue && !tokens_initializing) { cb.sendNotice("Congratulations " + u.name + " on becoming purple.", "", "", "", "bold"); } u.color = Color.purple; } } // Dark purple if (u.color < Color.dark_purple) { if (user_obj['tipped_tons_recently']) { u.color = Color.dark_purple; } else if (user_obj['from_user_tipped_tons_recently']) { if (u.color == Color.purple && !tokens_initializing) { cb.sendNotice("Every body give a round of applause to " + u.name + " on becoming dark purple.", "", "", "", "bold"); } u.color = Color.dark_purple; } } } else if (u.color) { if (tip_amount) { cb.sendNotice("Everybody!! " + u.name + " went grey for me!!!", "", "", "", "bold"); } else { cb.sendNotice(box(admin) + ' went grey for someone else.', admin, "", "", "", "bold"); } } else { u.color = Color.grey; } var lastMessage = user_obj['m']; if (lastMessage) { u.newMessage(lastMessage); } return u; } function cacheUser(name) { if (name in users) { cb.log("User is cached: " + name); return users[name]; } else { cb.log("Creating user: " + name); return users[name] = new User(name); } } function processCommand(user, command, options) { switch (command) { case '-profit': var total = 0; for (var key in users) { if (users.hasOwnProperty(key)) { var u = users[key]; if (!hidden[u.name]) { total += u.tipped; } } } var profit_message; if (total) { profit_message = "Today's earnings so far is: " + profit(total); } else { profit_message = "You have earned nothing so far."; } cb.sendNotice(profit_message, user, "#ffff33", "", "bold"); break; case '-hide': if (options.length == 1) { if (options[0] == admin) { return; } if (!(options[0] in hidden) && options[0] in users) { hidden[options[0]] = users[options[0]]; } } break; case '-show': if (options.length == 1) { if (options[0] == admin) { return; } if (options[0] in hidden) { delete hidden[options[0]]; } } break; case '-stats': var indexOfUsername = options.indexOf('-user'); if (indexOfUsername == -1) { return; } indexOfUsername++; var indexOfCommand = options.indexOf('-display'); if (indexOfCommand == -1) { return; } indexOfCommand++; if (indexOfUsername < options.length && indexOfCommand < options.length) { if (options[indexOfUsername] in users) { var user_info = users[options[indexOfUsername]]; var message; switch (options[indexOfCommand]) { case 'token': if (!user_info.tipped) { message = box(user_info.name) + " hasn't tipped as of yet."; } else { message = box(user_info.name) + " has tipped you a total of: " + profit(user_info.tipped) + " :: Last tipped received " + howLongAgo(user_info.tipped_timestamp); } break; case 'messages': if (user_info.message_count == 0) { message = box(user_info.name) + " has not sent a message yet."; } else { message = box(user_info.name) + " has sent the following message(s): \n"; for (i = 0; i < user_info.messages.length; i++) { message += howLongAgo(user_info.messages[i].timestamp) + " : " + user_info.messages[i].message + "\n"; } message = message.substr(0, message.length - 1); } break; case 'session': break; case 'all': break; default: message = options[indexOfCommand] + "is an unsupported command. The supported commands are the following: token, messages, session, all"; } cb.sendNotice(message, user, "", "", "bold"); } } return; } } function box(string) { return '[' + string + ']'; } (function initialize() { }()); // Chaturbate functions. cb.onEnter(function (user) { var u = updateUser(user); u.openSession(); }); cb.onLeave(function (user) { var u = updateUser(user); u.closeSession(); }); cb.onMessage(function (message) { if (message['user'] in mods) { var split_message = message['m'].split(' '); if (split_message[0] === '!su' || split_message[0] === '/su' && split_message.length >= 2) { var option = split_message[1]; processCommand(message['user'], split_message[1], split_message.slice(2, split_message.length)); message['X-Spam'] = true; return message; } } var u = updateUser(message); u.openSession(); }); cb.onTip(function (tip) { var u = updateUser(tip); cb.sendNotice(box(u.name) + " tip is worth: " + profit(tip['amount']), admin, "#ffff33", "", "bold"); }); }());
© Copyright Chaturbate 2011- 2026. All Rights Reserved.