From 921da917a4dd5465601571320bc8714b57288337 Mon Sep 17 00:00:00 2001 From: reality Date: Tue, 15 Jan 2013 17:23:14 +0000 Subject: [PATCH] Command hooks [#86] * Command API function to addHook(command, callback) * Commands open to callbacks must return information or indicate failed completion by return false * Hooks to be added in module onLoad * Command loop checks for available hooks and return state, then applies with module scope. * onLoad running moved to end of all module loading to facilitate this without order problems * Added example for ~qadd dent --- modules/command/api.js | 10 ++++++++++ modules/command/command.js | 7 ++++++- modules/dent/dent.js | 6 ++++++ modules/quotes/commands.js | 6 ++---- run.js | 10 ++++++---- 5 files changed, 30 insertions(+), 9 deletions(-) diff --git a/modules/command/api.js b/modules/command/api.js index 60509e4..2347977 100644 --- a/modules/command/api.js +++ b/modules/command/api.js @@ -62,6 +62,16 @@ var api = function(dbot) { applies = true; } return applies; + }, + + 'addHook': function(command, callback) { + console.log('adding hook'); + if(_.has(dbot.commands, command)) { + if(!_.has(dbot.commands[command], 'hooks')) { + dbot.commands[command].hooks = []; + } + dbot.commands[command].hooks.push(callback); + } } }; }; diff --git a/modules/command/command.js b/modules/command/command.js index 57aa9a3..fd3606d 100644 --- a/modules/command/command.js +++ b/modules/command/command.js @@ -30,7 +30,12 @@ var command = function(dbot) { if(this.api.applyRegex(commandName, event)) { try { var command = dbot.commands[commandName]; - command.apply(dbot.modules[command.module], [event]); + var results = command.apply(dbot.modules[command.module], [event]); + if(_.has(command, 'hooks') && results !== false) { + _.each(command['hooks'], function(hook) { + hook.apply(hook.module, results); + }, this); + } } catch(err) { if(dbot.config.debugMode == true) { event.reply('- Error in ' + commandName + ':'); diff --git a/modules/dent/dent.js b/modules/dent/dent.js index 064b732..15d7639 100644 --- a/modules/dent/dent.js +++ b/modules/dent/dent.js @@ -32,6 +32,12 @@ var dent = function(dbot) { } }; this.commands['~dent'].regex = [/^~dent (.+)$/, 2]; + + this.onLoad = function() { + dbot.api.command.addHook('~qadd', function(key, text) { + this.api.post(key + ': ' + text); + }.bind(this)); + }.bind(this); }; exports.fetch = function(dbot) { diff --git a/modules/quotes/commands.js b/modules/quotes/commands.js index c6061ad..824dcb5 100644 --- a/modules/quotes/commands.js +++ b/modules/quotes/commands.js @@ -187,11 +187,9 @@ var commands = function(dbot) { 'count': quotes[key].length })); - // TODO hook - if(_.has(dbot.api, 'dent')) { - dbot.api.dent.post(key + ': ' + text); - } + return { 'key': key, 'text': text }; } + return false; }, '~rq': function(event) { diff --git a/run.js b/run.js index 156ba3b..0528cfc 100644 --- a/run.js +++ b/run.js @@ -229,10 +229,6 @@ DBot.prototype.reloadModules = function() { return this.name; } - if(module.onLoad) { - module.onLoad(); - } - this.modules[module.name] = module; } catch(err) { console.log(this.t('module_load_error', {'moduleName': name})); @@ -245,6 +241,12 @@ DBot.prototype.reloadModules = function() { }.bind(this)); if(_.has(this.modules, 'web')) this.modules.web.reloadPages(); + + _.each(this.modules, function(module, name) { + if(module.onLoad) { + module.onLoad(); + } + }, this); this.save(); };