Skip to content

Instantly share code, notes, and snippets.

@rlemon
Forked from kendfrey/hangman.js
Created September 6, 2019 14:33
Show Gist options
  • Select an option

  • Save rlemon/9f9e6d2c18cc0790af428dac4b0d642e to your computer and use it in GitHub Desktop.

Select an option

Save rlemon/9f9e6d2c18cc0790af428dac4b0d642e to your computer and use it in GitHub Desktop.

Revisions

  1. @kendfrey kendfrey created this gist Sep 6, 2019.
    72 changes: 72 additions & 0 deletions hangman.js
    Original file line number Diff line number Diff line change
    @@ -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);
    }
    };
    17 changes: 17 additions & 0 deletions main.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    "use strict";

    (async function ()
    {
    const Discord = require("discord.js");
    const Hangman = require("./hangman.js");
    const config = require("./config.json");

    const discord = new Discord.Client();
    await discord.login(config.token);

    discord.on("message", m =>
    {
    if (m.isMentioned(discord.user))
    new Hangman().start(m.channel);
    });
    })();
    3,230 changes: 3,230 additions & 0 deletions words.json
    3,230 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.