2015-02-04 17:34:59 +01:00
|
|
|
/**
|
|
|
|
* Module Name: Karma
|
|
|
|
* Description: Thanking, with Karma!
|
|
|
|
*/
|
|
|
|
var _ = require('underscore')._;
|
|
|
|
|
|
|
|
var karma = function(dbot) {
|
|
|
|
this.lastKarma = {};
|
|
|
|
|
|
|
|
this.internalAPI = {
|
|
|
|
'getKarma': function(item, callback) {
|
|
|
|
this.db.read('karma', item.toLowerCase(), callback);
|
|
|
|
}.bind(this),
|
|
|
|
|
|
|
|
'setKarma': function(item, value, callback) {
|
2015-02-04 18:00:47 +01:00
|
|
|
this.db.save('karma', item.toLowerCase(), {
|
2015-02-04 18:02:39 +01:00
|
|
|
'item': item.toLowerCase(),
|
2015-02-04 18:00:47 +01:00
|
|
|
'karma': value
|
|
|
|
}, callback);
|
2015-02-04 17:34:59 +01:00
|
|
|
}.bind(this)
|
|
|
|
};
|
|
|
|
|
|
|
|
this.commands = {
|
|
|
|
'karma': function(event) {
|
|
|
|
var item = event.params[1] || event.user;
|
2015-02-04 18:05:20 +01:00
|
|
|
this.internalAPI.getKarma(item, function(err, karma) {
|
2015-02-04 18:04:50 +01:00
|
|
|
if(karma) {
|
|
|
|
karma = karma.karma;
|
|
|
|
event.reply(dbot.t('karma', {
|
|
|
|
'item': item,
|
|
|
|
'karma': karma
|
|
|
|
}));
|
|
|
|
}
|
2015-02-04 17:34:59 +01:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
'setkarma': function(event) {
|
|
|
|
var item = event.params[1],
|
|
|
|
value = parseInt(event.params[2]);
|
|
|
|
|
|
|
|
this.internalAPI.setKarma(item, value, function(err, karma) {
|
|
|
|
event.reply(dbot.t('newkarma', {
|
|
|
|
'item': item,
|
|
|
|
'value': value
|
|
|
|
}));
|
|
|
|
});
|
2015-02-04 18:00:47 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
'unkarmaiest': function(event) {
|
|
|
|
var karmas = {};
|
|
|
|
this.db.scan('karma', function(karma) {
|
|
|
|
if(karma && !_.isUndefined(karma.item)) {
|
|
|
|
karmas[karma.item] = karma.karma;
|
|
|
|
}
|
|
|
|
}.bind(this), function(err) {
|
|
|
|
var qSizes = _.chain(karmas)
|
|
|
|
.pairs()
|
|
|
|
.sortBy(function(category) { return category[1]; })
|
|
|
|
.first(10)
|
|
|
|
.value();
|
|
|
|
|
|
|
|
var qString = 'Unkarmaiest: ';
|
|
|
|
for(var i=0;i<qSizes.length;i++) {
|
|
|
|
qString += qSizes[i][0] + " (" + qSizes[i][1] + "), ";
|
|
|
|
}
|
|
|
|
|
|
|
|
event.reply(qString.slice(0, -2));
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
'karmaiest': function(event) {
|
|
|
|
var karmas = {};
|
|
|
|
this.db.scan('karma', function(karma) {
|
|
|
|
if(karma && !_.isUndefined(karma.item)) {
|
|
|
|
karmas[karma.item] = karma.karma;
|
|
|
|
}
|
|
|
|
}.bind(this), function(err) {
|
|
|
|
var qSizes = _.chain(karmas)
|
|
|
|
.pairs()
|
|
|
|
.sortBy(function(category) { return category[1]; })
|
|
|
|
.reverse()
|
|
|
|
.first(10)
|
|
|
|
.value();
|
|
|
|
|
|
|
|
var qString = 'Karmaiest: ';
|
|
|
|
for(var i=0;i<qSizes.length;i++) {
|
|
|
|
qString += qSizes[i][0] + " (" + qSizes[i][1] + "), ";
|
|
|
|
}
|
|
|
|
|
|
|
|
event.reply(qString.slice(0, -2));
|
|
|
|
});
|
2015-02-04 17:34:59 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
this.commands.setkarma.access = 'admin';
|
|
|
|
|
|
|
|
this.listener = function(event) {
|
|
|
|
var match = event.message.match(/^(.+)(\+\+|\-\-)$/);
|
|
|
|
if(match) {
|
2015-02-04 17:38:56 +01:00
|
|
|
match[1] = match[1].replace(/(\+|\-)/g,'');
|
2015-02-04 17:34:59 +01:00
|
|
|
if(_.has(this.lastKarma, event.rUser.id) && this.lastKarma[event.rUser.id] + 5000 > Date.now()) {
|
|
|
|
return event.reply('Try again in a few seconds : - )');
|
|
|
|
}
|
|
|
|
|
|
|
|
this.internalAPI.getKarma(match[1], function(err, karma) {
|
|
|
|
if(!karma) {
|
|
|
|
karma = 0;
|
2015-02-04 18:00:47 +01:00
|
|
|
} else {
|
|
|
|
karma = karma.karma;
|
2015-02-04 17:34:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if(match[2] === '--') {
|
|
|
|
karma -= 1;
|
|
|
|
} else {
|
|
|
|
karma += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.internalAPI.setKarma(match[1], karma, function(err, karma) {
|
|
|
|
this.lastKarma[event.rUser.id] = Date.now();
|
|
|
|
event.reply(dbot.t('newkarma', {
|
|
|
|
'item': match[1],
|
2015-02-04 18:00:47 +01:00
|
|
|
'value': karma.karma
|
2015-02-04 17:34:59 +01:00
|
|
|
}));
|
|
|
|
}.bind(this));
|
|
|
|
}.bind(this));
|
|
|
|
}
|
|
|
|
}.bind(this);
|
|
|
|
this.on = 'PRIVMSG';
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.fetch = function(dbot) {
|
|
|
|
return new karma(dbot);
|
|
|
|
};
|