Much better! [#131]

This commit is contained in:
reality 2013-01-14 16:24:38 +00:00
parent dc269b53b4
commit 5419a664dc
4 changed files with 84 additions and 78 deletions

View File

@ -1,68 +1,71 @@
var _ = require('underscore')._; var _ = require('underscore')._;
var api = { var api = function(dbot) {
'isBanned': function(user, command) { return {
var banned = false; 'isBanned': function(user, command) {
if(_.has(this.dbot.db.bans, command)) { var banned = false;
if(_.include(this.dbot.db.bans[command], user) || _.include(this.dbot.db.bans['*'], user)) { if(_.has(dbot.db.bans, command)) {
banned = true; if(_.include(dbot.db.bans[command], user) || _.include(dbot.db.bans['*'], user)) {
banned = true;
}
} }
} return banned;
return banned; },
},
/** /**
* Does the user have the correct access level to use the command? * Does the user have the correct access level to use the command?
*/ */
'hasAccess': function(user, command) { 'hasAccess': function(user, command) {
var access = true; var access = true;
var accessNeeded = this.dbot.commands[command].access; var accessNeeded = dbot.commands[command].access;
if(accessNeeded == 'admin') { if(accessNeeded == 'admin') {
if(!_.include(this.dbot.config.admins, user)) { if(!_.include(dbot.config.admins, user)) {
access = false; access = false;
}
} else if(accessNeeded == 'moderator') {
if(!_.include(dbot.config.moderators, user) &&
!_.include(dbot.config.admins, user)) {
access = false;
}
} }
} else if(accessNeeded == 'moderator') {
if(!_.include(this.dbot.config.moderators, user) && return access;
!_.include(this.dbot.config.admins, user)) { },
access = false;
/**
* Is user ignoring command?
*/
'isIgnoring': function(user, command) {
var module = dbot.commands[command].module;
var ignoring = false;
if(_.has(dbot.db.ignores, user) && _.include(dbot.db.ignores[user], module)) {
ignoring = true;
} }
} return ignoring;
},
return access; /**
}, * Apply Regex to event message, store result. Return false if it doesn't
* apply.
/** */
* Is user ignoring command? 'applyRegex': function(commandName, event) {
*/ var applies = false;
'isIgnoring': function(user, command) { if(_.has(dbot.commands[commandName], 'regex')) {
var module = this.dbot.commands[command].module; var cRegex = dbot.commands[commandName].regex;
var ignoring = false; var q = event.message.valMatch(cRegex[0], cRegex[1]);
if(_.has(this.dbot.db.ignores, user) && _.include(this.dbot.db.ignores[user], module)) { if(q) {
ignoring = true; applies = true;
} event.input = q;
return ignoring; }
}, } else {
/**
* Apply Regex to event message, store result. Return false if it doesn't
* apply.
*/
'applyRegex': function(commandName, event) {
var applies = false;
if(_.has(this.dbot.commands[commandName], 'regex')) {
var cRegex = this.dbot.commands[commandName].regex;
var q = event.message.valMatch(cRegex[0], cRegex[1]);
if(q) {
applies = true; applies = true;
event.input = q;
} }
} else { return applies;
applies = true;
} }
return applies; };
}
}; };
exports.fetch = api; exports.fetch = function(dbot) {
return api(dbot);
};

View File

@ -14,35 +14,34 @@ var command = function(dbot) {
* Run the appropriate command given the input. * Run the appropriate command given the input.
*/ */
this.listener = function(event) { this.listener = function(event) {
console.log(Object.keys(this));
var commandName = event.params[0]; var commandName = event.params[0];
if(!_.has(this.dbot.commands, commandName)) { if(!_.has(dbot.commands, commandName)) {
commandName = '~'; commandName = '~';
} }
if(this.api.isBanned(event.user, commandName)) { if(this.api.isBanned(event.user, commandName)) {
event.reply(this.dbot.t('command_ban', {'user': event.user})); event.reply(dbot.t('command_ban', {'user': event.user}));
} else { } else {
if(!this.api.isIgnoring(event.user, commandName) && if(!this.api.isIgnoring(event.user, commandName) &&
this.api.hasAccess(event.user, commandName) && this.api.hasAccess(event.user, commandName) &&
this.dbot.commands[commandName].disabled !== true) { dbot.commands[commandName].disabled !== true) {
if(this.api.applyRegex(commandName, event)) { if(this.api.applyRegex(commandName, event)) {
try { try {
this.dbot.commands[commandName](event); dbot.commands[commandName](event);
} catch(err) { } catch(err) {
if(this.dbot.config.debugMode == true) { if(dbot.config.debugMode == true) {
event.reply('- Error in ' + commandName + ':'); event.reply('- Error in ' + commandName + ':');
event.reply('- Message: ' + err); event.reply('- Message: ' + err);
event.reply('- Top of stack: ' + err.stack.split('\n')[1].trim()); event.reply('- Top of stack: ' + err.stack.split('\n')[1].trim());
} }
} }
this.dbot.save(); dbot.save();
} else { } else {
if(commandName !== '~') { if(commandName !== '~') {
if(_.has(this.dbot.usage, commandName)) { if(_.has(dbot.usage, commandName)) {
event.reply('Usage: ' + this.dbot.usage[commandName]); event.reply('Usage: ' + dbot.usage[commandName]);
} else { } else {
event.reply(this.dbot.t('syntax_error')); event.reply(dbot.t('syntax_error'));
} }
} }
} }

View File

@ -1,15 +1,16 @@
var _ = require('underscore')._; var _ = require('underscore')._;
var commands = { var commands = function(dbot) {
return {
'~usage': function(event) { '~usage': function(event) {
var commandName = event.params[1]; var commandName = event.params[1];
if(_.has(this.dbot.usage, commandName)) { if(_.has(dbot.usage, commandName)) {
event.reply(this.dbot.t('usage', { event.reply(dbot.t('usage', {
'command': commandName, 'command': commandName,
'usage': this.dbot.usage[commandName] 'usage': dbot.usage[commandName]
})); }));
} else { } else {
event.reply(this.dbot.t('no_usage_info', { event.reply(dbot.t('no_usage_info', {
'command': commandName 'command': commandName
})); }));
} }
@ -17,13 +18,13 @@ var commands = {
'~help': function(event) { '~help': function(event) {
var moduleName = event.params[1]; var moduleName = event.params[1];
if(!_.has(this.dbot.modules, moduleName)) { if(!_.has(dbot.modules, moduleName)) {
var moduleName = this.dbot.commands[moduleName].module; var moduleName = dbot.commands[moduleName].module;
} }
if(moduleName && _.has(this.dbot.config[moduleName], 'help')) { if(moduleName && _.has(dbot.config[moduleName], 'help')) {
var help = this.dbot.config[moduleName].help; var help = dbot.config[moduleName].help;
event.reply(this.dbot.t('help_link', { event.reply(dbot.t('help_link', {
'module': moduleName, 'module': moduleName,
'link': help 'link': help
})); }));
@ -31,9 +32,12 @@ var commands = {
if(!moduleName) { if(!moduleName) {
moduleName = event.params[1]; moduleName = event.params[1];
} }
event.reply(this.dbot.t('no_help', { 'module': moduleName })) event.reply(dbot.t('no_help', { 'module': moduleName }))
} }
} }
};
}; };
exports.fetch = commands; exports.fetch = function(dbot) {
return commands(dbot);
};

2
run.js
View File

@ -181,7 +181,7 @@ DBot.prototype.reloadModules = function() {
try { try {
var propertyKey = require.resolve(moduleDir + property); var propertyKey = require.resolve(moduleDir + property);
if(propertyKey) delete require.cache[propertyKey]; if(propertyKey) delete require.cache[propertyKey];
var propertyObj = require(moduleDir + property).fetch; var propertyObj = require(moduleDir + property).fetch(this);
} catch(err) { } catch(err) {
console.log(err.stack); console.log(err.stack);
return; return;