Moved all data loaded from config to be used only from dbot.config. Changed admin key in to admins to avoid clash with admin module config. Admins should probably be a key under dbot.admin module anyway.

This commit is contained in:
Luke Slater 2012-12-17 17:18:31 +00:00
parent 96312303f0
commit d0c47d18eb
8 changed files with 43 additions and 45 deletions

View File

@ -11,7 +11,7 @@
]
}
},
"admin": [ "batman" ],
"admins": [ "batman" ],
"moduleNames": [ "ignore", "admin", "command", "dice", "js", "kick", "puns", "quotes", "spelling", "youare" ],
"language": "english"
}

View File

@ -70,21 +70,22 @@ var admin = function(dbot) {
// Load new module
'load': function(event) {
var moduleName = event.params[1];
dbot.moduleNames.push(moduleName);
dbot.config.moduleNames.push(moduleName);
dbot.reloadModules();
event.reply(dbot.t('load_module', {'moduleName': moduleName}));
},
// Unload a loaded module
'unload': function(event) {
var moduleNames = dbot.config.moduleNames;
var moduleName = event.params[1];
if(dbot.moduleNames.include(moduleName)) {
if(moduleNames.include(moduleName)) {
var moduleDir = '../' + moduleName + '/';
var cacheKey = require.resolve(moduleDir + moduleName);
delete require.cache[cacheKey];
var moduleIndex = dbot.moduleNames.indexOf(moduleName);
dbot.moduleNames.splice(moduleIndex, 1);
var moduleIndex = moduleNames.indexOf(moduleName);
moduleNames.splice(moduleIndex, 1);
dbot.reloadModules();
event.reply(dbot.t('unload_module', {'moduleName': moduleName}));
@ -134,7 +135,7 @@ var admin = function(dbot) {
*/
'listener': function(event) {
var commandName = event.params[0];
if(commands.hasOwnProperty(commandName) && dbot.admin.include(event.user)) {
if(commands.hasOwnProperty(commandName) && dbot.config.admins.include(event.user)) {
commands[commandName](event);
dbot.save();
}

View File

@ -22,7 +22,7 @@ var js = function(dbot) {
// Run JS code un-sandboxed, with access to DBot memory (admin-only).
'~ajs': function(event) {
if(dbot.admin.include(event.user) ) {
if(dbot.config.admins.include(event.user) ) {
var ret = eval(event.input[1]);
if(ret !== undefined) {
event.reply(ret);

View File

@ -46,10 +46,10 @@ var kick = function(dbot) {
'commands': commands,
'listener': function(event) {
if(event.kickee == dbot.name) {
if(event.kickee == dbot.config.name) {
dbot.instance.join(event, event.channel);
event.reply(dbot.t('kicked_dbot', {'botname': dbot.name}));
dbot.db.kicks[dbot.name] += 1;
event.reply(dbot.t('kicked_dbot', {'botname': dbot.config.name}));
dbot.db.kicks[dbot.config.name] += 1;
} else {
if(!dbot.db.kicks.hasOwnProperty(event.kickee)) {
dbot.db.kicks[event.kickee] = 1;

View File

@ -8,7 +8,7 @@ var puns = function(dbot) {
'listener': function(event) {
event.user = dbot.cleanNick(event.user);
if(dbot.moduleNames.include('quotes') &&
if(dbot.config.moduleNames.include('quotes') &&
dbot.db.quoteArrs.hasOwnProperty(event.user)) {
event.message = '~q ' + event.user;
event.action = 'PRIVMSG';

View File

@ -94,10 +94,10 @@ var quotes = function(dbot) {
},
'~rmlast': function(event) {
if(rmAllowed == true || dbot.admin.include(event.user)) {
if(rmAllowed == true || dbot.config.admins.include(event.user)) {
var key = event.input[1].trim().toLowerCase();
if(quotes.hasOwnProperty(key)) {
if(!dbot.db.locks.include(key) || dbot.admin.include(event.user)) {
if(!dbot.db.locks.include(key) || dbot.config.admins.include(event.user)) {
var quote = quotes[key].pop();
if(quotes[key].length === 0) {
delete quotes[key];
@ -116,7 +116,7 @@ var quotes = function(dbot) {
},
'~rm': function(event) {
if(rmAllowed == true || dbot.admin.include(event.user)) {
if(rmAllowed == true || dbot.config.admins.include(event.user)) {
var key = event.input[1].trim().toLowerCase();
var quote = event.input[2];

View File

@ -9,21 +9,21 @@ var webInterface = function(dbot) {
app.set('view engine', 'jade');
app.get('/', function(req, res) {
res.render('index', { 'name': dbot.name });
res.render('index', { 'name': dbot.config.name });
});
app.get('/connections', function(req, res) {
var connections = Object.keys(dbot.instance.connections);
res.render('connections', { 'name': dbot.name, 'connections': connections });
res.render('connections', { 'name': dbot.config.name, 'connections': connections });
});
app.get('/channels/:connection', function(req, res) {
var connection = req.params.connection;
if(dbot.instance.connections.hasOwnProperty(connection)) {
var channels = Object.keys(dbot.instance.connections[connection].channels);
res.render('channels', { 'name': dbot.name, 'connection': connection, 'channels': channels});
res.render('channels', { 'name': dbot.config.name, 'connection': connection, 'channels': channels});
} else {
res.render('error', { 'name': dbot.name, 'message': 'No such connection.' });
res.render('error', { 'name': dbot.config.name, 'message': 'No such connection.' });
}
});
@ -35,10 +35,10 @@ var webInterface = function(dbot) {
if(connections.hasOwnProperty(connection) &&
connections[connection].channels.hasOwnProperty(channel)) {
var nicks = Object.keys(connections[connection].channels[channel].nicks);
res.render('users', { 'name': dbot.name, 'connection': connection,
res.render('users', { 'name': dbot.config.name, 'connection': connection,
'channel': channel, 'nicks': nicks });
} else {
res.render('error', { 'name': dbot.name, 'message': 'No such connection or channel.' });
res.render('error', { 'name': dbot.config.name, 'message': 'No such connection or channel.' });
}
});
@ -64,35 +64,35 @@ var webInterface = function(dbot) {
var kicked = dbot.db.kickers[req.params.user];
}
res.render('user', { 'name': dbot.name, 'user': req.params.user,
res.render('user', { 'name': dbot.config.name, 'user': req.params.user,
'channel': channel, 'connection': connection, 'cleanUser': user,
'quotecount': quoteCount, 'kicks': kicks, 'kicked': kicked });
});
// Lists the quote categories
app.get('/quotes', function(req, res) {
res.render('quotelist', { 'name': dbot.name, 'quotelist': Object.keys(dbot.db.quoteArrs) });
res.render('quotelist', { 'name': dbot.config.name, 'quotelist': Object.keys(dbot.db.quoteArrs) });
});
// Lists quotes in a category
app.get('/quotes/:key', function(req, res) {
var key = req.params.key.toLowerCase();
if(dbot.db.quoteArrs.hasOwnProperty(key)) {
res.render('quotes', { 'name': dbot.name, 'quotes': dbot.db.quoteArrs[key], locals: { 'url_regex': RegExp.prototype.url_regex() } });
res.render('quotes', { 'name': dbot.config.name, 'quotes': dbot.db.quoteArrs[key], locals: { 'url_regex': RegExp.prototype.url_regex() } });
} else {
res.render('error', { 'name': dbot.name, 'message': 'No quotes under that key.' });
res.render('error', { 'name': dbot.config.name, 'message': 'No quotes under that key.' });
}
});
// Load random quote category page
app.get('/rq', function(req, res) {
var rCategory = Object.keys(dbot.db.quoteArrs).random();
res.render('quotes', { 'name': dbot.name, 'quotes': dbot.db.quoteArrs[rCategory], locals: { 'url_regex': RegExp.prototype.url_regex() } });
res.render('quotes', { 'name': dbot.config.name, 'quotes': dbot.db.quoteArrs[rCategory], locals: { 'url_regex': RegExp.prototype.url_regex() } });
});
// Lists all of the polls
app.get('/polls', function(req, res) {
res.render('polllist', { 'name': dbot.name, 'polllist': Object.keys(dbot.db.polls) });
res.render('polllist', { 'name': dbot.config.name, 'polllist': Object.keys(dbot.db.polls) });
});
// Shows the results of a poll
@ -107,9 +107,9 @@ var webInterface = function(dbot) {
totalVotes += N;
}
}
res.render('polls', { 'name': dbot.name, 'description': dbot.db.polls[key].description, 'votees': Object.keys(dbot.db.polls[key].votees), 'options': dbot.db.polls[key].votes, locals: { 'totalVotes': totalVotes, 'url_regex': RegExp.prototype.url_regex() } });
res.render('polls', { 'name': dbot.config.name, 'description': dbot.db.polls[key].description, 'votees': Object.keys(dbot.db.polls[key].votees), 'options': dbot.db.polls[key].votes, locals: { 'totalVotes': totalVotes, 'url_regex': RegExp.prototype.url_regex() } });
} else {
res.render('error', { 'name': dbot.name, 'message': 'No polls under that key.' });
res.render('error', { 'name': dbot.config.name, 'message': 'No polls under that key.' });
}
});

31
run.js
View File

@ -5,7 +5,7 @@ require('./snippets');
var DBot = function(timers) {
// Load external files
var requiredConfigKeys = [ 'name', 'servers', 'admin', 'moduleNames', 'language' ];
var requiredConfigKeys = [ 'name', 'servers', 'admins', 'moduleNames', 'language' ];
try {
this.config = JSON.parse(fs.readFileSync('config.json', 'utf-8'));
} catch(err) {
@ -55,19 +55,14 @@ var DBot = function(timers) {
this.timers = timers.create();
// Populate bot properties with config data
for(var configKey in this.config) {
if(this.config.hasOwnProperty(configKey) && this.hasOwnProperty(configKey) === false) {
this[configKey] = this.config[configKey];
}
}
// Create JSBot and connect to each server
this.instance = jsbot.createJSBot(this.name);
for(var name in this.servers) {
if(this.servers.hasOwnProperty(name)) {
var server = this.servers[name];
this.instance.addConnection(name, server.server, server.port, this.admin, function(event) {
var server = this.servers[event.server];
this.instance = jsbot.createJSBot(this.config.name);
for(var name in this.config.servers) {
if(this.config.servers.hasOwnProperty(name)) {
var server = this.config.servers[name];
this.instance.addConnection(name, server.server, server.port,
this.config.admin, function(event) {
var server = this.config.servers[event.server];
for(var i=0;i<server.channels.length;i++) {
this.instance.join(event, server.channels[i]);
}
@ -89,7 +84,7 @@ DBot.prototype.say = function(server, channel, message) {
DBot.prototype.t = function(string, formatData) {
var formattedString;
if(this.strings.hasOwnProperty(string)) {
var lang = this.language;
var lang = this.config.language;
if(!this.strings[string].hasOwnProperty(lang)) {
lang = "english";
}
@ -135,10 +130,12 @@ DBot.prototype.reloadModules = function() {
this.strings = {};
}
var moduleNames = this.config.moduleNames;
// Enforce having command. it can still be reloaded, but dbot _will not_
// function without it, so not having it should be impossible
if(!this.moduleNames.include("command")) {
this.moduleNames.push("command");
if(!moduleNames.include("command")) {
moduleNames.push("command");
}
// Reload Javascript snippets
@ -148,7 +145,7 @@ DBot.prototype.reloadModules = function() {
this.instance.removeListeners();
this.moduleNames.each(function(name) {
moduleNames.each(function(name) {
var moduleDir = './modules/' + name + '/';
var cacheKey = require.resolve(moduleDir + name);
delete require.cache[cacheKey];