diff --git a/modules/sstats/api.js b/modules/sstats/api.js index e7860ec..fdf31ce 100644 --- a/modules/sstats/api.js +++ b/modules/sstats/api.js @@ -46,6 +46,7 @@ var api = function(dbot) { 'createTrackedWord': function(word, callback) { var tWord = { + 'word': word, 'total': 0, 'channels': {}, 'users': {}, diff --git a/modules/sstats/sstats.js b/modules/sstats/sstats.js index ab22723..efd3fc7 100644 --- a/modules/sstats/sstats.js +++ b/modules/sstats/sstats.js @@ -171,6 +171,36 @@ var sstats = function(dbot) { } }.bind(this)); }.bind(this)); + + dbot.api.event.addHook('~mergeusers', function(server, oldUser, newUser) { + this.api.getUserStats(oldUser.id, function(ouStats) { + this.api.getUserStats(newUser.id, function(nuStats) { + _.each(ouStats, function(stat, key) { + if(_.isObject(stat) && key != 'creation') { + _.each(ouStats[key], function(stat, sKey) { + nuStats[key][sKey] += stat; + }); + } else { + nuStats[key] += stat; + } + }); + this.db.del('user_stats', oldUser.id, function() {}); + this.db.save('user_stats', newUser.id, function() {}); + }); + }); + + this.db.scan('tracked_words', function(tWord) { + if(_.has(tWord.users, oldUser.id)) { + if(_.has(tWord.users, newUser.id)) { + tWord.users[newUser.id] += tWord.users[oldUser.id]; + } else { + tWord.users[newUser.id] = tWord.users[oldUser.id]; + } + delete tWord.users[oldUser.id]; + this.db.save('tracked_words', tWord.word, tWord, function() {}); + } + }); + }.bind(this)); }.bind(this); };