Bots Home
|
Create an App
nicknames_testbed
Author:
patrik128
Description
Source Code
Launch Bot
Current Users
Created by:
Patrik128
/* Name: NickColour Author: billyab Inspiration: PrincessLexi__ Modified: 2015-08-09 For support, suggestions and enquiries, contact cb@billyab.co.uk The moral right of "billyab" to be identified as the author of this work has been asserted. */ /* * Change Log * * 20150802 2330 BB Remove Not X-Spam Requirements for commands * 20150809 1747 BB Removed Unused Colours. * 20150810 1650 BB Tidied Code and Matched Standards Better. * 20150810 1721 BB When Commands Invoked by User they are logged to app log (To Prevent Abuse). */ //Variables //TODO Remove This Var CB //var cb = {}; //Fix for my IDE. (DO NOT UNCOMMENT WHEN USING LIVE) var Application = { Name: "NickColour", // The name of the application Version: 0.78, // The current version of the application Author: "billyab", // The author of this version. Don't change this unless you modified something! OriginalAuthor: "billyab", // The original author, I.E. me. If you change this, you're a dick. It's not like it gets displayed anywhere Debug: false, // Whether the application is in debug (verbose) mode. Don't change this in prod, or you're a retard StartupTime: null, // The time the application started up. Don't set this, it gets set at runtime PreRelease: true //Whether app is prerelease }; var Settings = {}; var Colours = { // App Specific Colours HiPurple: "#f37ef3", AliceBlue: "#F0F8FF", IndianRed: "#CD5C5C", Black: "#000000", Blue: "#0000FF", Brown: "#A52A2A", CadetBlue: "#5F9EA0", Cyan: "#00FFFF", DarkBlue: "#00008B", DarkGrey: "#A9A9A9", DarkGreen: "#006400", DarkRed: "#8B0000", DeepPink: "#FF1493", Gold: "#FFD700", Grey: "#808080", Green: "#008000", HotPink: "#FF69B4", LightBlue: "#ADD8E6", LightGrey: "#D3D3D3", LightGreen: "#90EE90", LightPink: "#FFB6C1", Orange: "#FFA500", Pink: "#FFC0CB", Purple: "#800080", Red: "#FF0000", Silver: "#C0C0C0", Violet: "#EE82EE", White: "#FFFFFF", Yellow: "#FFFF00" }; var nbsp = '\u00a0'; var tab = nbsp + nbsp; var app_notice_newline = '\n' + Application.Name + ': '; var dashLine = "------------------------------------------------------------"; var aliases = {}; // aliases[user] = alias var users = {}; // users[alias] = user //CB Settings cb.settings_choices = [ {name: 'nicknames', type: 'str', minLenght: 0, label: "Nicknames user@nick e.g billyab@TheMaster"}, { name: 'show_alias', type: 'choice', choice2: 'No', choice1: 'Yes', defaultValue: 'Yes', label: "Show Nicknames (Obviously the reason for this bot!)" }, { name: 'show_mod', type: 'choice', choice2: 'No', choice1: 'Yes', defaultValue: 'No', label: "Show Mod status next to name/nick" }, { name: 'allow_mod', type: 'choice', choice2: 'No', choice1: 'Yes', defaultValue: 'Yes', label: "Allow Mods to edit Nicknames" } ]; //Functions //Nickname Functions function ParseNick() { var presets, index, length, value; presets = (Settings.nicknames || '').split(/[,]+/); for (index = 0, length = presets.length; index < length; index++) { value = presets[index].split('@'); if (value.length === 2 && value[0].length && value[1].length) { appLog(set_alias(value[0].toLowerCase().trim(), parse_ucodes_and_emotes(value[1])).join(', ')); } else { cb.sendNotice('Error: malformed alias record (user@alias): ' + presets[index], cb.room_slug, "#f37e7e"); } } } function set_alias(user, alias) { // aliases[user] = alias // users[alias] = user var out = []; if (alias.length) { if (users.hasOwnProperty(alias)) { } if (user.length) { out.push(user + '\'s alias \'' + alias + '\' added.'); aliases[user] = alias; users[alias] = user; } } else if (user.length) { if (aliases.hasOwnProperty(user)) { } } return out; } //END NickName Functions // On Message Function function BBonMessage(oMsg) { //TODO Split into separate Functions. var //start = (new Date().valueOf()), message = oMsg.m, originalmessage = message, user = oMsg.user, originaluser = user.toLowerCase(), bbNick, bAuthor = (user === Application.OriginalAuthor), bBroadcaster = (user === cb.room_slug), bModerator = (oMsg.is_mod), //bPM = oMsg.hasOwnProperty('is_pm') && oMsg['is_pm'], reCmd = /\/(nick|#[0-3])?(nickhelp|nickmod|nicklog|nicknames?|\?)\s*([^\/]*)/ig, cmd, target, key, arg, match, aliases_key, help, slot, out; //Check for Commands //if (!oMsg['X-Spam'] && message.trim()[0] === '/') { if (message.trim()[0] === '/') { while ((cmd = reCmd.exec(message)) !== null) { target = (cmd[1] || 'nick').toLowerCase(); if (target === 'nick' || target === slot) { oMsg['X-Spam'] = true; // suppress command broadcast arg = (cmd[3] || '').trim(); key = cmd[2].toLowerCase(); switch (key) { case 'nicklog': if ((bBroadcaster || bAuthor) && appLog.hasOwnProperty('log')) { cb.sendNotice(Application.Name + ': Log:\n' + tab + appLog.log.join('\n' + tab), user, Colours.HiPurple); } break; case 'nickname': if (bBroadcaster || bModerator) { if (Settings.allow_mod === "No" && bModerator) { //Mods Not Allowed cb.sendNotice(Application.Name + ': Sorry the broadcaster has not allowd Mods to modify this bot therfore, /' + (cmd[1] || '') + cmd[2] + ' is a broadcaster-only command.', user, Colours.IndianRed); } else { //Process Command if ((match = /^([a-zA-Z0-9_]{3,})(\s+.+)?$/.exec(arg)) !== null) { cb.sendNotice(Application.Name + ": " + set_alias(match[1].toLowerCase(), parse_ucodes_and_emotes((match[2] || '').trim())).join(app_notice_newline), user, Colours.AliceBlue); appLog(user + ": Set " + match[1] + "\'s Nickname to " + parse_ucodes_and_emotes((match[2] || 'NICKNAME REMOVED').trim())); } else { cb.sendNotice(Application.Name + ': Error: ' + (arg.length ? 'Unknown' : 'Missing') + ' /' + (cmd[1] || '') + cmd[2] + ' arguments: ' + arg, user, Colours.IndianRed); } } } else { cb.sendNotice(Application.Name + ': Sorry, /' + (cmd[1] || '') + cmd[2] + ' is a broadcaster-only command.', user, Colours.IndianRed); } break; case 'nicknames': if (bBroadcaster || bAuthor) { out = []; for (aliases_key in aliases) { if (aliases.hasOwnProperty(aliases_key)) { out.push(aliases_key + '@' + encode_ucodes(aliases[aliases_key])); } } cb.sendNotice(Application.Name + ': Nicknames List: ' + (out.length ? out.join(', ') : '(empty)'), user, Colours.HiPurple); } else { cb.sendNotice(Application.Name + ': Sorry, /' + (cmd[1] || '') + cmd[2] + ' is a broadcaster-only command.', user, Colours.IndianRed); } break; case 'nickmod': if (bBroadcaster || bAuthor) { //Toggle Mod Access if (Settings.allow_mod === "Yes") { //Dissable Settings.allow_mod = "No"; cb.sendNotice(Application.Name + ': Mod Access : REVOKED', user, Colours.HiPurple); appLog("Mod Access : REVOKED - by " + user); } else if (Settings.allow_mod === "No") { //Enable Settings.allow_mod = "Yes"; cb.sendNotice(Application.Name + ': Mod Access : ENABLED', user, Colours.HiPurple); appLog("Mod Access : ENABLED - by " + user); } } else { cb.sendNotice(Application.Name + ': Sorry, /' + (cmd[1] || '') + cmd[2] + ' is a broadcaster-only command.', user, Colours.IndianRed); } break; case '?': case 'nickhelp': out = []; if (bAuthor) { out.push(Application.Name + ': Version: ' + Application.Version + '\n' + Application.Name); } help = Application.Name + ': In-chat Commands:\n'; // commands only available to broadcaster and Bod if (bBroadcaster || bModerator) { help += tab + '/nickname user nickname - set the nick for user\n'; } // commands only available to broadcaster and author (for debug purposes) if (bBroadcaster || bAuthor) { help += tab + '/nicknames - display list of nicknames \n'; help += tab + '/nickmod - Toggle Mod Access to this Bot \n'; } help += tab + '/?\n' + tab + '/nickhelp - show this message'; if (bBroadcaster) { help += '\nTo adjust other settings, deactivate & re-launch bot.'; } out.push(help); cb.sendNotice(out.join(app_notice_newline), user, Colours.AliceBlue); break; default: cb.sendNotice('Sorry, unknown command: /' + (cmd[1] || '') + cmd[2] + '\n' + tab + 'Type /help for a list of available in-chat commands.', user, Colours.IndianRed); break; } } } } //END COMMAND CHECKING //appLog("New Message From " + oMsg['user']); bbNick = ""; if (!oMsg['X-Spam']) { if (Settings.show_alias === "Yes") { if (aliases.hasOwnProperty(originaluser) && oMsg.m[0] !== '!' && oMsg.m[0] !== '/') { // note. mustn't tag messages beginning with possible command qualifiers, cos it'll break other scripts. bbNick += "[" + aliases[originaluser] + "]"; } } if (Settings.show_mod === "Yes" && bModerator && oMsg.m[0] !== '!' && oMsg.m[0] !== '/') { bbNick += "{MOD}"; } if (bbNick !== "") { oMsg.m = bbNick + " " + originalmessage; } else { oMsg.m = originalmessage; } } return oMsg; } //END OnMessage Function //Misc Functions function appLog(str) { var me = appLog;//arguments.callee; function timeStamp() { var a = new Date(), b = /(..)(:..)(:..)/.exec(a), c = b[1] % 12 || 12; return (10 > c ? "0" + c : c) + b[2] + b[3] + " " + (12 > b[1] ? "A" : "P") + "M : "; } if (str && typeof str === 'string') { if (!me.hasOwnProperty('log')) { me.log = []; } me.log.push(timeStamp() + str); if (me.log.length > 25) { me.log.shift(); } str = Application.Name + ': ' + str; str = (str.replace(/\+/g, '\uff0b')).replace(/&/g, encodeURIComponent('&')); // seemingly cb.log also swallows & and + } if (Application.PreRelease || !me.hasOwnProperty('log')) { cb.log(str.replace(/(\r\n|\n|\r|\\n)/gm, ' ').trim()); // note. logs can't include newlines } } function uniqueArray(element, index, object) { return object.indexOf(element) === index; } function parse_ucodes_and_emotes(str) { // convert unicode escape sequences \uXXXX into character equivalents var reUCode = /\\u([0-9a-f]{4})/gi, ucode, ucodes = [], index, length; str = str || ''; while ((ucode = reUCode.exec(str)) !== null) { ucodes.push(ucode[1]); } if (ucodes.length) { // discard duplicate ucodes ucodes = ucodes.filter(uniqueArray); // replace each ucode with unicode char for (index = 0, length = ucodes.length; index < length; index++) { str = str.replace(new RegExp('\\\\u' + ucodes[index], 'gi'), String.fromCharCode(parseInt(ucodes[index], 16))); } } // replace winks, smiles and woots str = str.replace(/(^|\s|\\n);-*\)/gm, '$1:wink ').replace(/(^|\s|\\n):-*\)/gm, '$1:smile ').replace(/(^|\s|\\n):D/gm, '$1:woot '); str = str.replace(/(\r\n|\n|\r|\\n)/gm, ' ').trim(); // note. messages can't include newlines if (/(^|\s):([\w\-][\w\-]+)$/.test(str)) { // check for :emote at end of str str += ' '; } if (/^:([\w\-][\w\-]+)(\s|$)/.test(str)) { // check for :emote at start of str str = ' ' + str; } return str; } function encode_ucodes(str) { // escape spaces, commas, commercial ats, etc as unicode escape sequences \uXXXX str = str || ''; str = str.trim(); str = str.replace(/\s/g, '\\u0020').replace(/,/g, '\\u002c').replace(/@/g, '\\u0040'); return str.replace(/[^\u0020-\u007e]/g, function (match) { return '\\u' + (match.charCodeAt(0) + 0x10000).toString(16).slice(1).toLowerCase(); }); } //END Misc Functions //CB Handlers cb.onMessage(function (oMsg) { //'use strict'; return BBonMessage(oMsg); }); cb.onEnter(function(viewer) { if (viewer.is_mod) cb.sendNotice(dashLine+"\n* Broadcaster '" + cb.room_slug + "' is running "+Application.Name+".\n\n* Type /nickhelp for a list of available commands.\n* Simple Usage: /nickname Username Nickname \n"+dashLine,viewer.user,'',Colours.Purple,'bold'); }); function init() { //Log Start Time var start = (new Date().valueOf()); //cb.log("Running BB Test Bot 1"); if (Application.PreRelease) { appLog('This Application is still in development by BillyAB'); cb.sendNotice('This Application is still in development by BillyAB', cb.room_slug); } appLog('Version: ' + Application.Version); /*if (Settings.hasOwnProperty('slot')) { var slot = '#' + cb.settings.slot; }*/ //Assign Settings Settings = { nicknames: cb.settings.nicknames, show_alias: cb.settings.show_alias, show_mod: cb.settings.show_mod, allow_mod: cb.settings.allow_mod }; ParseNick(); appLog('init: ' + ((new Date().valueOf()) - start) + 'ms'); } init();
© Copyright Chaturbate 2011- 2024. All Rights Reserved.