API functionality as per [#98]

* Also resolveUser api function in user module [#98]
This commit is contained in:
reality 2013-01-03 19:56:30 +00:00
parent 99909e2dc5
commit 078379b5f1
2 changed files with 29 additions and 10 deletions

View File

@ -4,15 +4,15 @@
*/ */
var users = function(dbot) { var users = function(dbot) {
var knownUsers = dbot.db.knownUsers; var knownUsers = dbot.db.knownUsers;
var getServerUsers = function(event) { var getServerUsers = function(server) {
if(!knownUsers.hasOwnProperty(event.server)) { if(!knownUsers.hasOwnProperty(server)) {
knownUsers[event.server] = { 'users': [], 'aliases': {} }; knownUsers[server] = { 'users': [], 'aliases': {} };
} }
return knownUsers[event.server]; return knownUsers[server];
}; };
var updateAliases = function(event, oldUser, newUser) { var updateAliases = function(event, oldUser, newUser) {
var knownUsers = getServerUsers(event); var knownUsers = getServerUsers(event.server);
for(var alias in knownUsers.aliases) { for(var alias in knownUsers.aliases) {
if(knownUsers.aliases.hasOwnProperty(alias)) { if(knownUsers.aliases.hasOwnProperty(alias)) {
if(knownUsers.aliases[alias] === oldUser) { if(knownUsers.aliases[alias] === oldUser) {
@ -23,7 +23,7 @@ var users = function(dbot) {
} }
dbot.instance.addListener('366', 'users', function(event) { dbot.instance.addListener('366', 'users', function(event) {
var knownUsers = getServerUsers(event); var knownUsers = getServerUsers(event.server);
for(var nick in event.channel.nicks) { for(var nick in event.channel.nicks) {
if(!knownUsers.users.include(nick) && !knownUsers.aliases.hasOwnProperty(nick) && if(!knownUsers.users.include(nick) && !knownUsers.aliases.hasOwnProperty(nick) &&
event.channel.nicks.hasOwnProperty(nick)) { event.channel.nicks.hasOwnProperty(nick)) {
@ -32,9 +32,21 @@ var users = function(dbot) {
} }
}); });
var api = {
'resolveUser': function(server, nick) {
var knownUsers = getServerUsers(server);
var user = nick;
if(!knownUsers.users.include(nick) && knownUsers.aliases.hasOwnProperty(nick)) {
user = knownUsers.aliases[nick];
}
return user;
}
};
var commands = { var commands = {
'~alias': function(event) { '~alias': function(event) {
var knownUsers = getServerUsers(event); var knownUsers = getServerUsers(event.server);
var alias = event.params[1].trim(); var alias = event.params[1].trim();
if(knownUsers.users.include(alias)) { if(knownUsers.users.include(alias)) {
var aliasCount = 0; var aliasCount = 0;
@ -52,7 +64,7 @@ var users = function(dbot) {
'~setaliasparent': function(event) { '~setaliasparent': function(event) {
if(dbot.config.admins.include(event.user)) { if(dbot.config.admins.include(event.user)) {
var knownUsers = getServerUsers(event); var knownUsers = getServerUsers(event.server);
var newParent = event.params[1]; var newParent = event.params[1];
if(knownUsers.aliases.hasOwnProperty(newParent)) { if(knownUsers.aliases.hasOwnProperty(newParent)) {
@ -82,7 +94,7 @@ var users = function(dbot) {
'~mergeusers': function(event) { '~mergeusers': function(event) {
if(dbot.config.admins.include(event.user)) { if(dbot.config.admins.include(event.user)) {
var knownUsers = getServerUsers(event); var knownUsers = getServerUsers(event.server);
var primaryUser = event.params[1]; var primaryUser = event.params[1];
var secondaryUser = event.params[2]; var secondaryUser = event.params[2];
@ -106,9 +118,10 @@ var users = function(dbot) {
'name': 'users', 'name': 'users',
'ignorable': false, 'ignorable': false,
'commands': commands, 'commands': commands,
'api': api,
'listener': function(event) { 'listener': function(event) {
var knownUsers = getServerUsers(event); var knownUsers = getServerUsers(event.server);
if(event.action == 'JOIN') { if(event.action == 'JOIN') {
if(!knownUsers.users.include(event.user)) { if(!knownUsers.users.include(event.user)) {
knownUsers.users.push(event.user); knownUsers.users.push(event.user);

6
run.js
View File

@ -119,6 +119,7 @@ DBot.prototype.reloadModules = function() {
this.rawModules = []; this.rawModules = [];
this.modules = {}; this.modules = {};
this.commands = {}; this.commands = {};
this.api = {};
this.commandMap = {}; // Map of which commands belong to which modules this.commandMap = {}; // Map of which commands belong to which modules
this.usage = {}; this.usage = {};
this.timers.clearTimers(); this.timers.clearTimers();
@ -194,6 +195,11 @@ DBot.prototype.reloadModules = function() {
} }
} }
// Load module API
if(module.api) {
this.api[module.name] = module.api;
}
// Load the module usage data // Load the module usage data
try { try {
var usage = JSON.parse(fs.readFileSync(moduleDir + 'usage.json', 'utf-8')); var usage = JSON.parse(fs.readFileSync(moduleDir + 'usage.json', 'utf-8'));