From 4887b4901832aed2d9599141c4df31fd8a783e1d Mon Sep 17 00:00:00 2001 From: Villads Valur Korsholm Nielsen Date: Tue, 17 May 2016 11:21:29 +0000 Subject: [PATCH] =?UTF-8?q?tilf=C3=B8jede=20patternbot.js?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- patternbot.js | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 patternbot.js diff --git a/patternbot.js b/patternbot.js new file mode 100644 index 0000000..1c9126b --- /dev/null +++ b/patternbot.js @@ -0,0 +1,66 @@ +var Botkit = require('botkit'); +var controller = Botkit.slackbot({ + json_file_store: process.argv[3] +}); +var bot = controller.spawn({ + token: process.argv[2] +}) +bot.startRTM(function(err,bot,payload) { + if (err) { + 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(["pattern","^pattern$"],["direct_message","direct_mention","mention","ambient"],function(bot,message) { + console.log(message.user + " mentioned \"pattern\""); + + // Increment the logged amount of mentions + incrementKey(message.user, "bot_mentions", 1); + + // Reply properly + bot.reply(message,':clap: PAAATTERN! :clap:'); + bot.api.reactions.add({ + timestamp: message.ts, + channel: message.channel, + name: "clap", + },function(err, res) { + if (err) { + bot.botkit.log('Failed to add reaction emoji'); + } + }); +}); + +controller.hears(["pattern","^pattern$"],["direct_message","direct_mention","mention","ambient"],function(bot,message) { + console.log(message.user + " mentioned \"pattern\""); + + // Increment the logged amount of mentions + incrementKey(message.user, "jon_mentions", 1); + + // React with appropriate emoji + bot.api.reactions.add({ + timestamp: message.ts, + channel: message.channel, + name: "clap", + },function(err, res) { + if (err) { + bot.botkit.log('Failed to add reaction emoji'); + } + }); +}); \ No newline at end of file