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')._;
|
2013-04-24 22:45:20 +02:00
|
|
|
|
2012-12-11 17:04:52 +01:00
|
|
|
var command = function(dbot) {
|
|
|
|
/**
|
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-08-13 01:18:40 +02:00
|
|
|
if(commandName.charAt(0) != '~') {
|
|
|
|
return;
|
|
|
|
}
|
2013-01-14 17:24:38 +01:00
|
|
|
if(!_.has(dbot.commands, commandName)) {
|
2013-08-17 20:13:38 +02:00
|
|
|
if(_.has(dbot.modules, 'quotes')) {
|
|
|
|
var key = event.message.substring(1);
|
2013-09-01 00:57:29 +02:00
|
|
|
dbot.api.quotes.getInterpolatedQuote(event.server,
|
|
|
|
event.channel.name, event.user, key, function(quote) {
|
2013-08-17 20:13:38 +02:00
|
|
|
if(quote) {
|
|
|
|
event.reply(key + ': ' + quote);
|
|
|
|
} else if(_.has(dbot.modules, 'spelling')) {
|
|
|
|
var commands = _.keys(dbot.commands)
|
|
|
|
winner = false,
|
|
|
|
closestMatch = Infinity;
|
2013-08-13 01:14:44 +02:00
|
|
|
|
2013-08-17 20:13:38 +02:00
|
|
|
_.each(commands, function(command) {
|
|
|
|
var distance = dbot.api.spelling.distance(commandName, command);
|
|
|
|
if(distance < closestMatch) {
|
|
|
|
closestMatch = distance;
|
|
|
|
winner = command;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-08-28 01:23:33 +02:00
|
|
|
if(closestMatch < 1) {
|
2013-08-17 20:13:38 +02:00
|
|
|
event.reply(commandName + ' not found. Did you mean ' + winner + '?');
|
|
|
|
return;
|
|
|
|
} else if(_.has(dbot.modules, 'quotes')) {
|
|
|
|
dbot.api.link.udLookup(key, function(word, definition) {
|
|
|
|
if(word) {
|
|
|
|
event.reply(key + '[UD]: ' + definition);
|
|
|
|
} else {
|
|
|
|
event.reply(dbot.t('category_not_found', { 'category': key }));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
2013-08-13 01:14:44 +02:00
|
|
|
}
|
2013-08-17 20:13:38 +02:00
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
2013-08-13 01:14:44 +02:00
|
|
|
|
2013-08-17 20:13:38 +02:00
|
|
|
else if(_.has(dbot.modules, 'quotes')) {
|
2013-01-14 17:47:48 +01:00
|
|
|
commandName = '~';
|
2013-01-24 00:39:42 +01:00
|
|
|
} else {
|
2013-01-14 17:47:48 +01:00
|
|
|
return;
|
2013-01-24 00:39:42 +01:00
|
|
|
}
|
2013-01-14 17:47:48 +01:00
|
|
|
}
|
2013-04-11 23:21:12 +02:00
|
|
|
|
2013-09-07 19:55:48 +02:00
|
|
|
this.api.hasAccess(event.rUser, event.channel, 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) {
|
2013-04-24 22:45:20 +02:00
|
|
|
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}));
|
|
|
|
}
|
2013-05-26 16:14:01 +02:00
|
|
|
} else if(!hasAccess) {
|
|
|
|
if(this.config.accessOutput) {
|
|
|
|
event.reply(dbot.t('access_denied', { 'user': event.user }));
|
|
|
|
}
|
2013-07-25 00:48:35 +02:00
|
|
|
} else if(!isIgnoring && _.has(dbot.commands, commandName) && !dbot.commands[commandName].disabled) {
|
2013-04-23 21:07:23 +02:00
|
|
|
if(this.api.applyRegex(commandName, event)) {
|
|
|
|
try {
|
2013-10-25 17:26:53 +02:00
|
|
|
var command = dbot.commands[commandName],
|
|
|
|
results;
|
|
|
|
if(_.has(command, 'resolver')) {
|
|
|
|
event.res = [];
|
|
|
|
command.resolver(event, function(err) {
|
|
|
|
if(!err) {
|
|
|
|
results = command.apply(dbot.modules[command.module], [event]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
results = command.apply(dbot.modules[command.module], [event]);
|
|
|
|
}
|
2013-04-23 21:07:23 +02:00
|
|
|
} catch(err) {
|
|
|
|
if(dbot.config.debugMode == true) {
|
2013-04-30 18:44:39 +02:00
|
|
|
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);
|
2013-04-30 18:44:39 +02:00
|
|
|
|
|
|
|
_.each(stack, function(stackLine, index) {
|
|
|
|
event.reply('- Stack[' + index + ']: ' +
|
|
|
|
stackLine.trim());
|
|
|
|
});
|
2013-04-23 21:07:23 +02:00
|
|
|
}
|
2013-04-10 03:28:53 +02:00
|
|
|
}
|
2013-07-25 00:42:02 +02:00
|
|
|
if(!_.include(['~reload', '~load', '~unload', '~setconfig'], commandName)) dbot.api.event.emit('command', [ event ]);
|
2013-04-23 21:07:23 +02:00
|
|
|
dbot.save();
|
2013-01-14 17:02:40 +01:00
|
|
|
} 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'));
|
|
|
|
}
|
2013-04-10 03:28:53 +02:00
|
|
|
}
|
2012-12-23 03:25:58 +01:00
|
|
|
}
|
2013-04-11 22:12:29 +02:00
|
|
|
}
|
2013-04-24 22:45:20 +02:00
|
|
|
}.bind(this));
|
2013-04-10 03:28:53 +02:00
|
|
|
}.bind(this));
|
2013-04-11 22:12:29 +02:00
|
|
|
}.bind(this));
|
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
|
|
|
};
|