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) {
|
|
|
|
/**
|
|
|
|
* Is user banned from using command?
|
|
|
|
*/
|
|
|
|
var isBanned = function(user, command) {
|
|
|
|
var banned = false;
|
2013-01-12 17:14:17 +01:00
|
|
|
if(_.has(dbot.db.bans, command)) {
|
|
|
|
if(_.include(dbot.db.bans[command], user) || _.include(dbot.db.bans['*'], user)) {
|
2012-12-11 17:04:52 +01:00
|
|
|
banned = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return banned;
|
|
|
|
};
|
|
|
|
|
2013-01-12 10:38:13 +01:00
|
|
|
/**
|
|
|
|
* Does the user have the correct access level to use the command?
|
|
|
|
*/
|
|
|
|
var hasAccess = function(user, command) {
|
|
|
|
var access = true;
|
|
|
|
var accessNeeded = dbot.commands[command].access;
|
|
|
|
|
|
|
|
if(accessNeeded == 'admin') {
|
2013-01-12 17:14:17 +01:00
|
|
|
if(!_.include(dbot.config.admins, user)) {
|
2013-01-12 10:38:13 +01:00
|
|
|
access = false;
|
|
|
|
}
|
|
|
|
} else if(accessNeeded == 'moderator') {
|
2013-01-12 17:14:17 +01:00
|
|
|
if(!_.include(dbot.config.moderators, user) &&
|
|
|
|
!_.include(dbot.config.admins, user)) {
|
2013-01-12 10:38:13 +01:00
|
|
|
access = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return access;
|
|
|
|
};
|
|
|
|
|
2012-12-11 17:04:52 +01:00
|
|
|
/**
|
|
|
|
* Is user ignoring command?
|
|
|
|
*/
|
|
|
|
var isIgnoring = function(user, command) {
|
2013-01-12 17:14:17 +01:00
|
|
|
var module = dbot.commands[command].module;
|
2012-12-11 17:04:52 +01:00
|
|
|
var ignoring = false;
|
2013-01-12 17:14:17 +01:00
|
|
|
if(_.has(dbot.db.ignores, user) && _.include(dbot.db.ignores[user], module)) {
|
2012-12-11 17:04:52 +01:00
|
|
|
ignoring = true;
|
|
|
|
}
|
|
|
|
return ignoring;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Apply Regex to event message, store result. Return false if it doesn't
|
|
|
|
* apply.
|
|
|
|
*/
|
|
|
|
var applyRegex = function(commandName, event) {
|
|
|
|
var applies = false;
|
2013-01-12 17:14:17 +01:00
|
|
|
if(_.has(dbot.commands[commandName], 'regex')) {
|
2012-12-11 17:04:52 +01:00
|
|
|
var cRegex = dbot.commands[commandName].regex;
|
|
|
|
var q = event.message.valMatch(cRegex[0], cRegex[1]);
|
|
|
|
if(q) {
|
|
|
|
applies = true;
|
|
|
|
event.input = q;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
applies = true;
|
|
|
|
}
|
|
|
|
return applies;
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
'name': 'command',
|
|
|
|
'ignorable': false,
|
|
|
|
|
|
|
|
'commands': {
|
|
|
|
'~usage': function(event) {
|
|
|
|
var commandName = event.params[1];
|
2013-01-12 17:14:17 +01:00
|
|
|
if(_.has(dbot.usage, commandName)) {
|
2012-12-30 21:15:48 +01:00
|
|
|
event.reply(dbot.t('usage', {
|
|
|
|
'command': commandName,
|
|
|
|
'usage': dbot.usage[commandName]
|
|
|
|
}));
|
2012-12-11 17:04:52 +01:00
|
|
|
} else {
|
2012-12-30 21:15:48 +01:00
|
|
|
event.reply(dbot.t('no_usage_info', {
|
|
|
|
'command': commandName
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
'~help': function(event) {
|
|
|
|
var moduleName = event.params[1];
|
2013-01-12 17:14:17 +01:00
|
|
|
if(!_.has(dbot.modules, moduleName)) {
|
|
|
|
var moduleName = dbot.commands[moduleName].module;
|
2012-12-30 21:15:48 +01:00
|
|
|
}
|
|
|
|
|
2013-01-12 17:14:17 +01:00
|
|
|
if(moduleName && _.has(dbot.config[moduleName], 'help')) {
|
2012-12-30 21:30:49 +01:00
|
|
|
var help = dbot.config[moduleName].help;
|
2012-12-30 21:15:48 +01:00
|
|
|
event.reply(dbot.t('help_link', {
|
|
|
|
'module': moduleName,
|
|
|
|
'link': help
|
|
|
|
}));
|
|
|
|
} else {
|
|
|
|
if(!moduleName) {
|
|
|
|
moduleName = event.params[1];
|
|
|
|
}
|
|
|
|
event.reply(dbot.t('no_help', { 'module': moduleName }))
|
2012-12-11 17:04:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Run the appropriate command given the input.
|
|
|
|
*/
|
|
|
|
'listener': function(event) {
|
|
|
|
var commandName = event.params[0];
|
2013-01-12 17:14:17 +01:00
|
|
|
if(!_.has(dbot.commands, commandName)) {
|
2012-12-11 17:04:52 +01:00
|
|
|
commandName = '~';
|
|
|
|
}
|
|
|
|
|
|
|
|
if(isBanned(event.user, commandName)) {
|
|
|
|
event.reply(dbot.t('command_ban', {'user': event.user}));
|
|
|
|
} else {
|
2013-01-12 11:12:56 +01:00
|
|
|
if(!isIgnoring(event.user, commandName) &&
|
|
|
|
hasAccess(event.user, commandName) &&
|
|
|
|
dbot.commands[commandName].disabled !== true) {
|
2012-12-11 17:04:52 +01:00
|
|
|
if(applyRegex(commandName, event)) {
|
2012-12-23 03:25:58 +01:00
|
|
|
try {
|
|
|
|
dbot.commands[commandName](event);
|
|
|
|
} catch(err) {
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
}
|
2012-12-11 17:04:52 +01:00
|
|
|
dbot.save();
|
|
|
|
} else {
|
|
|
|
if(commandName !== '~') {
|
2013-01-12 17:14:17 +01:00
|
|
|
if(_.has(dbot.usage, commandName)) {
|
2012-12-11 21:31:11 +01:00
|
|
|
event.reply('Usage: ' + dbot.usage[commandName]);
|
2012-12-11 17:04:52 +01:00
|
|
|
} else {
|
|
|
|
event.reply(dbot.t('syntax_error'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
'on': 'PRIVMSG'
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.fetch = function(dbot) {
|
|
|
|
return command(dbot);
|
|
|
|
};
|
|
|
|
|