From 466cb12c9448280ad9616ae29a3c3f23781a0bf6 Mon Sep 17 00:00:00 2001 From: reality Date: Sat, 20 Apr 2013 13:50:47 +0000 Subject: [PATCH] getCurrentConfig, pushconfig, showconfig and setconfig databankerised [#331] --- modules/admin/admin.js | 29 +++++++ modules/admin/commands.js | 159 ++++++++++++++------------------------ modules/admin/config.json | 1 + 3 files changed, 89 insertions(+), 100 deletions(-) diff --git a/modules/admin/admin.js b/modules/admin/admin.js index f1edaf3..23feda0 100644 --- a/modules/admin/admin.js +++ b/modules/admin/admin.js @@ -7,6 +7,35 @@ var fs = require('fs'), _ = require('underscore')._; var admin = function(dbot) { + this.internalAPI = { + 'getCurrentConfig': function(configKey, callback) { + if(configKey) { + this.db.read('config', configKey, function(err, cRecord) { + if(cRecord) { + callback(cRecord.value) + } else { + var configPath = dbot.config; + configKey = configKey.split('.'); + + for(var i=0;i " + newOption); - configPath['user'][configKey] = newOption; - dbot.reloadModules(); + event.reply(configPath + ": " + config + " -> " + newOption); + config = newOption; + this.db.save('config', configPath, { 'value': config }, function(err) { + dbot.reloadModules(); + }); + } else { + event.reply('Config path doesn\'t exist, bro.'); + } + }.bind(this)); } else { event.reply("This config option cannot be altered while the bot is running."); } }, 'pushconfig': function(event) { - var configPathString = event.params[1], - configKey = _.last(configPathString.split('.')), - newOption = event.params[2]; + var configPath = event.input[1], + newOption = event.input[2]; - if(!_.include(noChangeConfig, configKey)) { - var configPath = getCurrentConfig(configPathString); - if(configPath == false || _.isUndefined(configPath.value)) { - event.reply("Config key doesn't exist bro"); - return; - } - var currentArray = configPath.value; - - if(!_.isArray(currentArray)) { - event.reply("Config option is not an array. Try 'setconfig'."); - return - } - - event.reply(configPathString + ": " + currentArray + " << " + newOption); - currentArray.push(newOption); - dbot.reloadModules(); + if(!_.include(noChangeConfig, configPath)) { + this.internalAPI.getCurrentConfig(configPath, function(config) { + if(config) { + if(_.isArray(config)) { + event.reply(configPath + ": " + config + " << " + newOption); + config.push(newOption); + this.db.save('config', configPath, { 'value': config }, function(err) { + dbot.reloadModules(); + }); + } else { + event.reply("Config option is not an array. Try 'setconfig'."); + } + } else { + event.reply('Config path doesn\'t exist, bro.'); + } + }.bind(this)); + } else { + event.reply("This config option cannot be altered while the bot is running."); } }, 'showconfig': function(event) { - var configPathString = event.params[1]; - var configPath = getCurrentConfig(configPathString); - - if(configPathString) { - var configKey = _.last(configPathString.split('.')); - if(configKey == false) { - event.reply("Config path doesn't exist"); - return; - } - - if(_.isArray(configPath.value)) { - event.reply(configKey + ': ' + configPath.value); - } else if(_.isObject(configPath.value)) { - event.reply('Config keys in ' + configPathString + ': ' + Object.keys(configPath.value)); - } else { - event.reply(configKey + ': ' + configPath.value); - } + var configPath = event.params[1]; + if(configPath) { + this.internalAPI.getCurrentConfig(configPath, function(config) { + if(config) { + if(_.isArray(config)) { + event.reply('Config keys in ' + configPath + ': ' + config); + } else if(_.isObject(config)) { + event.reply('Config keys in ' + configPath + ': ' + _.keys(config)); + } else { + event.reply(configPath + ': ' + config); + } + } else { + event.reply('Config path doesn\'t exist, bro.'); + } + }); } else { - event.reply('Config keys in root: ' + Object.keys(configPath['default'])); + event.reply('Config keys in root: ' + _.keys(dbot.config)); } - } + } }; _.each(commands, function(command) { @@ -277,6 +233,9 @@ var commands = function(dbot) { commands['opme'].access = 'moderator'; commands['say'].access = 'moderator'; + commands['pushconfig'].regex = [/pushconfig ([^ ]+) ([^ ]+)/, 3]; + commands['setconfig'].regex = [/setconfig ([^ ]+) ([^ ]+)/, 3]; + return commands; }; diff --git a/modules/admin/config.json b/modules/admin/config.json index f87d4b9..cd5a001 100644 --- a/modules/admin/config.json +++ b/modules/admin/config.json @@ -1,5 +1,6 @@ { "ignorable": false, + "dbType": "redis", "dependencies": [ "command" ], "help": "http://github.com/reality/depressionbot/blob/master/modules/admin/README.md" }