3
0
mirror of https://github.com/reality/dbot.git synced 2024-11-24 04:49:25 +01:00
dbot/modules/command/command.js

74 lines
3.2 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) {
/**
* 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
}
}
this.api.hasAccess(event.rUser, commandName, function(hasAccess) {
2013-06-27 17:33:18 +02:00
dbot.api.ignore.isUserIgnoring(event.rUser, commandName, function(isIgnoring) {
dbot.api.ignore.isUserBanned(event.rUser, commandName, function(isBanned) {
if(isBanned) {
2013-05-27 19:48:36 +02:00
if(this.config.banOutput && commandName != '~') {
2013-05-27 19:45:30 +02:00
event.reply(dbot.t('command_ban', {'user': event.user}));
}
} else if(!hasAccess) {
if(this.config.accessOutput) {
event.reply(dbot.t('access_denied', { 'user': event.user }));
}
} else if(!isIgnoring && !dbot.commands[commandName].disabled) {
2013-04-23 21:07:23 +02:00
if(this.api.applyRegex(commandName, event)) {
try {
var command = dbot.commands[commandName];
var results = command.apply(dbot.modules[command.module], [event]);
} catch(err) {
if(dbot.config.debugMode == true) {
var stack = err.stack.split('\n').slice(1, dbot.config.debugLevel + 1);
2013-04-23 21:07:23 +02:00
event.reply('- Error in ' + commandName + ':');
event.reply('- Message: ' + err);
_.each(stack, function(stackLine, index) {
event.reply('- Stack[' + index + ']: ' +
stackLine.trim());
});
2013-04-23 21:07:23 +02:00
}
}
if(_.include(commandName, ['reload', 'load', 'unload'])) dbot.api.event.emit('command', [ event ]);
2013-04-23 21:07:23 +02:00
dbot.save();
} else {
2013-04-23 21:07:23 +02:00
if(commandName !== '~') {
if(_.has(dbot.usage, commandName)) {
event.reply('Usage: ' + dbot.usage[commandName]);
} else {
event.reply(dbot.t('syntax_error'));
}
}
}
}
}.bind(this));
}.bind(this));
}.bind(this));
}.bind(this);
this.on = 'PRIVMSG';
};
exports.fetch = function(dbot) {
return new command(dbot);
};