forked from GitHub/dbot
Here is an experimental look at how modules will have to be as per [#131]
This commit is contained in:
parent
ba2ab323e3
commit
dc269b53b4
68
modules/command/api.js
Normal file
68
modules/command/api.js
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
var _ = require('underscore')._;
|
||||||
|
|
||||||
|
var api = {
|
||||||
|
'isBanned': function(user, command) {
|
||||||
|
var banned = false;
|
||||||
|
if(_.has(this.dbot.db.bans, command)) {
|
||||||
|
if(_.include(this.dbot.db.bans[command], user) || _.include(this.dbot.db.bans['*'], user)) {
|
||||||
|
banned = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return banned;
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Does the user have the correct access level to use the command?
|
||||||
|
*/
|
||||||
|
'hasAccess': function(user, command) {
|
||||||
|
var access = true;
|
||||||
|
var accessNeeded = this.dbot.commands[command].access;
|
||||||
|
|
||||||
|
if(accessNeeded == 'admin') {
|
||||||
|
if(!_.include(this.dbot.config.admins, user)) {
|
||||||
|
access = false;
|
||||||
|
}
|
||||||
|
} else if(accessNeeded == 'moderator') {
|
||||||
|
if(!_.include(this.dbot.config.moderators, user) &&
|
||||||
|
!_.include(this.dbot.config.admins, user)) {
|
||||||
|
access = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return access;
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Is user ignoring command?
|
||||||
|
*/
|
||||||
|
'isIgnoring': function(user, command) {
|
||||||
|
var module = this.dbot.commands[command].module;
|
||||||
|
var ignoring = false;
|
||||||
|
if(_.has(this.dbot.db.ignores, user) && _.include(this.dbot.db.ignores[user], module)) {
|
||||||
|
ignoring = true;
|
||||||
|
}
|
||||||
|
return ignoring;
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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;
|
||||||
|
event.input = q;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
applies = true;
|
||||||
|
}
|
||||||
|
return applies;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
exports.fetch = api;
|
||||||
|
|
@ -6,154 +6,52 @@
|
|||||||
*/
|
*/
|
||||||
var _ = require('underscore')._;
|
var _ = require('underscore')._;
|
||||||
var command = function(dbot) {
|
var command = function(dbot) {
|
||||||
/**
|
this.name = 'command';
|
||||||
* Is user banned from using command?
|
this.ignorable = false;
|
||||||
*/
|
this.dbot = dbot;
|
||||||
var isBanned = function(user, command) {
|
|
||||||
var banned = false;
|
|
||||||
if(_.has(dbot.db.bans, command)) {
|
|
||||||
if(_.include(dbot.db.bans[command], user) || _.include(dbot.db.bans['*'], user)) {
|
|
||||||
banned = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return banned;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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') {
|
|
||||||
if(!_.include(dbot.config.admins, user)) {
|
|
||||||
access = false;
|
|
||||||
}
|
|
||||||
} else if(accessNeeded == 'moderator') {
|
|
||||||
if(!_.include(dbot.config.moderators, user) &&
|
|
||||||
!_.include(dbot.config.admins, user)) {
|
|
||||||
access = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return access;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Is user ignoring command?
|
|
||||||
*/
|
|
||||||
var 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;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Apply Regex to event message, store result. Return false if it doesn't
|
|
||||||
* apply.
|
|
||||||
*/
|
|
||||||
var applyRegex = function(commandName, event) {
|
|
||||||
var applies = false;
|
|
||||||
if(_.has(dbot.commands[commandName], 'regex')) {
|
|
||||||
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];
|
|
||||||
if(_.has(dbot.usage, commandName)) {
|
|
||||||
event.reply(dbot.t('usage', {
|
|
||||||
'command': commandName,
|
|
||||||
'usage': dbot.usage[commandName]
|
|
||||||
}));
|
|
||||||
} else {
|
|
||||||
event.reply(dbot.t('no_usage_info', {
|
|
||||||
'command': commandName
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
'~help': function(event) {
|
|
||||||
var moduleName = event.params[1];
|
|
||||||
if(!_.has(dbot.modules, moduleName)) {
|
|
||||||
var moduleName = dbot.commands[moduleName].module;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(moduleName && _.has(dbot.config[moduleName], 'help')) {
|
|
||||||
var help = dbot.config[moduleName].help;
|
|
||||||
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 }))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Run the appropriate command given the input.
|
* Run the appropriate command given the input.
|
||||||
*/
|
*/
|
||||||
'listener': function(event) {
|
this.listener = function(event) {
|
||||||
|
console.log(Object.keys(this));
|
||||||
var commandName = event.params[0];
|
var commandName = event.params[0];
|
||||||
if(!_.has(dbot.commands, commandName)) {
|
if(!_.has(this.dbot.commands, commandName)) {
|
||||||
commandName = '~';
|
commandName = '~';
|
||||||
}
|
}
|
||||||
|
|
||||||
if(isBanned(event.user, commandName)) {
|
if(this.api.isBanned(event.user, commandName)) {
|
||||||
event.reply(dbot.t('command_ban', {'user': event.user}));
|
event.reply(this.dbot.t('command_ban', {'user': event.user}));
|
||||||
} else {
|
} else {
|
||||||
if(!isIgnoring(event.user, commandName) &&
|
if(!this.api.isIgnoring(event.user, commandName) &&
|
||||||
hasAccess(event.user, commandName) &&
|
this.api.hasAccess(event.user, commandName) &&
|
||||||
dbot.commands[commandName].disabled !== true) {
|
this.dbot.commands[commandName].disabled !== true) {
|
||||||
if(applyRegex(commandName, event)) {
|
if(this.api.applyRegex(commandName, event)) {
|
||||||
try {
|
try {
|
||||||
dbot.commands[commandName](event);
|
this.dbot.commands[commandName](event);
|
||||||
} catch(err) {
|
} catch(err) {
|
||||||
if(dbot.config.debugMode == true) {
|
if(this.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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
dbot.save();
|
this.dbot.save();
|
||||||
} else {
|
} else {
|
||||||
if(commandName !== '~') {
|
if(commandName !== '~') {
|
||||||
if(_.has(dbot.usage, commandName)) {
|
if(_.has(this.dbot.usage, commandName)) {
|
||||||
event.reply('Usage: ' + dbot.usage[commandName]);
|
event.reply('Usage: ' + this.dbot.usage[commandName]);
|
||||||
} else {
|
} else {
|
||||||
event.reply(dbot.t('syntax_error'));
|
event.reply(this.dbot.t('syntax_error'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
}.bind(this);
|
||||||
'on': 'PRIVMSG'
|
this.on = 'PRIVMSG';
|
||||||
};
|
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.fetch = function(dbot) {
|
exports.fetch = function(dbot) {
|
||||||
return command(dbot);
|
return new command(dbot);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
39
modules/command/commands.js
Normal file
39
modules/command/commands.js
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
var _ = require('underscore')._;
|
||||||
|
|
||||||
|
var commands = {
|
||||||
|
'~usage': function(event) {
|
||||||
|
var commandName = event.params[1];
|
||||||
|
if(_.has(this.dbot.usage, commandName)) {
|
||||||
|
event.reply(this.dbot.t('usage', {
|
||||||
|
'command': commandName,
|
||||||
|
'usage': this.dbot.usage[commandName]
|
||||||
|
}));
|
||||||
|
} else {
|
||||||
|
event.reply(this.dbot.t('no_usage_info', {
|
||||||
|
'command': commandName
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
'~help': function(event) {
|
||||||
|
var moduleName = event.params[1];
|
||||||
|
if(!_.has(this.dbot.modules, moduleName)) {
|
||||||
|
var moduleName = this.dbot.commands[moduleName].module;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(moduleName && _.has(this.dbot.config[moduleName], 'help')) {
|
||||||
|
var help = this.dbot.config[moduleName].help;
|
||||||
|
event.reply(this.dbot.t('help_link', {
|
||||||
|
'module': moduleName,
|
||||||
|
'link': help
|
||||||
|
}));
|
||||||
|
} else {
|
||||||
|
if(!moduleName) {
|
||||||
|
moduleName = event.params[1];
|
||||||
|
}
|
||||||
|
event.reply(this.dbot.t('no_help', { 'module': moduleName }))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
exports.fetch = commands;
|
23
run.js
23
run.js
@ -158,8 +158,7 @@ DBot.prototype.reloadModules = function() {
|
|||||||
// Load the module config data
|
// Load the module config data
|
||||||
var config = {};
|
var config = {};
|
||||||
try {
|
try {
|
||||||
config = JSON.parse(fs.readFileSync(moduleDir + 'config.json', 'utf-8'))
|
config = JSON.parse(fs.readFileSync(moduleDir + 'config.json', 'utf-8'));
|
||||||
|
|
||||||
} catch(err) {
|
} catch(err) {
|
||||||
// Invalid or no config data
|
// Invalid or no config data
|
||||||
}
|
}
|
||||||
@ -174,9 +173,27 @@ DBot.prototype.reloadModules = function() {
|
|||||||
// Load the module itself
|
// Load the module itself
|
||||||
var rawModule = require(moduleDir + name);
|
var rawModule = require(moduleDir + name);
|
||||||
var module = rawModule.fetch(this);
|
var module = rawModule.fetch(this);
|
||||||
|
module.name = name;
|
||||||
this.rawModules.push(rawModule);
|
this.rawModules.push(rawModule);
|
||||||
|
|
||||||
module.name = name;
|
// Load the module with any addition objects we can find...
|
||||||
|
_.each([ 'commands', 'pages', 'api' ], function(property) {
|
||||||
|
try {
|
||||||
|
var propertyKey = require.resolve(moduleDir + property);
|
||||||
|
if(propertyKey) delete require.cache[propertyKey];
|
||||||
|
var propertyObj = require(moduleDir + property).fetch;
|
||||||
|
} catch(err) {
|
||||||
|
console.log(err.stack);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
module[property] = {};
|
||||||
|
_.each(propertyObj, function(item, name) {
|
||||||
|
if(_.isFunction(item)) {
|
||||||
|
module[property][name] = item.bind(module);
|
||||||
|
}
|
||||||
|
}, this);
|
||||||
|
}, this);
|
||||||
|
|
||||||
if(module.listener) {
|
if(module.listener) {
|
||||||
if(!_.isArray(module.on)) {
|
if(!_.isArray(module.on)) {
|
||||||
|
Loading…
Reference in New Issue
Block a user