Skip to content

Instantly share code, notes, and snippets.

@captDaylight
Last active September 8, 2016 04:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save captDaylight/c76325231f73518a7ee926587c618cd9 to your computer and use it in GitHub Desktop.
Save captDaylight/c76325231f73518a7ee926587c618cd9 to your computer and use it in GitHub Desktop.
if (!process.env.token) {
process.exit(1);
}
const Botkit = require('botkit');
const controller = Botkit.slackbot({
debug: true,
});
controller.spawn({
token: process.env.token,
}).startRTM();
const { hears, storage: { channels } } = controller;
function privateConvo(bot, message) {
const { user, channel } = message;
return (err, convo) => {
if (err) throw err;
convo.ask('Do you want to play `paper`, `rock`, or `scissors`?', [
{
pattern: 'paper|rock|scissors',
callback(response, convo) {
// since no further messages are queued after this,
// the conversation will end naturally with status === 'completed'
convo.next();
},
}, {
default: true,
callback(response, convo) {
convo.repeat();
convo.next();
},
},
], { key: 'rockPaperScissors' }); // store the results in a field called rockPaperScissors
convo.on('end', (convo) => {
if (convo.status === 'completed') {
const prc = convo.extractResponse('rockPaperScissors');
channels.get(channel, (err, data) => {
if (err) throw err;
const updateData = data;
updateData.players[user].played = prc;
const { players } = updateData;
const playerIDs = Object.keys(players);
// check if only one player has played
const onlyOnePlayed = playerIDs.find((id) => players[id].played === '');
if (onlyOnePlayed) {
channels.save(updateData, (err) => {
if (err) throw err;
bot.reply(message, `<@${user}> has played!`);
});
} else {
const gameResults = playerIDs.map((id) => `<@${id}> played ${players[id].played}`);
bot.reply(message, gameResults.join(' & '));
// reset the game data
channels.save({ id: updateData.id }, (err) => {
if (err) throw err;
});
}
});
} else {
// this happens if the conversation ended prematurely for some reason
bot.reply(message, 'OK, nevermind!');
}
});
};
}
hears(['play'], 'direct_message,direct_mention,mention', (bot, message) => {
const { user, channel, text } = message;
const userData = text.match(/<@([A-Z0-9]{9})>/);
if (userData) {
const playerTwo = userData[1];
const gameData = {
id: channel,
players: {
[user]: {
accepted: true,
played: '',
},
[playerTwo]: {
accepted: false,
played: '',
},
},
};
channels.save(gameData, (err) => {
if (err) throw err;
bot.say({
text: `<@${playerTwo}> you've been challenged to a game of ROCK PAPER SCISSORS by <@${user}>, say \`accept\` unless you're too scared.`,
channel,
});
bot.startPrivateConversation(message, privateConvo(bot, message));
});
} else {
bot.reply(message, 'You didn\'t challenge anyone...');
}
});
hears(['accept'], 'ambient', (bot, message) => {
const { channel } = message;
channels.get(channel, (err, data) => {
if (err) throw err;
if (data && 'players' in data) {
const { user } = message;
const { players } = data;
if (user in players && !players[user].accepted) {
bot.reply(message, 'GREAT, LET THE BATTLE BEGIN!!!');
bot.startPrivateConversation(message, privateConvo(bot, message));
} else {
const player = Object.keys(players).find((p) => !players[p].accepted);
bot.reply(message, `Not you <@${user}>, waiting for <@${player}>.`);
}
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment