From d0c47d18eb3137625863c1fd239b88352f2d1142 Mon Sep 17 00:00:00 2001 From: Luke Slater Date: Mon, 17 Dec 2012 17:18:31 +0000 Subject: [PATCH] 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. --- config.json.sample | 2 +- modules/admin/admin.js | 11 ++++++----- modules/js/js.js | 2 +- modules/kick/kick.js | 6 +++--- modules/puns/puns.js | 2 +- modules/quotes/quotes.js | 6 +++--- modules/web/web.js | 28 ++++++++++++++-------------- run.js | 31 ++++++++++++++----------------- 8 files changed, 43 insertions(+), 45 deletions(-) diff --git a/config.json.sample b/config.json.sample index bda53b7..31c11f6 100644 --- a/config.json.sample +++ b/config.json.sample @@ -11,7 +11,7 @@ ] } }, - "admin": [ "batman" ], + "admins": [ "batman" ], "moduleNames": [ "ignore", "admin", "command", "dice", "js", "kick", "puns", "quotes", "spelling", "youare" ], "language": "english" } diff --git a/modules/admin/admin.js b/modules/admin/admin.js index d6fae5e..d51474b 100644 --- a/modules/admin/admin.js +++ b/modules/admin/admin.js @@ -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(); } diff --git a/modules/js/js.js b/modules/js/js.js index 83e1bf0..76d5f91 100644 --- a/modules/js/js.js +++ b/modules/js/js.js @@ -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); diff --git a/modules/kick/kick.js b/modules/kick/kick.js index 540f9b7..150fb55 100644 --- a/modules/kick/kick.js +++ b/modules/kick/kick.js @@ -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; diff --git a/modules/puns/puns.js b/modules/puns/puns.js index 7144932..187a652 100644 --- a/modules/puns/puns.js +++ b/modules/puns/puns.js @@ -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'; diff --git a/modules/quotes/quotes.js b/modules/quotes/quotes.js index 49a7b71..abcc073 100644 --- a/modules/quotes/quotes.js +++ b/modules/quotes/quotes.js @@ -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]; diff --git a/modules/web/web.js b/modules/web/web.js index af18b31..9f01c8d 100644 --- a/modules/web/web.js +++ b/modules/web/web.js @@ -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.' }); } }); diff --git a/run.js b/run.js index 5a0abce..fec9441 100644 --- a/run.js +++ b/run.js @@ -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