Bots Home
|
Create an App
fkakskds
Author:
yourboydev1
Description
Source Code
Launch Bot
Current Users
Created by:
Yourboydev1
/** * timelock_greys * * Author: loremipsumbacon * Description: Silence greys for a period after joining * Release: 2 */ // Settings cb.settings_choices = [ { name: "timelock_duration", type: "int", minValue: 1, maxValue: 99, defaultValue: 5, label: "Timelock duration in minutes" }, { name: "greys_off", type: "choice", choice1: "False", choice2: "True", defaultValue: "False", label: "Turn greys off entirely" }, { name: "messages_header", required: false, type: "str", minLength: 1, maxLength: 255, defaultValue: "{0} will become the number of minutes", label: "Below are messages for customization/translation (Optional)" }, { name: "msg_welcome", type: "str", minLength: 1, maxLength: 255, defaultValue: "Please sit back and enjoy the show! You are timelocked and will be able to chat {0} minutes after joining.", label: "Welcome message for greys (timelock active)" }, { name: "msg_welcome_greys_off", type: "str", minLength: 1, maxLength: 255, defaultValue: "Welcome! Grey chat has been disabled so please sit back and enjoy the show!", label: "Welcome message for greys (greys off)" }, { name: "msg_locked", type: "str", minLength: 1, maxLength: 255, defaultValue: "You are timelocked and cannot chat for another {0} minutes. You may skip the wait by purchasing tokens.", label: "Message for chatting while timelocked" }, { name: "msg_cantchat", type: "str", minLength: 1, maxLength: 255, defaultValue: "Grey chat has been disabled. You must purchase some tokens before you will be allowed to chat.", label: "Message for chatting while grey chat is off" }, { name: "msg_greyson", type: "str", minLength: 1, maxLength: 255, defaultValue: "Grey chat has been enabled.", label: "Greys on announcement" }, { name: "msg_greysoff", type: "str", minLength: 1, maxLength: 255, defaultValue: "Grey chat has been disabled.", label: "Greys off announcement" } ]; // Script globals var seen = {}; // Tracks which users we've seen // Logging var log = {}; log.DEBUG = 0; log.INFO = 1; log.WARN = 2; log.ERR = 3; log.level = log.WARN; // Default log level // Log to debug level log.debug = function(msg) { if (log.level > log.DEBUG) return; //cb.log("Debug: " + msg); tellOwner("Debug: " + log.format(msg)); }; // Log to info level log.info = function(msg) { if (log.level > log.INFO) return; tellOwner("Info: " + log.format(msg)); }; // Log to warning level log.warn = function(msg) { if (log.level > log.WARN) return; tellOwner("Warning: " + log.format(msg)); }; // Log to info level log.err = function(msg) { if (log.level > log.ERR) return; tellOwner("Error: " + log.format(msg)); }; // Format as string, using JSON to pretty print structures log.format = function(msg) { if (typeof msg === "string") return msg; else return JSON.stringify(msg, null, 4); }; // Utility functions /** * Formats arguments accordingly the formatting string. * Each occurence of the "{\d+}" substring refers to * the appropriate argument. * * From: http://code.fosshub.com/jsxt * * @example * '{0}is not {1} + {2}'.format('JavaScript', 'Java', 'Script'); * * @param mixed * @return string * @access public */ String.prototype.format = function() { var args = arguments; return this.replace(/\{(\d+)\}/g, function($0, $1) { return args[$1] !== void 0 ? args[$1] : $0; }); }; // Have we seen this user before? function isNew(username) { return !seen.hasOwnProperty(username); } // Do we always allow this type of user to speak? function isPermaSpeaker(user) { var username = user.user; // Broadcaster can always speak if (username == cb.room_slug) return true; // Test user is always grey if (user.user == "loremipsumbacongrey") return false; // If blue, green or red they can always speak if (user.has_tokens || user.in_fanclub || user.is_mod) return true; else return false; } // If a user is grey. // Split out to allow isPermaSpeaker to change if needed at some point. function isGrey(user) { return !isPermaSpeaker(user); } // Can the user use admin commands? function isAdmin(user) { var username = user.user; // Broadcasters and mods can run admin commands if (username == cb.room_slug || user.is_mod) return true; else return false; } // Is the user allowed to chat function canSpeak(user) { var username = user.user; // If we haven't seen the user before, add them if (isNew(username)) { addUser(user); if (username != cb.room_slug) log.info("Note: Pre-existing user added (no join seen): " + username); } // If they've been marked as canSpeak, yes if (seen[username].canSpeak) return true; // If the timelock has expired, yes if (expired(seen[username].entered)) { setSpeaker(username); return true; } return false; } // Set a user function setSpeaker(username) { seen[username].canSpeak = true; } // Check if a timelock has expired function expired(date) { return elapsed(date) > cb.settings.timelock_duration; } // Get elapsed time since a date, in minutes function elapsed(date) { var now = Date.now(); var minutes = Math.floor((now - date) / (1000 * 60)); return minutes; } // Give user the welcome message function welcomeUser(username) { var msg = cb.settings.msg_welcome; var duration = cb.settings.timelock_duration; tellUser(msg.format(duration), username); } // Start tracking the user function addUser(user) { var username = user.user; var u = {}; // User info u.entered = Date.now(); u.canSpeak = isPermaSpeaker(user); seen[username] = u; log.info("Added user: " + username + " with values: "); log.info(u); log.debug("DB state:"); log.debug(seen); } // Send text to everyone function tellAll(msg) { cb.sendNotice(msg); } // Send text to a given user function tellUser(msg, username) { cb.sendNotice(msg, username); } // Send text to the broadcaster function tellOwner(msg) { tellUser(msg, cb.room_slug); } // How many minutes until a user can speak function timeLeft(username) { var ERR = -1; if (isNew(username)) { log.err("timeLeft called on invalid username: " + username); return ERR; } var entered = seen[username].entered; return cb.settings.timelock_duration - elapsed(entered); } // Does the text look like a command? function isCmd(txt) { if (txt.charAt(0) === "/") return true; else return false; } // Check if greys are off function greysOff() { return cb.settings.greys_off == "True"; } // Commands // Execute command function runCmd(msg) { var username = msg.user; var user = msg; // Msg includes user info, so we can treat it as such // Only admins can run commands if (!isAdmin(user)) { return false; } // Check for the tlg prefix, otherwise command is likely meant for another app if (msg.m.search("/tlg") !== 0) return false; // Extract the command name and any parameters var regex = /^\/tlg\s*(\S*)\s*(\S*.*)/i; var cmdSplit = msg.m.match(regex); log.debug("Command split:"); log.debug(cmdSplit); // No command found if (typeof cmdSplit === 'undefined' || cmdSplit === null) { log.err("Invalid command: " + msg.m); return false; } // Command name var cmd = cmdSplit[1]; // Command parameters var args; if (cmdSplit.length > 1) args = cmdSplit[2]; // If no command is provided just display the help if (typeof cmd === "undefined" || cmd === "") { cmdHelp(username); return false; } // Execute the command switch (cmd.toLowerCase()) { case "resetall": cmdResetAll(); break; case "resetuser": cmdResetUser(args); break; case "greysoff": cmdGreysOff(msg, args); break; case "help": cmdHelp(); break; default: tellUser("Unknown command: " + cmd, username); return false; } return true; } // Show commands function cmdHelp(username) { log.debug("Running cmdHelp"); // If msg is null (such as upon init) assume it's the broadcaster if (typeof username === "undefined") { username = cb.room_slug; } tellUser("", username); tellUser("TimeLockGreys", username); tellUser("=============", username); tellUser("", username); tellUser("/tlg greysoff - Toggle silencing greys on/off", username); tellUser("/tlg resetuser <user> - Force user to restart the timelock", username); tellUser("/tlg resetall - Force all users to restart the timelock", username); tellUser("/tlg help - Display this help message", username); tellUser("", username); } // Reset all seen data, as if starting fresh function cmdResetAll() { seen = {}; tellOwner("Reset all user info. Note: Timelocks for existing users will only start when they rejoin or attempt to speak."); } // Reset data for a specific user function cmdResetUser(username) { if (!isNew(username)) { delete seen[username]; tellOwner("Reset user info for {0}. Note: Timelocks for existing users will only start when they rejoin or attempt to speak.".format(username)); } else { tellOwner("Unknown user {0}.".format(username)); } } // Toggle the ability of greys to speak function cmdGreysOff() { if (cb.settings.greys_off === "True") { cb.settings.greys_off = "False"; tellAll(cb.settings.msg_greyson); } else { cb.settings.greys_off = "True"; tellAll(cb.settings.msg_greysoff); } } // CB Hooks // Welcome and track users as they join cb.onEnter(function(user) { var username = user.user; // If new, start tracking if (isNew(username)) { log.info("New user entered: " + username); log.debug(user); addUser(user); // Track enter time } else { log.info("User re-entered: " + username); log.debug(user); } // Send appropriate welcome message to greys if (isGrey(user)) { if (greysOff()) { var msg = cb.settings.msg_welcome_greys_off; tellUser(msg, username); } else { welcomeUser(username); // Send welcome msg } } }); // Leave function is required by CB, but not used by this script. cb.onLeave(function(user) { var username = user.user; log.info("User left: " + username); }); // When a user attempts to chat, block if timelocked cb.onMessage(function(msg) { var user = msg; // Msg includes user info, so we can treat it as such var username = msg.user; var txt = msg.m; log.debug("Message received:"); log.debug(msg); log.debug("User is grey? " + isGrey(user)); // Check if we should hide this message if (greysOff() && isGrey(user)) { msg["X-Spam"] = true; // Set flag to hide chat from room tellUser(cb.settings.msg_cantchat, username); log.info("Silenced message from " + username + " (greys are off)"); } else if (!canSpeak(user)) { var left = timeLeft(username); msg["X-Spam"] = true; // Set flag to hide chat from room tellUser(cb.settings.msg_locked.format(left), username); log.info("Silenced message from " + username); } // If looks like a command, hide it and try running it if (isCmd(txt)) { msg["X-Spam"] = true; // Set flag to hide chat from room runCmd(msg); } return msg; }); // Script startup function init() { //log.level = log.DEBUG; log.debug("Initial user db: " + log.format(seen)); cmdHelp(); } init();
© Copyright Chaturbate 2011- 2024. All Rights Reserved.