patternbot/jonbot.js

67 lines
1.9 KiB
JavaScript
Raw Normal View History

2016-02-22 13:50:42 +00:00
var Botkit = require('botkit');
2016-03-21 13:28:02 +00:00
var controller = Botkit.slackbot({
json_file_store: procss.argv[3]
});
2016-02-22 13:50:42 +00:00
var bot = controller.spawn({
token: process.argv[2]
})
bot.startRTM(function(err,bot,payload) {
if (err) {
throw new Error('Could not connect to Slack');
}
});
2016-03-21 13:28:02 +00:00
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);
});
}
2016-02-22 14:54:22 +00:00
controller.hears(["jonbot","^pattern$"],["direct_message","direct_mention","mention","ambient"],function(bot,message) {
2016-03-21 13:28:02 +00:00
console.log(message.user + " mentioned \"jonbot\"");
// Increment the logged amount of mentions
incrementKey(message.user, "bot_mentions", 1);
// Reply properly
2016-02-22 13:50:42 +00:00
bot.reply(message,':clap: JOOOOON! :clap:');
2016-02-22 14:54:22 +00:00
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');
}
});
2016-02-22 13:50:42 +00:00
});
2016-02-22 14:03:20 +00:00
controller.hears(["jon","^pattern$"],["direct_message","direct_mention","mention","ambient"],function(bot,message) {
2016-03-21 13:28:02 +00:00
console.log(message.user + " mentioned \"jon\"");
// Increment the logged amount of mentions
incrementKey(message.user, "jon_mentions", 1);
// React with appropriate emoji
2016-02-22 14:03:20 +00:00
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');
}
});
});