From d97e4969f90490555082015407891d29209503f3 Mon Sep 17 00:00:00 2001 From: reality Date: Wed, 4 Feb 2015 16:34:59 +0000 Subject: [PATCH] karma --- modules/karma/config.json | 5 +++ modules/karma/karma.js | 78 ++++++++++++++++++++++++++++++++++++++ modules/karma/strings.json | 8 ++++ 3 files changed, 91 insertions(+) create mode 100644 modules/karma/config.json create mode 100644 modules/karma/karma.js create mode 100644 modules/karma/strings.json diff --git a/modules/karma/config.json b/modules/karma/config.json new file mode 100644 index 0000000..32ed5ef --- /dev/null +++ b/modules/karma/config.json @@ -0,0 +1,5 @@ +{ + "announce": [ { "server": "tripsit", "name": "#dbot" } ], + "dbKeys": [ "karma" ], + "dbType": "redis" +} diff --git a/modules/karma/karma.js b/modules/karma/karma.js new file mode 100644 index 0000000..a82158d --- /dev/null +++ b/modules/karma/karma.js @@ -0,0 +1,78 @@ +/** + * 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) { + this.db.save('karma', item.toLowerCase(), value, callback); + }.bind(this) + }; + + this.commands = { + 'karma': function(event) { + var item = event.params[1] || event.user; + this.internalAPI.getKarma(event.server, target, function(err, karma) { + event.reply(dbot.t('karma', { + 'item': item, + 'karma': karma + })); + }); + }, + + '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 + })); + }); + } + }; + this.commands.setkarma.access = 'admin'; + + this.listener = function(event) { + var match = event.message.match(/^(.+)(\+\+|\-\-)$/); + if(match) { + 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; + } + + 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], + 'value': karma + })); + }.bind(this)); + }.bind(this)); + } + }.bind(this); + this.on = 'PRIVMSG'; +}; + +exports.fetch = function(dbot) { + return new karma(dbot); +}; diff --git a/modules/karma/strings.json b/modules/karma/strings.json new file mode 100644 index 0000000..9b3b449 --- /dev/null +++ b/modules/karma/strings.json @@ -0,0 +1,8 @@ +{ + "karma": { + "en": "{item} has {karma} karma" + }, + "newkarma": { + "en": "{item} now has {value} karma" + } +}