3
0
mirror of https://github.com/reality/dbot.git synced 2024-11-30 16:09:27 +01:00

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
This commit is contained in:
reality 2013-01-12 09:38:13 +00:00
parent 04901cc0ba
commit f8d4896eb6
4 changed files with 38 additions and 14 deletions

View File

@ -12,6 +12,7 @@
}
},
"admins": [ "batman" ],
"moderators": [ "whatever" ],
"moduleNames": [ "ignore", "admin", "command", "dice", "js", "kick", "quotes", "spelling", "youare" ],
"language": "english",
"debugMode": true

View File

@ -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
};
};

View File

@ -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);

2
run.js
View File

@ -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) {