From 9746641f48b440f702b84c9226f621c1998d6b1e Mon Sep 17 00:00:00 2001 From: reality Date: Sun, 20 Oct 2013 18:29:54 +0000 Subject: [PATCH] track words and shit yo --- modules/sstats/api.js | 18 ++++++++++++++++++ modules/sstats/commands.js | 31 +++++++++++++++++++++++++++++++ modules/sstats/sstats.js | 22 ++++++++++++++++++++++ modules/sstats/strings.json | 3 +++ 4 files changed, 74 insertions(+) diff --git a/modules/sstats/api.js b/modules/sstats/api.js index b92e341..e7860ec 100644 --- a/modules/sstats/api.js +++ b/modules/sstats/api.js @@ -42,6 +42,24 @@ var api = function(dbot) { this.db.save('channel_stats', id, cStats, function(err, cStats) { callback(cStats); }); + }, + + 'createTrackedWord': function(word, callback) { + var tWord = { + 'total': 0, + 'channels': {}, + 'users': {}, + 'creation': new Date().getTime() + }; + this.db.save('tracked_words', word, tWord, function(err, tWord) { + callback(tWord); + }); + }, + + 'getTrackedWord': function(word, callback) { + this.db.read('tracked_words', word, function(err, tWord) { + callback(tWord); + }); } }; diff --git a/modules/sstats/commands.js b/modules/sstats/commands.js index e3ca433..397ce40 100644 --- a/modules/sstats/commands.js +++ b/modules/sstats/commands.js @@ -171,8 +171,39 @@ var commands = function(dbot) { event.reply('Unknown user.'); } }.bind(this)); + }, + + '~trackword': function(event) { + var word = event.params[1].trim(); + this.api.getTrackedWord(word, function(tWord) { + if(!tWord) { + this.api.createTrackedWord(word, function(tWord) { + event.reply('Now tracking ' + word); + }); + } else { + event.reply('Word already being tracked.'); + } + }.bind(this)); + }, + + '~word': function(event) { + var word = event.params[1].trim(); + this.api.getTrackedWord(word, function(tWord) { + if(tWord) { + event.reply(dbot.t('sstats_word', { + 'word': word, + 'total': tWord.total, + 'channels': _.keys(tWord.channels).length, + 'users': _.keys(tWord.users).length, + 'since': new Date(tWord.creation) + })); + } else { + event.reply(word + ' isn\'t being tracked.'); + } + }); } }; + commands['~trackword'].access = 'power_user'; return commands; }; diff --git a/modules/sstats/sstats.js b/modules/sstats/sstats.js index 26df7fb..c7ed450 100644 --- a/modules/sstats/sstats.js +++ b/modules/sstats/sstats.js @@ -110,6 +110,28 @@ var sstats = function(dbot) { event.uStats.channels[event.rChannel.id].capitals += capitals; event.uStats.channels[event.rChannel.id].curses += curses; } + + // Look for tracked words. + if(event.message.charAt(0) != '~') { + var wMap = {}; // Why reduce isn't working idk + _.each(words, function(word) { + if(!_.has(wMap, word)) wMap[word] = 0; + wMap[word]++; + }); + _.each(wMap, function(count, word) { + this.api.getTrackedWord(word, function(tWord) { + if(tWord) { + tWord.total += count; + if(!_.has(tWord.channels, event.rChannel.id)) tWord.channels[event.rChannel.id] = 0; + if(!_.has(tWord.users, event.rUser.id)) tWord.users[event.rUser.id] = 0; + tWord.channels[event.rChannel.id] += count; + tWord.users[event.rUser.id] += count; + this.db.save('tracked_words', word, tWord, function() {}); + } + }.bind(this)); + }, this); + } + this.db.save('channel_stats', event.cStats.id, event.cStats, function() {}); this.db.save('user_stats', event.uStats.id, event.uStats, function() {}); }.bind(this); diff --git a/modules/sstats/strings.json b/modules/sstats/strings.json index 223e03e..1fa527b 100644 --- a/modules/sstats/strings.json +++ b/modules/sstats/strings.json @@ -19,5 +19,8 @@ }, "sstats_ucwords": { "en": " - {words} words, {curses} curses, {capitals} capitalised words in {channel}" + }, + "sstats_word": { + "en": "{word} has been used {total} times by {users} users in {channels} channels since {since}." } }