2012-12-11 17:04:52 +01:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2013-01-12 17:14:17 +01:00
|
|
|
var _ = require('underscore')._;
|
2012-12-11 17:04:52 +01:00
|
|
|
var command = function(dbot) {
|
2013-01-14 17:02:40 +01:00
|
|
|
this.dbot = dbot;
|
|
|
|
|
2012-12-11 17:04:52 +01:00
|
|
|
/**
|
2013-01-14 17:02:40 +01:00
|
|
|
* Run the appropriate command given the input.
|
2012-12-11 17:04:52 +01:00
|
|
|
*/
|
2013-01-14 17:02:40 +01:00
|
|
|
this.listener = function(event) {
|
|
|
|
var commandName = event.params[0];
|
2013-01-14 17:24:38 +01:00
|
|
|
if(!_.has(dbot.commands, commandName)) {
|
2013-01-14 17:47:48 +01:00
|
|
|
if(_.has(dbot.modules, 'quotes')) {
|
|
|
|
commandName = '~';
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-14 17:02:40 +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}));
|
2012-12-11 17:04:52 +01:00
|
|
|
} else {
|
2013-01-14 17:02:40 +01:00
|
|
|
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) {
|
2013-01-14 17:02:40 +01:00
|
|
|
if(this.api.applyRegex(commandName, event)) {
|
|
|
|
try {
|
2013-01-14 18:45:53 +01:00
|
|
|
var command = dbot.commands[commandName];
|
|
|
|
command.apply(dbot.modules[command.module], [event]);
|
2013-01-14 17:02:40 +01:00
|
|
|
} catch(err) {
|
2013-01-14 17:24:38 +01:00
|
|
|
if(dbot.config.debugMode == true) {
|
2013-01-14 17:02:40 +01:00
|
|
|
event.reply('- Error in ' + commandName + ':');
|
|
|
|
event.reply('- Message: ' + err);
|
|
|
|
event.reply('- Top of stack: ' + err.stack.split('\n')[1].trim());
|
2012-12-23 03:25:58 +01:00
|
|
|
}
|
2013-01-14 17:02:40 +01:00
|
|
|
}
|
2013-01-14 17:24:38 +01:00
|
|
|
dbot.save();
|
2013-01-14 17:02:40 +01:00
|
|
|
} else {
|
|
|
|
if(commandName !== '~') {
|
2013-01-14 17:24:38 +01:00
|
|
|
if(_.has(dbot.usage, commandName)) {
|
|
|
|
event.reply('Usage: ' + dbot.usage[commandName]);
|
2013-01-14 17:02:40 +01:00
|
|
|
} else {
|
2013-01-14 17:24:38 +01:00
|
|
|
event.reply(dbot.t('syntax_error'));
|
2012-12-11 17:04:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-01-14 17:02:40 +01:00
|
|
|
}
|
|
|
|
}.bind(this);
|
|
|
|
this.on = 'PRIVMSG';
|
2012-12-11 17:04:52 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.fetch = function(dbot) {
|
2013-01-14 17:02:40 +01:00
|
|
|
return new command(dbot);
|
2012-12-11 17:04:52 +01:00
|
|
|
};
|