3
0
mirror of https://github.com/reality/dbot.git synced 2024-12-25 04:02:39 +01:00

fixes for merge and setaliasparent, basic sstats functionality

This commit is contained in:
reality 2014-09-04 12:15:11 +00:00
parent 8ba4db33a7
commit 1037070bff
4 changed files with 73 additions and 74 deletions

View File

@ -5,46 +5,41 @@ var _ = require('underscore')._,
var commands = function(dbot) { var commands = function(dbot) {
var commands = { var commands = {
'~words': function(event) { '~words': function(event) {
var getWords = function(user) { var id = event.rUser.primaryNick + '.' + event.server,
this.api.getUserStats(user.id, function(uStats) { user = event.rUser.currentNick,
if(uStats) { cId = event.channel + '.' + event.server;
var output = dbot.t('sstats_uwords', {
'user': user.primaryNick,
'words': uStats.words,
'curses': uStats.curses,
'capitals': uStats.capitals,
'date': moment(uStats.creation).format('DD/MM/YYYY')
});
if(event.rChannel && _.has(uStats.channels, event.rChannel.id)) {
ucStats = uStats.channels[event.rChannel.id];
output += dbot.t('sstats_ucwords', {
'channel': event.channel,
'words': ucStats.words,
'curses': ucStats.curses,
'capitals': ucStats.capitals
});
}
event.reply(output);
} else {
event.reply(dbot.t('sstats_noustats'));
}
});
}.bind(this);
if(event.params[1]) { if(event.params[1]) {
dbot.api.users.resolveUser(event.server, event.params[1], function(user) { id = event.params[1] + '.' + event.server;
if(user) { user = event.params[1];
getWords(user);
} else {
event.reply(dbot.t('sstats_unknown_user'));
}
});
} else {
getWords(event.rUser);
} }
this.api.getUserStats(id, function(uStats) {
if(uStats) {
var output = dbot.t('sstats_uwords', {
'user': user,
'words': uStats.words,
'curses': uStats.curses,
'capitals': uStats.capitals,
'date': moment(uStats.creation).format('DD/MM/YYYY')
});
if(_.has(uStats.channels, cId)) {
ucStats = uStats.channels[cId];
output += dbot.t('sstats_ucwords', {
'channel': event.channel,
'words': ucStats.words,
'curses': ucStats.curses,
'capitals': ucStats.capitals
});
}
event.reply(output);
} else {
event.reply(dbot.t('sstats_noustats'));
}
});
}, },
'~lines': function(event) { '~lines': function(event) {
var cId = event.channel + '.' + event.server;
var getLines = function(user) { var getLines = function(user) {
this.api.getUserStats(user.id, function(uStats) { this.api.getUserStats(user.id, function(uStats) {
if(uStats) { if(uStats) {
@ -53,10 +48,10 @@ var commands = function(dbot) {
'lines': uStats.lines, 'lines': uStats.lines,
'date': moment(uStats.creation).format('DD/MM/YYYY') 'date': moment(uStats.creation).format('DD/MM/YYYY')
}); });
if(event.rChannel && _.has(uStats.channels, event.rChannel.id)) { if(_.has(uStats.channels, cId)) {
output += dbot.t('sstats_uclines', { output += dbot.t('sstats_uclines', {
'channel': event.channel, 'channel': event.channel,
'lines': uStats.channels[event.rChannel.id].lines 'lines': uStats.channels[cId].lines
}); });
} }
event.reply(output); event.reply(output);

View File

@ -44,7 +44,7 @@ var sstats = function(dbot) {
.value(); .value();
async.eachSeries(pCounts, function(pCount, next) { async.eachSeries(pCounts, function(pCount, next) {
dbot.api.users.getUser(pCount[0], function(user) { dbot.api.users.getUser(pCount[0], function(err, user) {
pCount[0] = user.primaryNick; next(); pCount[0] = user.primaryNick; next();
}); });
}, function() { }, function() {
@ -54,14 +54,9 @@ var sstats = function(dbot) {
}.bind(this), }.bind(this),
'channelHighscore': function(key, server, channel, property, callback) { 'channelHighscore': function(key, server, channel, property, callback) {
dbot.api.users.resolveChannel(server, channel, function(channel) { var cId = channel + '.' + server;
if(channel) { var newProperty = 'channels.' + cId + '.' + property;
var newProperty = 'channels.' + channel.id + '.' + property; this.internalAPI.highscore(key, newProperty, callback);
this.internalAPI.highscore(key, newProperty, callback);
} else {
callback(null);
}
}.bind(this));
}.bind(this), }.bind(this),
'formatHighscore': function(string, pCounts) { 'formatHighscore': function(string, pCounts) {
@ -78,6 +73,7 @@ var sstats = function(dbot) {
event.uStats.lines++; event.uStats.lines++;
var message = event.message.replace(/[\.,-\/#!$%\^&\*;:{}=\-_`~()]/g, ""); var message = event.message.replace(/[\.,-\/#!$%\^&\*;:{}=\-_`~()]/g, "");
var cId = event.channel + '.' + event.server;
var words = message.split(' '), var words = message.split(' '),
wCount = words.length, wCount = words.length,
capitals = 0, capitals = 0,
@ -98,18 +94,18 @@ var sstats = function(dbot) {
event.cStats.capitals += capitals; event.cStats.capitals += capitals;
event.cStats.curses += curses; event.cStats.curses += curses;
if(!_.has(event.uStats.channels, event.rChannel.id)) { if(!_.has(event.uStats.channels, cId)) {
event.uStats.channels[event.rChannel.id] = { event.uStats.channels[cId] = {
'lines': 1, 'lines': 1,
'words': wCount, 'words': wCount,
'capitals': capitals, 'capitals': capitals,
'curses': curses 'curses': curses
}; };
} else { } else {
event.uStats.channels[event.rChannel.id].lines++; event.uStats.channels[cId].lines++;
event.uStats.channels[event.rChannel.id].words += wCount; event.uStats.channels[cId].words += wCount;
event.uStats.channels[event.rChannel.id].capitals += capitals; event.uStats.channels[cId].capitals += capitals;
event.uStats.channels[event.rChannel.id].curses += curses; event.uStats.channels[cId].curses += curses;
} }
// Look for tracked words. // Look for tracked words.
@ -124,9 +120,9 @@ var sstats = function(dbot) {
this.api.getTrackedWord(word, function(tWord) { this.api.getTrackedWord(word, function(tWord) {
if(tWord) { if(tWord) {
tWord.total += count; tWord.total += count;
if(!_.has(tWord.channels, event.rChannel.id)) tWord.channels[event.rChannel.id] = 0; if(!_.has(tWord.channels, cId)) tWord.channels[cId] = 0;
if(!_.has(tWord.users, event.rUser.id)) tWord.users[event.rUser.id] = 0; if(!_.has(tWord.users, event.rUser.id)) tWord.users[event.rUser.id] = 0;
tWord.channels[event.rChannel.id] += count; tWord.channels[cId] += count;
tWord.users[event.rUser.id] += count; tWord.users[event.rUser.id] += count;
this.db.save('tracked_words', word, tWord, function() {}); this.db.save('tracked_words', word, tWord, function() {});
} }
@ -158,13 +154,14 @@ var sstats = function(dbot) {
// Preload channel stats // Preload channel stats
dbot.instance.addPreEmitHook(function(event, callback) { dbot.instance.addPreEmitHook(function(event, callback) {
if(!event.rChannel) return callback(); if(!event.channel) return callback();
this.api.getChannelStats(event.rChannel.id, function(cStats) { var id = event.channel + '.' + event.server;
this.api.getChannelStats(id, function(cStats) {
if(cStats) { if(cStats) {
event.cStats = cStats; event.cStats = cStats;
callback(); callback();
} else { } else {
this.api.createChannelStats(event.rChannel.id, function(cStats) { this.api.createChannelStats(id, function(cStats) {
event.cStats = cStats; event.cStats = cStats;
callback(); callback();
}); });

View File

@ -65,7 +65,7 @@ var commands = function(dbot) {
'~setaliasparent': function(event) { '~setaliasparent': function(event) {
var newPrimary = event.params[1]; var newPrimary = event.params[1];
this.api.resolveUser(event.server, newPrimary, function(user) { this.api.resolveUser(event.server, newPrimary, function(err, user) {
if(user) { if(user) {
if(user.primaryNick !== newPrimary) { if(user.primaryNick !== newPrimary) {
this.internalAPI.reparentUser(user, newPrimary, function() { this.internalAPI.reparentUser(user, newPrimary, function() {
@ -78,9 +78,9 @@ var commands = function(dbot) {
event.reply(dbot.t('already_primary', { 'user': newPrimary })); event.reply(dbot.t('already_primary', { 'user': newPrimary }));
} }
} else { } else {
event.reply(dbot.t('unknown_alias', { 'alias': nick })); event.reply(dbot.t('unknown_alias', { 'alias': newPrimary }));
} }
}); }.bind(this));
}, },
'~rmalias': function(event) { '~rmalias': function(event) {
@ -107,11 +107,11 @@ var commands = function(dbot) {
this.api.resolveUser(event.server, oldNick, function(err, oldUser) { this.api.resolveUser(event.server, oldNick, function(err, oldUser) {
if(oldUser) { if(oldUser) {
this.api.resolveUser(event.server, newNick, function(err, newUser) { this.api.resolveUser(event.server, newNick, function(err, newUser) {
if(newUser) { if(newUser && newUser.id !== oldUser.id) {
this.internalAPI.mergeUsers(oldUser, newUser, function() { this.internalAPI.mergeUsers(oldUser, newUser, function() {
event.reply(dbot.t('merged_users', { event.reply(dbot.t('merged_users', {
'old_user': secondaryUser, 'old_user': oldNick,
'new_user': primaryUser 'new_user': newNick
})); }));
}); });
} else { } else {

View File

@ -35,7 +35,7 @@ var users = function(dbot) {
'user': user.id 'user': user.id
}, function(err, result) { }, function(err, result) {
if(!err) { if(!err) {
dbot.api.event.emit('new_user_alias', [ event.rUser, event.newNick ]); dbot.api.event.emit('new_user_alias', [ user, alias ]);
callback(null, result); callback(null, result);
} else { } else {
callback(true, null); callback(true, null);
@ -67,9 +67,15 @@ var users = function(dbot) {
// Merge two user records and aliases // Merge two user records and aliases
'mergeUsers': function(oldUser, newUser, callback) { 'mergeUsers': function(oldUser, newUser, callback) {
this.db.search('user_aliases', { 'user': oldUser.id }, function(alias) { this.db.search('user_aliases', { 'user': oldUser.id }, function(alias) {
alias.user = newUser.id; if(alias.alias === newUser.primaryNick) {
this.db.save('user_aliases', alias.id, alias, function(){}); this.db.del('user_aliases', alias.id, function(){});
}.bind(this), function(){}); } else {
alias.user = newUser.id;
this.db.save('user_aliases', alias.id, alias, function(){});
}
}.bind(this), function(){
this.internalAPI.createAlias(oldUser.primaryNick, newUser, function(){});
}.bind(this));
this.db.del('users', oldUser.id, function(err) { this.db.del('users', oldUser.id, function(err) {
if(!err) { if(!err) {
@ -99,13 +105,14 @@ var users = function(dbot) {
// Track nick changes // Track nick changes
this.listener = function(event) { this.listener = function(event) {
// Update current nick // Update current nick
this.internalAPI.updateCurrentNick(event.rUser, event.newNick, function(){}); this.api.resolveUser(event.server, event.user, function(err, user) {
console.log(user);
// Add new alias record if nick is not already claimed this.internalAPI.updateCurrentNick(user, event.newNick, function(){});
this.api.resolveUser(event.server, event.newNick, function(err, user) { this.api.resolveUser(event.server, event.newNick, function(err, eUser) {
if(!user) { if(!eUser) {
this.internalAPI.createAlias(event.newNick, event.rUser, function(){}); this.internalAPI.createAlias(event.newNick, user, function(){});
} }
}.bind(this));
}.bind(this)); }.bind(this));
}.bind(this); }.bind(this);
this.on = ['NICK']; this.on = ['NICK'];