forked from GitHub/dbot
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:
parent
1bd1b5aa56
commit
921da917a4
@ -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);
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
|
@ -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 + ':');
|
||||
|
@ -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) {
|
||||
|
@ -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
10
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}));
|
||||
@ -246,6 +242,12 @@ DBot.prototype.reloadModules = function() {
|
||||
|
||||
if(_.has(this.modules, 'web')) this.modules.web.reloadPages();
|
||||
|
||||
_.each(this.modules, function(module, name) {
|
||||
if(module.onLoad) {
|
||||
module.onLoad();
|
||||
}
|
||||
}, this);
|
||||
|
||||
this.save();
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user