Bots Home
|
Create an App
hightip_module
Author:
aety
Description
Source Code
Launch Bot
Current Users
Created by:
Aety
/** * @link https://stackoverflow.com/a/4760279/3033603 * @param property * @returns {function(*, *): number} */ function dynamicSort(property) { var sortOrder = 1; if (property[0] === "-") { sortOrder = -1; property = property.substr(1); } return function (a, b) { var result = (a[property] < b[property]) ? -1 : (a[property] > b[property]) ? 1 : 0; return result * sortOrder; }; } /** * * @type {{state: {}, modules: {}, settings: {}, event_bus: {event_listeners: {}, triggerEvent: event_bus.triggerEvent, addEventListener: event_bus.addEventListener}, addModule: frame.addModule, bootstrapModule: frame.bootstrapModule, init: frame.init}} */ let frame = { state: {}, modules: [], settings: {}, commands: {}, global_alias_map: {}, msg_pipeline: [], initial_top_levels: [ { command: "help", alias: ["?"], callback: function (frame, command, msg) { cb.sendNotice("frame provides a framework for loading modules. To see the list of modules, use the /" + frame.cmd_prefix + " list_modules command", msg.user); cb.sendNotice("To see the list of top-level commands, use the '/" + frame.cmd_prefix + " help' command", msg.user); } }, { command: "list_modules", alias: ["list"], callback: function (frame, command, msg) { for (let i in frame.modules) { let module = frame.modules[i]; cb.sendNotice("The module " + module.name + " is loaded, to talk to it use " + "'/" + frame.cmd_prefix + " " + module.cmd_prefix + " <command_here>'", msg.user); } } } ], initial_modules: [ { name: "cb_hooks", cmd_prefix: null, event_listeners: [ { event: "init", /** * An example module event listener callback, implements CB specific event triggers on the init event, as well as `/` command delegation * @param frame * @param event * @param params */ callback: function (frame, event, params = {}) { cb.log("running callback for cb_hooks init event"); cb.onTip(function (tip) { frame.event_bus.triggerEvent(frame, "tip", { tip: tip }); }); //command are like this: /<cmd_prefix> <module.cmd_prefix> <command>, e.g. /frame cb_hooks help OR /<cmd_prefix> <top_level_command> cb.onMessage( /** * * @param msg * @returns {*} */ function (msg) { cb.log("got message " + msg.m); /* Here we're checking if (a) the message starts with `/` and (b) if the message starts with `/` + frame.cmd_prefix */ if (msg.m[0] === '/') { cb.log(msg.m + " starts with `/`"); //split the message text let message = msg.m.split(" "); if (frame.global_alias_map.hasOwnProperty(message[0])) { cb.log(msg.m + " is a registered global alias, executing the command"); t_msg = frame.global_alias_map[message[0]](frame, message[0], msg); if (t_msg !== undefined && t_msg !== null) { msg = t_msg; } return msg; } if (msg.m.substring(0, frame.cmd_prefix.length + 1) === '/' + frame.cmd_prefix) { //if we don't have a module or top-level command if (!message[1] || message[1] === "") { cb.log(msg.m + " doesn't have an associated module to look for, ejecting"); return msg; } //if we're dealing with a top-level command, delegate and eject if (frame.top_level_commands.hasOwnProperty(message[1])) { cb.log(msg.m + " is a registered top-level command, executing"); t_msg = frame.top_level_commands[message[1]](frame, message[1], msg); //note that we're just passing the top-level command down if (t_msg !== undefined && t_msg !== null) { msg = t_msg; } return msg; } //if we don't have a module command if (!message[2] || message[2] === "") { cb.log(msg.m + " doesn't have a command for the specified module, ejecting..."); return msg; } //build out cmd key, skipping the leading cmd_prefix in the message let cmd_key = frame.cmd_prefix + "::" + message[1] + "::" + message[2]; //if the cmd key exists... if (frame.commands.hasOwnProperty(cmd_key)) { cb.log(msg.m + " is a registered module command, executing"); t_msg = frame.commands[cmd_key](frame, message[2], msg); if (t_msg !== undefined && t_msg !== null) { msg = t_msg; } return msg; } } } frame.event_bus.triggerEvent(frame, "message", { message: msg }); cb.log(msg.m + " is being passed through the message pipeline"); for (let i in frame.msg_pipeline) { let pipecmd = frame.msg_pipeline[i]; msg = pipecmd.callback(frame, msg); } frame.event_bus.triggerEvent(frame, "message.post", { message: msg }); return msg; }); cb.onEnter(function (user) { frame.event_bus.triggerEvent(frame, "enter", { user: user }); }); cb.onLeave(function (user) { frame.event_bus.triggerEvent(frame, "leave", { user: user }); }); } } ], commands: [ { command: "help", alias: ["?"], callback: function (frame, command, msg) { cb.sendNotice("The cb_hooks module does not have any commands", msg.user); } } ], } ], event_bus: { event_listeners: {}, triggerEvent: function (frame, event, params = {}) { cb.log("event " + event + " triggered"); if (this.event_listeners.hasOwnProperty(event)) { for (let i in this.event_listeners[event]) { let action = this.event_listeners[event][i]; if (!action.hasOwnProperty("callback")) { continue; } action.callback(frame, event, params); } } for (let i in this.event_listeners['*']) { let action = this.event_listeners['*'][i]; if (!action.hasOwnProperty("callback")) { continue; } action.callback(frame, event, params); } }, addEventListener: function (event, callback, priority = 0) { cb.log("event listener for " + event + " added"); if (!this.event_listeners.hasOwnProperty(event)) { this.event_listeners[event] = []; } this.event_listeners[event].push({ callback: callback, priority: priority }); this.event_listeners[event].sort(dynamicSort("priority")); //note: the sort order is reversed, i.e. the lowest priority value is executed first } }, cmd_prefix: "frame", top_level_commands: {}, /** * * @param callback * @param priority */ addMessagePipelineCommand: function (callback, priority = 0) { cb.log("message pipeline command added"); this.msg_pipeline.push({ callback: callback, priority: priority }); this.msg_pipeline.sort(dynamicSort("priority")); //note: the sort order is reversed, i.e. the lowest priority value is executed first }, /** * * @param commands */ addMessagePipelineCommands: function (commands = []) { for (let i in commands) { let pipecmd = commands[i]; this.addMessagePipelineCommand(pipecmd.callback, (pipecmd.hasOwnProperty("priority") ? pipecmd.priority : 0)); } }, /** * * @param module */ addModule: function (module) { cb.log("module " + module.name + " added"); this.modules.push(module); if (module.hasOwnProperty("event_listeners")) { for (let i in module.event_listeners) { let listener = module.event_listeners[i]; this.event_bus.addEventListener( listener.event, listener.callback, (listener.hasOwnProperty("priority") ? listener.priority : 0) ); } } /* Here we set the default cmd_prefix if it doesn't exist */ if (!module.hasOwnProperty("cmd_prefix") || module.cmd_prefix === null) { module.cmd_prefix = module.name; } if (module.hasOwnProperty("commands")) { for (let i in module.commands) { let cmd = module.commands[i]; let cmd_line = this.cmd_prefix + "::" + module.cmd_prefix + "::" + cmd.command; this.commands[cmd_line] = cmd.callback; cb.log("command " + cmd_line + " added for module " + module.name); if (cmd.hasOwnProperty("alias") && cmd.alias.length > 0) { for (let i in cmd.alias) { let a = cmd.alias[i]; let a_cmd_line = frame.cmd_prefix + "::" + module.cmd_prefix + "::" + a; this.commands[a_cmd_line] = cmd.callback; cb.log(a_cmd_line + " added as an alias to " + cmd_line); } } if (cmd.hasOwnProperty("global_alias") && cmd.global_alias.length > 0) { for (let i in cmd.global_alias) { let a = cmd.global_alias[i]; this.global_alias_map['/' + a] = cmd.callback; cb.log(a + " added as a global alias to " + cmd_line); } } } } if (module.hasOwnProperty("settings") && module.settings.length > 0) { if(!cb.hasOwnProperty("settings_choices") || cb.settings_choices == null || typeof cb.settings_choices == 'undefined') { cb.settings_choices = []; } for (let i in module.settings) { let setting = Object.assign({}, module.settings[i]); setting.name = module.name + "_" + module.settings[i].name; cb.settings_choices.push(setting); } } }, /** * * @param modules */ addModules: function (modules = []) { for (let i in modules) { let module = modules[i]; this.addModule(module); } }, /** * * @param cmd */ addTopLevelCommand: function (cmd) { this.top_level_commands[cmd.command] = cmd.callback; cb.log(cmd.command + " added as a top level command"); if (cmd.hasOwnProperty("alias") && cmd.alias.length > 0) { for (let i in cmd.alias) { let a = cmd.alias[i]; this.top_level_commands[a] = cmd.callback; cb.log(a + " added as an alias to top level command " + cmd.command); } } }, /** * * @param cmds */ addTopLevelCommands: function (cmds = []) { for (let i in cmds) { this.addTopLevelCommand(cmds[i]); } }, /** * * @param modules */ init: function (modules = []) { this.addTopLevelCommands(this.initial_top_levels); this.addModules(this.initial_modules); this.addModules(modules); for (let i in this.modules) { let module = this.modules[i]; if (cb.hasOwnProperty("settings") && module.hasOwnProperty("settings") && module.settings.length > 0) { if (!this.settings.hasOwnProperty(module.name)) { this.settings[module.name] = {}; } for (let y in module.settings) { let setting = module.settings[y]; cb.log("Setting " + module.name + "_" + setting.name + ": " + cb.settings[module.name + "_" + setting.name]); this.settings[module.name][setting.name] = cb.settings[module.name + "_" + setting.name]; } } } this.event_bus.triggerEvent(this, "init"); } }; let modules_to_load = []; modules_to_load.push({ name: "high_tipper_highlighter", cmd_prefix: null, settings: [ { name: 'highlight_color', type: 'str', label: 'Highlight Color for HT: #', minLength: 6, maxLength: 6, defaultValue: '88ffaa' } ], event_listeners: [ { event: "init", callback: function (frame, event, params = {}) { cb.log("adding message pipeline command for tipper message highlight"); frame.addMessagePipelineCommand(function(frame, msg) { if(frame.state.hasOwnProperty(this.name) && msg.user == frame.state[this.name].from_user) { msg.background = "#" + frame.settings[this.name].highlight_color; } return msg; }, 0); } }, { event: "tip", callback: function (frame, event, params = {}) { cb.log("user " + params.tip.from_user + " tipped!"); if(!frame.state.hasOwnProperty("high_tipper") || params.tip.amount > frame.state.high_tipper.amount) { cb.log("new high tiper!"); frame.state.high_tipper = params.tip; } } } ], commands: [], }); frame.init(modules_to_load);
© Copyright Chaturbate 2011- 2024. All Rights Reserved.