add user stats 👏

This commit is contained in:
Christoffer Müller Madsen 2016-03-21 14:28:02 +01:00
parent feadb9ddd0
commit 032fff64e3
2 changed files with 35 additions and 1 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
node_modules/
data/

View File

@ -1,5 +1,7 @@
var Botkit = require('botkit'); var Botkit = require('botkit');
var controller = Botkit.slackbot(); var controller = Botkit.slackbot({
json_file_store: procss.argv[3]
});
var bot = controller.spawn({ var bot = controller.spawn({
token: process.argv[2] token: process.argv[2]
}) })
@ -8,7 +10,31 @@ bot.startRTM(function(err,bot,payload) {
throw new Error('Could not connect to Slack'); throw new Error('Could not connect to Slack');
} }
}); });
function createUserData(id) {
return {id: id, jon_mentions: 0, bot_mentions: 0};
};
function incrementKey(id, key, amount) {
controller.storage.users.get(id, function(err,user_data) {
if (!user_data) {
console.log("Initializing user data");
user_data = createUserData(id);
}
console.log("Current user data:",user_data);
console.log("Incrementing mentions. Was " + user_data[key] + " will be " + (user_data[key] + amount));
user_data[key] = user_data[key] + amount
controller.storage.users.save(user_data);
});
}
controller.hears(["jonbot","^pattern$"],["direct_message","direct_mention","mention","ambient"],function(bot,message) { controller.hears(["jonbot","^pattern$"],["direct_message","direct_mention","mention","ambient"],function(bot,message) {
console.log(message.user + " mentioned \"jonbot\"");
// Increment the logged amount of mentions
incrementKey(message.user, "bot_mentions", 1);
// Reply properly
bot.reply(message,':clap: JOOOOON! :clap:'); bot.reply(message,':clap: JOOOOON! :clap:');
bot.api.reactions.add({ bot.api.reactions.add({
timestamp: message.ts, timestamp: message.ts,
@ -22,6 +48,12 @@ controller.hears(["jonbot","^pattern$"],["direct_message","direct_mention","ment
}); });
controller.hears(["jon","^pattern$"],["direct_message","direct_mention","mention","ambient"],function(bot,message) { controller.hears(["jon","^pattern$"],["direct_message","direct_mention","mention","ambient"],function(bot,message) {
console.log(message.user + " mentioned \"jon\"");
// Increment the logged amount of mentions
incrementKey(message.user, "jon_mentions", 1);
// React with appropriate emoji
bot.api.reactions.add({ bot.api.reactions.add({
timestamp: message.ts, timestamp: message.ts,
channel: message.channel, channel: message.channel,