3
0
mirror of https://github.com/reality/dbot.git synced 2024-12-24 19:52:36 +01:00

thats a little better on the module loader i suppose [#331]

This commit is contained in:
reality 2013-04-10 01:07:29 +00:00
parent 384fe3024a
commit 6cea1256ce

175
run.js
View File

@ -207,6 +207,90 @@ DBot.prototype.reloadModules = function() {
} }
this.config[name] = config; this.config[name] = config;
var loadModule = function(db) {
var rawModule = require(moduleDir + name);
var module = rawModule.fetch(this);
this.rawModules.push(rawModule);
module.name = name;
module.db = db;
module.config = this.config[name];
// Load the module data
_.each([ 'commands', 'pages', 'api' ], function(property) {
var propertyObj = {};
if(fs.existsSync(moduleDir + property + '.js')) {
try {
var propertyKey = require.resolve(moduleDir + property);
if(propertyKey) delete require.cache[propertyKey];
propertyObj = require(moduleDir + property).fetch(this);
} catch(err) {
this.status[name] = 'Error loading ' + propertyKey +
': ' + err + ' - ' + err.stack.split('\n')[1].trim();
console.log('Module error (' + module.name + ') in ' +
property + ': ' + err);
}
}
if(!_.has(module, property)) module[property] = {};
_.extend(module[property], propertyObj);
_.each(module[property], function(item, itemName) {
item.module = name;
if(_.has(module.config, property) && _.has(module.config[property], itemName)) {
_.extend(item, module.config[property][itemName]);
}
module[property][itemName] = _.bind(item, module);
_.extend(module[property][itemName], item);
}, this);
if(property == 'api') {
this[property][name] = module[property];
} else {
_.extend(this[property], module[property]);
}
}, this);
// Load the module listener
if(module.listener) {
if(!_.isArray(module.on)) {
module.on = [ module.on ];
}
_.each(module.on, function(on) {
this.instance.addListener(on, module.name, module.listener);
}, this);
}
// Load string data for the module
_.each([ 'usage', 'strings' ], function(property) {
var propertyData = {};
try {
propertyData = JSON.parse(fs.readFileSync(moduleDir + property + '.json', 'utf-8'));
} catch(err) {};
_.extend(this[property], propertyData);
}, this);
// Provide toString for module name
module.toString = function() {
return this.name;
}
this.modules[module.name] = module;
if(_.has(this.modules, 'web')) this.modules.web.reloadPages();
_.each(this.modules, function(module, name) {
if(module.onLoad) {
try {
module.onLoad();
} catch(err) {
this.status[name] = 'Error in onLoad: ' + err + ' ' + err.stack.split('\n')[1].trim();
console.log('MODULE ONLOAD ERROR (' + name + '): ' + err );
}
}
}, this);
}.bind(this);
// Groovy funky database shit // Groovy funky database shit
if(!_.has(config, 'dbType') || config.dbType == 'olde') { if(!_.has(config, 'dbType') || config.dbType == 'olde') {
// Generate missing DB keys // Generate missing DB keys
@ -215,12 +299,12 @@ DBot.prototype.reloadModules = function() {
this.db[dbKey] = {}; this.db[dbKey] = {};
} }
}, this); }, this);
this.loadModule(name, this.db); loadModule(this.db);
} else { } else {
// Just use the name of the module for now, add dbKey iteration later // Just use the name of the module for now, add dbKey iteration later
this.ddb.createDB(name, config.dbType, {}, function(db) { this.ddb.createDB(name, config.dbType, {}, function(db) {
this.loadModule(name, db); loadModule(db);
}.bind(this)); });
} }
}.bind(this)); }.bind(this));
@ -229,93 +313,8 @@ DBot.prototype.reloadModules = function() {
// Load the module itself // Load the module itself
DBot.prototype.loadModule = function(name, db) { DBot.prototype.loadModule = function(name, db) {
var moduleDir = './modules/' + name + '/';
var rawModule = require(moduleDir + name);
var module = rawModule.fetch(this);
this.rawModules.push(rawModule);
module.name = name;
module.db = db;
module.config = this.config[name];
// Load the module data
_.each([ 'commands', 'pages', 'api' ], function(property) {
var propertyObj = {};
if(fs.existsSync(moduleDir + property + '.js')) {
try {
var propertyKey = require.resolve(moduleDir + property);
if(propertyKey) delete require.cache[propertyKey];
propertyObj = require(moduleDir + property).fetch(this);
} catch(err) {
this.status[name] = 'Error loading ' + propertyKey +
': ' + err + ' - ' + err.stack.split('\n')[1].trim();
console.log('Module error (' + module.name + ') in ' +
property + ': ' + err);
}
}
if(!_.has(module, property)) module[property] = {};
_.extend(module[property], propertyObj);
_.each(module[property], function(item, itemName) {
item.module = name;
if(_.has(module.config, property) && _.has(module.config[property], itemName)) {
_.extend(item, module.config[property][itemName]);
}
module[property][itemName] = _.bind(item, module);
_.extend(module[property][itemName], item);
}, this);
if(property == 'api') {
this[property][name] = module[property];
} else {
_.extend(this[property], module[property]);
}
}, this);
// Load the module listener
if(module.listener) {
if(!_.isArray(module.on)) {
module.on = [ module.on ];
}
_.each(module.on, function(on) {
this.instance.addListener(on, module.name, module.listener);
}, this);
} }
// Load string data for the module
_.each([ 'usage', 'strings' ], function(property) {
var propertyData = {};
try {
propertyData = JSON.parse(fs.readFileSync(moduleDir + property + '.json', 'utf-8'));
} catch(err) {};
_.extend(this[property], propertyData);
}, this);
// Provide toString for module name
module.toString = function() {
return this.name;
}
console.log(module);
console.log(this);
this.modules[module.name] = module;
if(_.has(this.modules, 'web')) this.modules.web.reloadPages();
_.each(this.modules, function(module, name) {
if(module.onLoad) {
try {
module.onLoad();
} catch(err) {
this.status[name] = 'Error in onLoad: ' + err + ' ' + err.stack.split('\n')[1].trim();
console.log('MODULE ONLOAD ERROR (' + name + '): ' + err );
}
}
}, this);
}
DBot.prototype.cleanNick = function(key) { DBot.prototype.cleanNick = function(key) {
key = key.toLowerCase(); key = key.toLowerCase();
while(key.endsWith("_")) { while(key.endsWith("_")) {