From b4e43381486de79aad2914c19645a70afc2271c9 Mon Sep 17 00:00:00 2001 From: reality Date: Wed, 2 Jan 2013 21:06:43 +0000 Subject: [PATCH] ~mergeusers command [#94] --- modules/users/strings.json | 6 ++++++ modules/users/users.js | 40 +++++++++++++++++++++++++++++++------- 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/modules/users/strings.json b/modules/users/strings.json index b334100..3aaf5bb 100644 --- a/modules/users/strings.json +++ b/modules/users/strings.json @@ -10,5 +10,11 @@ }, "aliasparentset": { "english": "{newParent} is now the parent user, and {newAlias} is an alias." + }, + "unprimary_error": { + "english": "One of those users isn't currently recorded as a primary user." + }, + "merged_users": { + "english": "{old_user} and their aliases have been merged into {new_user}." } } diff --git a/modules/users/users.js b/modules/users/users.js index ab342a4..df95052 100644 --- a/modules/users/users.js +++ b/modules/users/users.js @@ -11,6 +11,17 @@ var users = function(dbot) { return knownUsers[event.server]; }; + var updateAliases = function(event, oldUser, newUser) { + var knownUsers = getServerUsers(event); + for(var alias in knownUsers.aliases) { + if(knownUsers.aliases.hasOwnProperty(alias)) { + if(knownUsers.aliases[alias] === oldUser) { + knownUsers.aliases[alias] = newUser; + } + } + } + } + dbot.instance.addListener('366', 'users', function(event) { var knownUsers = getServerUsers(event); for(var nick in event.channel.nicks) { @@ -56,13 +67,7 @@ var users = function(dbot) { knownUsers.aliases[newAlias] = newParent; // Update aliases to point to new primary user - for(var alias in knownUsers.aliases) { - if(knownUsers.aliases.hasOwnProperty(alias)) { - if(knownUsers.aliases[alias] === newAlias) { - knownUsers.aliases[alias] = newParent; - } - } - } + updateAliases(event, newAlias, newParent); event.reply(dbot.t('aliasparentset', { 'newParent': newParent, 'newAlias': newAlias })); @@ -72,6 +77,27 @@ var users = function(dbot) { event.reply(dbot.t('unknown_alias', { 'alias': newParent})); } } + }, + + '~mergeusers': function(event) { + if(dbot.config.admins.include(event.user)) { + var knownUsers = getServerUsers(event); + var primaryUser = event.params[1]; + var secondaryUser = event.params[2]; + + if(knownUsers.users.include(primaryUser) && knownUsers.users.include(secondaryUser)) { + knownUsers.users.splice(knownUsers.users.indexOf(secondaryUser), 1); + knownUsers.aliases[secondaryUser] = primaryUser; + updateAliases(event, secondaryUser, primaryUser); + + event.reply(dbot.t('merged_users', { + 'old_user': secondaryUser, + 'new_user': primaryUser + })); + } else { + event.reply(dbot.t('unprimary_error')); + } + } } };