dbot/modules/sstats/commands.js

150 lines
5.6 KiB
JavaScript
Raw Normal View History

2013-10-17 11:10:01 +02:00
var _ = require('underscore')._,
async = require('async');
var commands = function(dbot) {
var commands = {
2013-10-17 05:59:29 +02:00
'~words': function(event) {
var getWords = function(user) {
this.api.getUserStats(user.id, function(uStats) {
if(uStats) {
var output = dbot.t('sstats_uwords', {
'user': user.primaryNick,
'words': uStats.words,
'curses': uStats.curses,
'capitals': uStats.capitals
});
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]) {
dbot.api.users.resolveUser(event.server, event.params[1], function(user) {
if(user) {
getWords(user);
} else {
event.reply(dbot.t('sstats_unknown_user'));
}
});
} else {
getWords(event.rUser);
}
},
'~lines': function(event) {
2013-10-16 17:22:45 +02:00
var getLines = function(user) {
this.api.getUserStats(user.id, function(uStats) {
if(uStats) {
var output = dbot.t('sstats_tlines', {
2013-10-16 17:22:45 +02:00
'user': user.primaryNick,
'lines': uStats.lines
});
if(event.rChannel && _.has(uStats.channels, event.rChannel.id)) {
output += dbot.t('sstats_uclines', {
'channel': event.channel,
'lines': uStats.channels[event.rChannel.id].lines
});
}
event.reply(output);
} else {
event.reply(dbot.t('sstats_noustats'));
}
});
2013-10-16 17:22:45 +02:00
}.bind(this);
if(event.params[1]) {
dbot.api.users.resolveUser(event.server, event.params[1], function(user) {
if(user) {
getLines(user);
} else {
event.reply(dbot.t('sstats_unknown_user'));
}
});
} else {
getLines(event.rUser);
}
},
2013-10-17 11:16:50 +02:00
// TODO: too much repeated code between loudest and cloudest yo
2013-10-17 11:10:01 +02:00
'~loudest': function(event) {
var lines = {};
this.db.scan('user_stats', function(uStats) {
lines[uStats.id] = uStats.lines;
}, function() {
var lCounts = _.chain(lines)
.pairs()
.sortBy(function(user) { return user[1]; })
.reverse()
.first(10)
.value();
async.eachSeries(lCounts, function(lCount, next) {
dbot.api.users.getUser(lCount[0], function(user) {
lCount[0] = user.primaryNick;
next();
});
}, function() {
var output = "Loudest users: ";
for(var i=0;i<lCounts.length;i++) {
output += lCounts[i][0] + " (" + lCounts[i][1] + "), ";
}
event.reply(output.slice(0, -2));
});
});
},
2013-10-17 11:16:50 +02:00
'~cloudest': function(event) {
var lines = {};
this.db.scan('user_stats', function(uStats) {
if(_.has(uStats.channels, event.rChannel.id)) {
lines[uStats.id] = uStats.channels[event.rChannel.id].lines;
}
}, function() {
var lCounts = _.chain(lines)
.pairs()
.sortBy(function(user) { return user[1]; })
.reverse()
.first(10)
.value();
async.eachSeries(lCounts, function(lCount, next) {
dbot.api.users.getUser(lCount[0], function(user) {
lCount[0] = user.primaryNick;
next();
});
}, function() {
var output = "Loudest users in " + event.channel + ": ";
for(var i=0;i<lCounts.length;i++) {
output += lCounts[i][0] + " (" + lCounts[i][1] + "), ";
}
event.reply(output.slice(0, -2));
});
});
},
'~clines': function(event) {
if(!event.cStats) return;
event.reply(dbot.t('sstats_clines', {
'channel': event.channel,
'lines': event.cStats.lines
}));
}
};
return commands;
};
exports.fetch = function(dbot) {
return commands(dbot);
};