From 6fa8b302523d8768bd672fc38dcad5edd0cfdd00 Mon Sep 17 00:00:00 2001 From: Daniel Odendahl Jr Date: Wed, 14 Jun 2017 00:36:41 +0000 Subject: [PATCH] Hangman --- commands/games/hangman.js | 56 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 commands/games/hangman.js diff --git a/commands/games/hangman.js b/commands/games/hangman.js new file mode 100644 index 00000000..9e69eb11 --- /dev/null +++ b/commands/games/hangman.js @@ -0,0 +1,56 @@ +const Command = require('../../structures/Command'); +const { stripIndents } = require('common-tags'); +const words = require('../../assets/json/hangman'); + +module.exports = class HangmanCommand extends Command { + constructor(client) { + super(client, { + name: 'hangman', + group: 'games', + memberName: 'hangman', + description: 'Play a game of hangman.' + }); + } + + async run(msg) { + const word = words[Math.floor(Math.random() * words.length)]; + let points = 0; + const confirmation = []; + const display = '_'.repeat(word.length).split(''); + while (word.length !== confirmation.length || points > 6) { + await msg.code(null, stripIndents` + ___________ + | | + | ${points > 0 ? 'O' : ''} + | ${points > 2 ? '—' : ''}${points > 1 ? '|' : ''}${points > 3 ? '—' : ''} + | ${points > 4 ? '/' : ''} ${points > 5 ? '\\' : ''} + =========== + The word is: ${display.join(' ')}. Which letter do you choose? + `); + const guess = await msg.channel.awaitMessages((res) => res.author.id === msg.author.id, { + max: 1, + time: 30000 + }); + if (!guess.size) { + await msg.say('Time!'); + break; + } + const choice = guess.first().content.toLowerCase(); + if (confirmation.includes(choice)) { + await msg.say('You have already picked that letter!'); + } else if (word.includes(choice)) { + await msg.say('Nice job!'); + confirmation.push(choice); + for (const letter of word.split('')) { + if (letter !== choice) continue; + else display[word.indexOf(letter)] = choice; + } + } else { + await msg.say('Nope!'); + points++; + } + } + if (word.length === confirmation.length) return msg.say('You won!'); + else return msg.say('Too bad...'); + } +};