dbot/modules/users/commands.js

146 lines
6.4 KiB
JavaScript
Raw Normal View History

var _ = require('underscore')._;
var commands = function(dbot) {
var commands = {
'~alias': function(event) {
var nick = event.params[1].trim();
this.api.resolveUser(event.server, nick, function(user) {
if(user) {
if(nick == user.primaryNick) {
var aliases = _.first(user.aliases, 10);
var including = 'including: ' + aliases.join(', ') + '.';
if(user.aliases.length != 0) {
event.reply(dbot.t('primary', {
'user': nick,
'currentNick': user.currentNick,
'count': user.aliases.length,
}) + including);
} else {
event.reply(dbot.t('primary', {
'user': nick,
'currentNick': user.currentNick,
'count': user.aliases.length
}).slice(0, -2) + ".");
}
} else {
event.reply(dbot.t('alias', {
'alias': nick,
'user': user.primaryNick
}));
}
2013-01-20 19:26:07 +01:00
} else {
event.reply(dbot.t('unknown_alias', { 'alias': nick }));
2013-01-20 19:19:44 +01:00
}
});
},
'~addalias': function(event) {
var nick = event.input[1],
alias = event.input[2];
this.api.resolveUser(event.server, nick, function(user) {
if(user) {
if(!_.include(user.aliases, alias)) {
user.aliases.push(alias);
this.db.save('users', user.id, user, function(err) {
if(!err) {
event.reply(dbot.t('alias_added', {
'user': user.primaryNick,
'alias': alias
}));
}
});
} else {
event.reply(dbot.t('alias_exists', { 'alias': alias }));
}
} else {
event.reply(dbot.t('unknown_alias', { 'alias': nick }));
}
}.bind(this));
},
'~setaliasparent': function(event) {
var newPrimary = event.params[1].trim();
this.api.resolveUser(event.server, newPrimary, function(user) {
2013-03-24 17:59:26 +01:00
if(user && user.primaryNick != newPrimary) {
var newAlias = user.primaryNick;
user.primaryNick = newPrimary;
2013-03-24 17:59:26 +01:00
user.aliases = _.without(user.aliases, newPrimary);
user.aliases.push(newAlias);
this.internalAPI.updateChannelPrimaryUser(event.server, newAlias, newPrimary);
this.db.save('users', user.id, user, function(err) {
if(!err) {
2013-03-24 17:59:26 +01:00
event.reply(dbot.t('aliasparentset', {
'newParent': newPrimary,
'newAlias': newAlias
}));
dbot.api.event.emit('~setaliasparent', {
'server': event.server,
'alias': newAlias
});
}
});
} else {
event.reply(dbot.t('unknown_alias', { 'alias': newPrimarj }));
}
}.bind(this));
},
'~mergeusers': function(event) {
var primaryUser = event.params[1];
var secondaryUser = event.params[2];
this.api.resolveUser(event.server, primaryUser, function(user) {
if(user) {
this.api.resolveUser(event.server, secondaryUser, function(oldUser) {
2013-05-26 15:29:21 +02:00
if(oldUser) {
user.aliases.push(oldUser.primaryNick);
2013-05-26 15:29:21 +02:00
user.aliases = _.union(user.aliases, oldUser.aliases);
2013-05-26 15:11:09 +02:00
this.internalAPI.mergeChannelUsers(event.server, oldUser, user);
this.db.del('users', oldUser.id, function(err) {
if(!err) {
this.db.save('users', user.id, user, function(err) {
if(!err) {
this.internalAPI.mergeChannelUsers(event.server, secondaryUser, primaryUser);
event.reply(dbot.t('merged_users', {
'old_user': secondaryUser,
'new_user': primaryUser
}));
2013-05-26 15:11:09 +02:00
dbot.api.event.emit('~mergeusers', [
event.server,
oldUser,
user
]);
}
2013-03-24 17:59:26 +01:00
}.bind(this));
}
2013-03-24 17:59:26 +01:00
}.bind(this));
} else {
event.reply(dbot.t('unprimary_error', { 'nick': secondaryUser }));
}
2013-03-24 17:59:26 +01:00
}.bind(this));
} else {
event.reply(dbot.t('unprimary_error', { 'nick': primaryUser }));
}
2013-03-24 17:59:26 +01:00
}.bind(this));
}
};
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['~addalias'].regex = [/^~addalias ([\d\w[\]{}^|\\`_-]+?) ([\d\w[\]{}^|\\`_-]+?)$/, 3];
commands['~setaliasparent'].access = 'moderator';
commands['~mergeusers'].access = 'moderator';
commands['~addalias'].access = 'moderator';
return commands;
};
exports.fetch = function(dbot) {
return commands(dbot);
};