3
0
mirror of https://github.com/reality/dbot.git synced 2024-12-26 12:42:51 +01:00
dbot/modules/command/api.js

64 lines
2.2 KiB
JavaScript
Raw Normal View History

var _ = require('underscore')._;
2013-01-14 17:24:38 +01:00
var api = function(dbot) {
return {
/**
* Does the user have the correct access level to use the command?
*/
'hasAccess': function(user, command, callback) {
2013-01-14 17:24:38 +01:00
var accessNeeded = dbot.commands[command].access;
2013-07-02 12:39:55 +02:00
if(accessNeeded == 'admin' || accessNeeded == 'moderator' || accessNeeded == 'power_user') {
2013-05-06 23:49:45 +02:00
var allowedNicks = dbot.config.admins;
if(accessNeeded == 'moderator') allowedNicks = _.union(allowedNicks, dbot.config.moderators);
2013-07-01 19:59:54 +02:00
if(accessNeeded == 'power_user') allowedNicks = _.union(allowedNicks, dbot.config.power_users);
2013-05-06 23:49:45 +02:00
if(!_.include(allowedNicks, user.primaryNick)) {
2013-04-23 21:07:23 +02:00
callback(false);
} else {
if(_.has(dbot.modules, 'nickserv') && this.config.useNickserv == true) {
dbot.api.nickserv.auth(user.server, user.currentNick, function(result) {
2013-04-23 21:07:23 +02:00
callback(result);
});
} else {
callback(true);
}
2013-01-14 17:24:38 +01:00
}
2013-04-23 21:07:23 +02:00
} else {
callback(true);
}
2013-01-14 17:24:38 +01:00
},
2013-01-14 17:24:38 +01:00
/**
* Apply Regex to event message, store result. Return false if it doesn't
* apply.
*/
'applyRegex': function(commandName, event) {
var applies = false;
if(_.has(dbot.commands[commandName], 'regex')) {
var cRegex = dbot.commands[commandName].regex;
2013-04-14 15:58:45 +02:00
if(_.isArray(cRegex) && cRegex.length == 2) {
var q = event.message.valMatch(cRegex[0], cRegex[1]);
if(q) {
applies = true;
event.input = q;
}
} else {
2013-04-14 15:58:45 +02:00
var q = event.message.match(cRegex);
if(q) {
applies = true;
event.input = q;
}
2013-01-14 17:24:38 +01:00
}
} else {
applies = true;
}
2013-01-14 17:24:38 +01:00
return applies;
}
2013-01-14 17:24:38 +01:00
};
};
2013-01-14 17:24:38 +01:00
exports.fetch = function(dbot) {
return api(dbot);
};