track words and shit yo

This commit is contained in:
reality 2013-10-20 18:29:54 +00:00
parent e45aa0bcde
commit 9746641f48
4 changed files with 74 additions and 0 deletions

View File

@ -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);
});
}
};

View File

@ -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;
};

View File

@ -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);

View File

@ -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}."
}
}