This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| if (userData) { | |
| ... | |
| 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, | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // a bot can listen for text it's not mentioned in | |
| // by passing 'ambient' as the second parameter | |
| hears(['accept'], 'ambient', (bot, message) => { | |
| const { channel } = message; | |
| channels.get(channel, (err, data) => { | |
| if (err) throw err; | |
| if (data && 'players' in data) { | |
| // game has started ! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ... | |
| if (data && 'players' in data) { | |
| const { user } = message; | |
| const { players } = data; | |
| if (user in players && !players[user].accepted) { | |
| // player two accepted | |
| bot.reply(message, 'GREAT, LET THE BATTLE BEGIN!!!'); | |
| } else { | |
| const player = Object.keys(players).find((p) => !players[p].accepted); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| hears([‘play’], ‘direct_message,direct_mention,mention’, (bot, message) => { | |
| ... | |
| 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, | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| hears(['accept'], 'ambient', (bot, message) => { | |
| ... | |
| if (user in players && !players[user].accepted) { | |
| bot.reply(message, 'GREAT, LET THE BATTLE BEGIN!!!'); | |
| bot.startPrivateConversation(message, callback); | |
| } else { | |
| ... | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function privateConvo(bot, message) { | |
| return (err, convo) => {}; | |
| } | |
| ... | |
| bot.startPrivateConversation(message, privateConvo(bot, message)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| return (err, convo) => { | |
| 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(); | |
| }, | |
| }, { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function privateConvo(bot, message) { | |
| const { user, channel } = message; | |
| return (err, convo) => { | |
| if (err) throw err; | |
| convo.ask(...); | |
| // on end, update game data and broadcast info | |
| convo.on('end', callback); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| convo.on('end', (convo) => { | |
| if (convo.status === 'completed') { | |
| // conversation completed correctly | |
| const prc = convo.extractResponse('rockPaperScissors'); | |
| } else { | |
| // this happens if the conversation ended prematurely for some reason | |
| bot.reply(message, 'OK, nevermind!'); | |
| } | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |