nyx/command/Fun/hangman.js

100 lines
3.3 KiB
JavaScript
Raw Normal View History

const axios = require("axios");
const { MessageEmbed } = require("discord.js");
2021-06-12 12:53:51 +02:00
const playing = new Set();
module.exports = {
name: "hangman",
description: "Play a hangman game",
category: "Fun",
run: async (client, interaction, args) => {
await interaction.deleteReply();
if (playing.has(interaction.channel.id))
return interaction.followUp({
content: "Only one game may be occurring per channel.",
});
playing.add(interaction.channel.id);
2021-06-12 12:53:51 +02:00
try {
const data = await axios
.get(`${process.env.api}/api/v1/fun/hangman`)
.then(res => res.data);
const word = data.word;
2021-06-12 12:53:51 +02:00
let points = 0;
let displayText = null;
let guessed = false;
const confirmation = [];
const incorrect = [];
const display = new Array(word.length).fill("◯");
while (word.length !== confirmation.length && points < 6) {
const embed = new MessageEmbed()
.setColor(client.color)
.setFooter(
interaction.user.tag,
interaction.user.displayAvatarURL({ dynamic: true })
)
.setTimestamp()
.setTitle("Hangman game").setDescription(`
2021-06-12 12:53:51 +02:00
${displayText === null ? "Here we go!" : displayText ? "Good job!" : "Nope!"}
\`${display.join(" ")}\`. Which letter do you choose?
Incorrect Tries: ${incorrect.join(", ") || "None"}
\`\`\`
.
.
. ${points > 0 ? "O" : ""}
. ${points > 2 ? "/" : " "}${points > 1 ? "|" : ""}${
points > 3 ? "\\" : ""
}
. ${points > 4 ? "/" : ""}${points > 5 ? "\\" : ""}
=============
\`\`\`
`);
let m = await interaction.channel.send({ embeds: [embed] });
2021-06-12 12:53:51 +02:00
const filter = res => {
const choice = res.content.toLowerCase();
return (
res.author.id === interaction.user.id &&
2021-06-12 12:53:51 +02:00
!confirmation.includes(choice) &&
!incorrect.includes(choice)
);
};
const guess = await interaction.channel.awaitMessages({
filter,
2021-06-12 12:53:51 +02:00
max: 1,
time: 30000,
});
//m.delete();
if (!guess.size) {
await interaction.channel.send({ content: "Sorry, time is up!" });
2021-06-12 12:53:51 +02:00
break;
}
const choice = guess.first().content.toLowerCase();
if (choice === "end") break;
if (choice.length > 1 && choice === word) {
guessed = true;
break;
} else if (word.includes(choice)) {
displayText = true;
for (let i = 0; i < word.length; i++) {
if (word.charAt(i) !== choice) continue; // eslint-disable-line max-depth
confirmation.push(word.charAt(i));
display[i] = word.charAt(i);
}
} else {
displayText = false;
if (choice.length === 1) incorrect.push(choice);
points++;
}
}
playing.delete(interaction.channel.id);
2021-06-12 12:53:51 +02:00
if (word.length === confirmation.length || guessed)
return interaction.channel.send({
content: `You won. The word is **${word}**!`,
});
return interaction.channel.send({
content: `You lost. The word is **${word}**.`,
});
2021-06-12 12:53:51 +02:00
} catch (err) {
console.log(err);
playing.delete(interaction.channel.id);
2021-06-12 12:53:51 +02:00
}
},
};