3
0
mirror of https://github.com/reality/dbot.git synced 2024-12-28 05:32:50 +01:00

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
This commit is contained in:
reality 2013-01-15 17:23:14 +00:00
parent 1bd1b5aa56
commit 921da917a4
5 changed files with 30 additions and 9 deletions

View File

@ -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);
}
}
};
};

View File

@ -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 + ':');

View File

@ -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) {

View File

@ -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) {

10
run.js
View File

@ -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();
};