|
|
@@ -0,0 +1,72 @@ |
|
|
"use strict"; |
|
|
|
|
|
const words = require("./words.json"); |
|
|
const faces = [":grin:", ":smile:", ":grinning:", ":slight_smile:", ":neutral_face:", ":slight_frown:", ":worried:", ":persevere:", ":tired_face:", ":dizzy_face:", ":skull:"]; |
|
|
|
|
|
module.exports = class Hangman |
|
|
{ |
|
|
constructor() |
|
|
{ |
|
|
this.guesses = new Set(); |
|
|
this.word = words[Math.floor(Math.random() * words.length)]; |
|
|
} |
|
|
|
|
|
async start(channel) |
|
|
{ |
|
|
this.message = await channel.send(this.build()); |
|
|
this.collector = this.message.createReactionCollector(() => true, { time: 1800000 }); |
|
|
this.collector.on("collect", this.react.bind(this)); |
|
|
} |
|
|
|
|
|
react(reaction) |
|
|
{ |
|
|
const guess = this.latinify(reaction.emoji.name); |
|
|
if (guess !== undefined) |
|
|
{ |
|
|
for (const user of reaction.users.keys()) |
|
|
reaction.remove(user); |
|
|
|
|
|
if (!this.guesses.has(guess)) |
|
|
{ |
|
|
this.guesses.add(guess); |
|
|
this.message.edit(this.build()); |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
build() |
|
|
{ |
|
|
let revealedWord = this.word.replace(/[a-z]/g, m => this.guesses.has(m) ? m : "-"); |
|
|
const misses = [...this.guesses].filter(g => !this.word.includes(g)); |
|
|
let face = misses.length; |
|
|
|
|
|
if (!revealedWord.includes("-")) |
|
|
{ |
|
|
this.collector.stop(); |
|
|
face = 0; |
|
|
} |
|
|
if (misses.length >= faces.length - 1) |
|
|
{ |
|
|
this.collector.stop(); |
|
|
revealedWord = this.word; |
|
|
} |
|
|
|
|
|
return `${faces[face]} ${this.emojify(revealedWord)} |
|
|
|
|
|
:x: ${this.emojify(misses.join(""))}`; |
|
|
} |
|
|
|
|
|
emojify(word) |
|
|
{ |
|
|
return word.replace(/[a-z]/g, ":regional_indicator_$&:").replace(/-/g, ":white_large_square:"); |
|
|
} |
|
|
|
|
|
latinify(emoji) |
|
|
{ |
|
|
const index = "๐ฆ๐ง๐จ๐ฉ๐ช๐ซ๐ฌ๐ญ๐ฎ๐ฏ๐ฐ๐ฑ๐ฒ๐ณ๐ด๐ต๐ถ๐ท๐ธ๐น๐บ๐ป๐ผ๐ฝ๐พ๐ฟ".indexOf(emoji) / 2; |
|
|
if (index < 0) |
|
|
return undefined; |
|
|
else |
|
|
return String.fromCharCode(97 + index); |
|
|
} |
|
|
}; |