From f8d4896eb6e246a42f549172c99818bb763e670a Mon Sep 17 00:00:00 2001 From: reality Date: Sat, 12 Jan 2013 09:38:13 +0000 Subject: [PATCH] Moderator + admin access level for commands [#124] * May now add property 'access' to commands specifying 'moderator' or 'admin' level access * Add 'moderators' key to config * Convert commands in admin module to use command marshalling listener rather than its own --- config.json.sample | 1 + modules/admin/admin.js | 24 ++++++++++++------------ modules/command/command.js | 25 ++++++++++++++++++++++++- run.js | 2 +- 4 files changed, 38 insertions(+), 14 deletions(-) diff --git a/config.json.sample b/config.json.sample index 27342c0..532460a 100644 --- a/config.json.sample +++ b/config.json.sample @@ -12,6 +12,7 @@ } }, "admins": [ "batman" ], + "moderators": [ "whatever" ], "moduleNames": [ "ignore", "admin", "command", "dice", "js", "kick", "quotes", "spelling", "youare" ], "language": "english", "debugMode": true diff --git a/modules/admin/admin.js b/modules/admin/admin.js index 962eda0..0401585 100644 --- a/modules/admin/admin.js +++ b/modules/admin/admin.js @@ -148,21 +148,21 @@ var admin = function(dbot) { } }; + commands['greload'].access = 'admin'; + commands['reload'].access = 'admin'; + commands['unload'].access = 'admin'; + commands['load'].access = 'admin'; + commands['join'].access = 'moderator'; + commands['part'].access = 'moderator'; + commands['opme'].access = 'moderator'; + commands['say'].access = 'moderator'; + commands['ban'].access = 'moderator'; + commands['unban'].access = 'moderator'; + return { 'name': 'admin', 'ignorable': false, - - /** - * Run the appropriate admin command given the input (and user). - */ - 'listener': function(event) { - var commandName = event.params[0]; - if(commands.hasOwnProperty(commandName) && dbot.config.admins.include(event.user)) { - commands[commandName](event); - dbot.save(); - } - }, - 'on': 'PRIVMSG' + 'commands': commands }; }; diff --git a/modules/command/command.js b/modules/command/command.js index 0ab8bf2..51d991b 100644 --- a/modules/command/command.js +++ b/modules/command/command.js @@ -18,6 +18,29 @@ var command = function(dbot) { return banned; }; + /** + * Does the user have the correct access level to use the command? + */ + var hasAccess = function(user, command) { + var access = true; + var accessNeeded = dbot.commands[command].access; + + if(accessNeeded == 'admin') { + if(!dbot.config.admins.include(user)) { + access = false; + } + } else if(accessNeeded == 'moderator') { + if(!dbot.config.moderators.include(user) && + !dbot.config.admins.include(user)) { + access = false; + } + } + console.log(accessNeeded); + console.log(user); + + return access; + }; + /** * Is user ignoring command? */ @@ -101,7 +124,7 @@ var command = function(dbot) { if(isBanned(event.user, commandName)) { event.reply(dbot.t('command_ban', {'user': event.user})); } else { - if(!isIgnoring(event.user, commandName)) { + if(!isIgnoring(event.user, commandName) && hasAccess(event.user, commandName)) { if(applyRegex(commandName, event)) { try { dbot.commands[commandName](event); diff --git a/run.js b/run.js index d8220fe..e83dbac 100644 --- a/run.js +++ b/run.js @@ -5,7 +5,7 @@ require('./snippets'); var DBot = function(timers) { // Load external files - var requiredConfigKeys = [ 'name', 'servers', 'admins', 'moduleNames', 'language', 'debugMode' ]; + var requiredConfigKeys = [ 'name', 'servers', 'admins', 'moderators', 'moduleNames', 'language', 'debugMode' ]; try { this.config = JSON.parse(fs.readFileSync('config.json', 'utf-8')); } catch(err) {