3
0
mirror of https://github.com/reality/dbot.git synced 2024-12-26 20:52:39 +01:00
dbot/modules/users/commands.js

113 lines
4.1 KiB
JavaScript
Raw Normal View History

var _ = require('underscore')._;
var commands = function(dbot) {
var commands = {
'~alias': function(event) {
var knownUsers = this.getServerUsers(event.server),
alias = event.params[1].trim();
if(_.include(knownUsers.users, alias)) {
2013-01-20 19:19:44 +01:00
var aliases = this.api.getAliases(event.server, alias);
var aliasCount = aliases.length;
2013-01-20 19:26:07 +01:00
if(aliasCount != 0) {
var aliases = _.first(aliases, 10);
var including = 'including: ' + aliases.join(', ') + '.';
2013-01-20 19:26:07 +01:00
event.reply(dbot.t('primary', {
'user': alias,
'count': aliasCount
}) + including);
} else {
event.reply(dbot.t('primary', {
'user': alias,
'count': aliasCount
}).slice(0, -2) + ".");
2013-01-20 19:19:44 +01:00
}
} else if(_.has(knownUsers.aliases, alias)) {
event.reply(dbot.t('alias', {
'alias': alias,
'user': knownUsers.aliases[alias]
}));
} else {
event.reply(dbot.t('unknown_alias', { 'alias': alias }));
}
},
'~setaliasparent': function(event) {
var knownUsers = this.getServerUsers(event.server);
var newParent = event.params[1];
if(_.has(knownUsers.aliases, newParent)) {
var newAlias = knownUsers.aliases[newParent];
// Replace user entry
knownUsers.users = _.without(knownUsers.users, newAlias);
knownUsers.users.push(newParent);
// Replace channels entries with new primary user
this.updateChannels(event, newAlias, newParent);
// Remove alias for new parent & add alias for new alias
delete knownUsers.aliases[newParent];
knownUsers.aliases[newAlias] = newParent;
// Update aliases to point to new primary user
this.updateAliases(event, newAlias, newParent);
event.reply(dbot.t('aliasparentset', {
'newParent': newParent,
'newAlias': newAlias
}));
return {
'server': event.server,
'alias': newAlias
};
} else {
event.reply(dbot.t('unknown_alias', { 'alias': newParent }));
}
return false;
},
'~mergeusers': function(event) {
var knownUsers = this.getServerUsers(event.server);
var primaryUser = event.params[1];
var secondaryUser = event.params[2];
if(_.include(knownUsers.users, primaryUser) && _.include(knownUsers.users, secondaryUser)) {
knownUsers.users = _.without(knownUsers.users, secondaryUser);
knownUsers.aliases[secondaryUser] = primaryUser;
this.updateAliases(event, secondaryUser, primaryUser);
this.updateChannels(event, secondaryUser, primaryUser);
event.reply(dbot.t('merged_users', {
'old_user': secondaryUser,
'new_user': primaryUser
}));
return {
'server': event.server,
2013-01-17 23:40:49 +01:00
'secondary': secondaryUser
};
} else {
event.reply(dbot.t('unprimary_error'));
}
return false;
}
};
commands['~alias'].regex = [/^~alias ([\d\w[\]{}^|\\`_-]+?)/, 2];
commands['~setaliasparent'].regex = [/^~setaliasparent ([\d\w[\]{}^|\\`_-]+?)/, 2];
commands['~mergeusers'].regex = [/^~mergeusers ([\d\w[\]{}^|\\`_-]+?)\s*?([\d\w[\]{}^|\\`_-]+?)/, 3];
commands['~setaliasparent'].access = 'moderator';
commands['~mergeusers'].access = 'moderator';
return commands;
};
exports.fetch = function(dbot) {
return commands(dbot);
};