3
0
mirror of https://github.com/reality/dbot.git synced 2024-11-30 16:09:27 +01:00

lots of underscorisation in module loading

This commit is contained in:
reality 2013-01-12 14:58:54 +00:00
parent 319dd44748
commit d9f8ff0c1f

92
run.js
View File

@ -164,17 +164,19 @@ DBot.prototype.reloadModules = function() {
// Load the module config data // Load the module config data
var config = {}; var config = {};
try { try {
var config = JSON.parse(fs.readFileSync(moduleDir + 'config.json', 'utf-8')) config = JSON.parse(fs.readFileSync(moduleDir + 'config.json', 'utf-8'))
this.config[name] = config;
for(var i=0;i<config.dbKeys.length;i++) {
if(!this.db.hasOwnProperty(config.dbKeys[i])) {
this.db[config.dbKeys[i]] = {};
}
}
} catch(err) { } catch(err) {
// Invalid or no config data // Invalid or no config data
} }
this.config[name] = config;
_.each(config.dbKeys, function(dbKey) {
if(!_.has(this.db, dbKey( {
this.db[dbKey] = {};
}
}, this);
// 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);
@ -183,14 +185,13 @@ DBot.prototype.reloadModules = function() {
module.name = name; module.name = name;
if(module.listener) { if(module.listener) {
var listenOn = module.on; if(!_.isArray(module.on)) {
if(!(listenOn instanceof Array)) { module.on = [ module.on ];
listenOn = [listenOn];
} }
listenOn.each(function(on) { _.each(module.on, function(on) {
this.instance.addListener(on, module.name, module.listener); this.instance.addListener(on, module.name, module.listener);
}.bind(this)); }, this);
} }
if(module.onLoad) { if(module.onLoad) {
@ -199,36 +200,21 @@ DBot.prototype.reloadModules = function() {
// Load module commands // Load module commands
if(module.commands) { if(module.commands) {
var newCommands = module.commands; _.extend(this.commands, module.commands);
for(key in newCommands) { _.each(module.commands, function(command, commandName) {
if(newCommands.hasOwnProperty(key) && Object.prototype.isFunction(newCommands[key])) { command.module = name;
this.commands[key] = newCommands[key]; if(_.has(config, 'commands') && _.has(config.commands, commandName)) {
this.commandMap[key] = name; _.extend(command, config.commands[commandName]);
}
}
}
// Load module commands with properties specified in config
if(module.commands && config.commands) {
for(key in config.commands) {
if(newCommands.hasOwnProperty(key)) {
for(var prop in config.commands[key]) {
newCommands[key][prop] = config.commands[key][prop];
}
}
} }
}, this);
} }
// Load module web bits // Load module web bits
if(module.pages) { if(module.pages) {
var newpages = module.pages; _.extend(this.pages, module.pages);
for(var key in newpages) _.each(module.pages, function(page) {
{ page.module = name;
if(newpages.hasOwnProperty(key) && Object.prototype.isFunction(newpages[key])) { }, this);
this.pages[key] = newpages[key];
this.pages[key].module = module;
}
}
} }
// Load module API // Load module API
@ -237,51 +223,39 @@ DBot.prototype.reloadModules = function() {
} }
// Load the module usage data // Load the module usage data
var usage = {};
try { try {
var usage = JSON.parse(fs.readFileSync(moduleDir + 'usage.json', 'utf-8')); usage = JSON.parse(fs.readFileSync(moduleDir + 'usage.json', 'utf-8'));
for(key in usage) {
if(usage.hasOwnProperty(key)) {
if(this.usage.hasOwnProperty(key)) {
console.log('Usage key clash for ' + key + ' in ' + name);
} else {
this.usage[key] = usage[key];
}
}
}
} catch(err) { } catch(err) {
// Invalid or no usage info // Invalid or no usage info
} }
_.extend(this.usage, usage);
// Load the module string data // Load the module string data
var strings = {};
try { try {
var strings = JSON.parse(fs.readFileSync(moduleDir + 'strings.json', 'utf-8')); strings = JSON.parse(fs.readFileSync(moduleDir + 'strings.json', 'utf-8'));
for(key in strings) {
if(strings.hasOwnProperty(key)) {
if(this.strings.hasOwnProperty(key)) {
console.log('Strings key clash for ' + key + ' in ' + name);
} else {
this.strings[key] = strings[key];
}
}
}
} catch(err) { } catch(err) {
// Invalid or no string info // Invalid or no string info
} }
_.extend(this.strings, strings);
// Provide toString for module name
module.toString = function() { module.toString = function() {
return this.name; return this.name;
} }
this.modules[module.name] = module; this.modules[module.name] = module;
} catch(err) { } catch(err) {
console.log(this.t('module_load_error', {'moduleName': name})); console.log(this.t('module_load_error', {'moduleName': name}));
if(this.config.debugMode) { if(this.config.debugMode) {
console.log('MODULE ERROR (' + name + '): ' + err.stack ); console.log('MODULE ERROR (' + name + '): ' + err.stack );
} } else {
else {
console.log('MODULE ERROR (' + name + '): ' + err ); console.log('MODULE ERROR (' + name + '): ' + err );
} }
} }
}.bind(this)); }.bind(this));
this.reloadPages(); this.reloadPages();
this.save(); this.save();
}; };