jonbot/jonbot.js
2016-05-17 11:15:02 +00:00

69 lines
1.9 KiB
JavaScript

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(["jonbot"],["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.api.reactions.add({
timestamp: message.ts,
channel: message.channel,
name: "clap",
},function(err, res) {
if (err) {
bot.botkit.log('Failed to add reaction emoji');
}
});
});
// Lol, Christoffer er en noob.
controller.hears(["jon"],["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({
timestamp: message.ts,
channel: message.channel,
name: "clap",
},function(err, res) {
if (err) {
bot.botkit.log('Failed to add reaction emoji');
}
});
});