dbot/modules/command/command.js

65 lines
2.4 KiB
JavaScript
Raw Normal View History

/**
* Module Name: Command
* Description: An essential module which maps PRIVMSG input to an appropriate
* command and then runs that command, given the user isn't banned from or
* ignoring that command.
*/
var _ = require('underscore')._;
var command = function(dbot) {
this.dbot = dbot;
/**
* Run the appropriate command given the input.
*/
this.listener = function(event) {
var commandName = event.params[0];
2013-01-14 17:24:38 +01:00
if(!_.has(dbot.commands, commandName)) {
2013-01-24 00:39:42 +01:00
if(_.has(dbot.modules, 'quotes')) {
commandName = '~';
2013-01-24 00:39:42 +01:00
} else {
return;
2013-01-24 00:39:42 +01:00
}
}
if(this.api.isBanned(event.user, commandName)) {
2013-01-14 17:24:38 +01:00
event.reply(dbot.t('command_ban', {'user': event.user}));
} else {
if(!this.api.isIgnoring(event.user, commandName) &&
this.api.hasAccess(event.user, commandName) &&
2013-01-14 17:24:38 +01:00
dbot.commands[commandName].disabled !== true) {
if(this.api.applyRegex(commandName, event)) {
try {
var command = dbot.commands[commandName];
var results = command.apply(dbot.modules[command.module], [event]);
if(_.has(command, 'hooks') && results !== false) {
_.each(command['hooks'], function(hook) {
2013-01-15 18:38:08 +01:00
hook.apply(hook.module, _.values(results));
}, this);
}
} catch(err) {
2013-01-14 17:24:38 +01:00
if(dbot.config.debugMode == true) {
event.reply('- Error in ' + commandName + ':');
event.reply('- Message: ' + err);
event.reply('- Top of stack: ' + err.stack.split('\n')[1].trim());
}
}
2013-01-14 17:24:38 +01:00
dbot.save();
} else {
if(commandName !== '~') {
2013-01-14 17:24:38 +01:00
if(_.has(dbot.usage, commandName)) {
event.reply('Usage: ' + dbot.usage[commandName]);
} else {
2013-01-14 17:24:38 +01:00
event.reply(dbot.t('syntax_error'));
}
}
}
}
}
}.bind(this);
this.on = 'PRIVMSG';
};
exports.fetch = function(dbot) {
return new command(dbot);
};