Bots Home
|
Create an App
frame3
Author:
root202
Description
Source Code
Launch Bot
Current Users
Created by:
Root202
/** * c_frame - A simple CB bot framework * @version v0.0.1 * @author root202 * @link * @license Proprietary */ "use strict";function dynamicSort(i){var s=1;return"-"===i[0]&&(s=-1,i=i.substr(1)),function(e,a){return(e[i]<a[i]?-1:e[i]>a[i]?1:0)*s}}var frame={state:{},modules:[],settings:{},commands:{},global_alias_map:{},msg_pipeline:[],initial_top_levels:[{command:"help",alias:["?"],callback:function(e,a,i){cb.sendNotice("frame provides a framework for loading modules. To see the list of modules, use the /"+e.cmd_prefix+" list_modules command",[i.user]),cb.sendNotice("To see the list of top-level commands, use the '/"+e.cmd_prefix+" help' command",[i.user])}},{command:"list_modules",alias:["list"],callback:function(e,a,i){for(var s in e.modules){var n=e.modules[s];cb.sendNotice("The module "+n.name+" is loaded, to talk to it use '/"+e.cmd_prefix+" "+n.cmd_prefix+" <command_here>'",[i.user])}}}],initial_modules:[{name:"cb_hooks",cmd_prefix:null,event_listeners:[{event:"init",callback:function(n,e){void 0===arguments[2]||arguments[2];cb.log("running callback for cb_hooks init event"),cb.onTip(function(e){n.event_bus.triggerEvent(n,"tip",{tip:e})}),cb.onMessage(function(e){if(cb.log("got message "+e.m),"/"===e.m[0]){cb.log(e.m+" starts with `/`");var a=e.m.split(" ");if(n.global_alias_map.hasOwnProperty(a[0]))return cb.log(e.m+" is a registered global alias, executing the command"),n.global_alias_map[a[0]](n,a[0],e);if(e.m.substring(0,n.cmd_prefix.length+1)==="/"+n.cmd_prefix){if(!a[1]||""===a[1])return cb.log(e.m+" doesn't have an associated module to look for, ejecting"),e;if(n.top_level_commands.hasOwnProperty(a[1]))return cb.log(e.m+" is a registered top-level command, executing"),n.top_level_commands[a[1]](n,a[1],e),e;if(!a[2]||""===a[2])return cb.log(e.m+" doesn't have a command for the specified module, ejecting..."),e;var i=n.cmd_prefix+"::"+a[1]+"::"+a[2];if(n.commands.hasOwnProperty(i))return cb.log(e.m+" is a registered module command, executing"),n.commands[i](n,a[2],e)}}for(var s in n.event_bus.triggerEvent(n,"message",{message:e}),cb.log(e.m+" is being passed through the message pipeline"),n.msg_pipeline){e=n.msg_pipeline[s].callback(n,e)}return n.event_bus.triggerEvent(n,"message.post",{message:e}),e}),cb.onEnter(function(e){n.event_bus.triggerEvent(n,"enter",{user:e})}),cb.onLeave(function(e){n.event_bus.triggerEvent(n,"leave",{user:e})})}}],commands:[{command:"help",alias:["?"],callback:function(e,a,i){cb.sendNotice("The cb_hooks module does not have any commands",[i.user])}}]}],event_bus:{event_listeners:{},triggerEvent:function(e,a){var i=void 0===arguments[2]?{}:arguments[2];if(cb.log("event "+a+" triggered"),this.event_listeners.hasOwnProperty(a))for(var s in this.event_listeners[a]){(n=this.event_listeners[a][s]).hasOwnProperty("callback")&&n.callback(e,a,i)}for(var s in this.event_listeners["*"]){var n;(n=this.event_listeners["*"][s]).hasOwnProperty("callback")&&n.callback(e,a,i)}},addEventListener:function(e,a){var i=void 0===arguments[2]?0:arguments[2];cb.log("event listener for "+e+" added"),this.event_listeners.hasOwnProperty(e)||(this.event_listeners[e]=[]),this.event_listeners[e].push({callback:a,priority:i}),this.event_listeners[e].sort(dynamicSort("-priority"))}},cmd_prefix:"frame",top_level_commands:{},addMessagePipelineCommand:function(e){var a=void 0===arguments[1]?0:arguments[1];cb.log("message pipeline command added"),this.msg_pipeline.push({callback:e,priority:a}),this.msg_pipeline.sort(dynamicSort("-priority"))},addMessagePipelineCommands:function(){var e=void 0===arguments[0]?[]:arguments[0];for(var a in e){var i=e[a];this.addMessagePipelineCommand(i.callback,i.hasOwnProperty("priority")?i.priority:0)}},addModule:function(e){if(cb.log("module "+e.name+" added"),this.modules.push(e),e.hasOwnProperty("event_listeners"))for(var a in e.event_listeners){var i=e.event_listeners[a];this.event_bus.addEventListener(i.event,i.callback,i.hasOwnProperty("priority")?i.priority:0)}if(e.hasOwnProperty("cmd_prefix")&&null!==e.cmd_prefix||(e.cmd_prefix=e.name),e.hasOwnProperty("commands"))for(var a in e.commands){var s=e.commands[a],n=this.cmd_prefix+"::"+e.cmd_prefix+"::"+s.command;if(this.commands[n]=s.callback,cb.log("command "+n+" added for module "+e.name),s.hasOwnProperty("alias")&&0<s.alias.length)for(var t in s.alias){var o=s.alias[t],l=frame.cmd_prefix+"::"+e.cmd_prefix+"::"+o;this.commands[l]=s.callback,cb.log(l+" added as an alias to "+n)}if(s.hasOwnProperty("global_alias")&&0<s.global_alias.length)for(var r in s.global_alias){o=s.global_alias[r];this.global_alias_map["/"+o]=s.callback,cb.log(o+" added as a global alias to "+n)}}},addModules:function(){var e=void 0===arguments[0]?[]:arguments[0];for(var a in e){var i=e[a];this.addModule(i)}},addTopLevelCommand:function(e){if(this.top_level_commands[e.command]=e.callback,cb.log(e.command+" added as a top level command"),e.hasOwnProperty("alias")&&0<e.alias.length)for(var a in e.alias){var i=e.alias[a];this.top_level_commands[i]=e.callback,cb.log(i+" added as an alias to top level command "+e.command)}},addTopLevelCommands:function(){var e=void 0===arguments[0]?[]:arguments[0];for(var a in e)this.addTopLevelCommand(e[a])},init:function(){var e=void 0===arguments[0]?[]:arguments[0];this.addTopLevelCommands(this.initial_top_levels),this.addModules(this.initial_modules),this.addModules(e),this.event_bus.triggerEvent(this,"init")}}; frame.init();
© Copyright Chaturbate 2011- 2024. All Rights Reserved.