2021-09-17 18:28:39 +02:00
|
|
|
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",
|
2021-11-18 18:56:42 +01:00
|
|
|
run: async (client, interaction) => {
|
2021-09-17 18:28:39 +02:00
|
|
|
await interaction.deleteReply();
|
2021-11-18 18:56:42 +01:00
|
|
|
if (playing.has(interaction.channel.id)) {
|
2021-09-17 18:28:39 +02:00
|
|
|
return interaction.followUp({
|
|
|
|
content: "Only one game may be occurring per channel.",
|
|
|
|
});
|
2021-11-18 18:56:42 +01:00
|
|
|
}
|
2021-09-17 18:28:39 +02:00
|
|
|
playing.add(interaction.channel.id);
|
2021-06-12 12:53:51 +02:00
|
|
|
try {
|
2021-09-17 18:28:39 +02:00
|
|
|
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)
|
2021-11-18 18:56:42 +01:00
|
|
|
.setFooter(`Made by ${client.author}`, client.user.displayAvatarURL())
|
2021-09-17 18:28:39 +02:00
|
|
|
.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 ? "\\" : ""}
|
|
|
|
=============
|
|
|
|
\`\`\`
|
|
|
|
`);
|
2021-11-18 18:56:42 +01:00
|
|
|
await interaction.channel.send({ embeds: [embed] });
|
2021-06-12 12:53:51 +02:00
|
|
|
const filter = res => {
|
|
|
|
const choice = res.content.toLowerCase();
|
|
|
|
return (
|
2021-09-17 18:28:39 +02:00
|
|
|
res.author.id === interaction.user.id &&
|
2021-06-12 12:53:51 +02:00
|
|
|
!confirmation.includes(choice) &&
|
|
|
|
!incorrect.includes(choice)
|
|
|
|
);
|
|
|
|
};
|
2021-09-17 18:28:39 +02:00
|
|
|
const guess = await interaction.channel.awaitMessages({
|
|
|
|
filter,
|
2021-06-12 12:53:51 +02:00
|
|
|
max: 1,
|
|
|
|
time: 30000,
|
|
|
|
});
|
|
|
|
if (!guess.size) {
|
2021-09-17 18:28:39 +02:00
|
|
|
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++;
|
|
|
|
}
|
|
|
|
}
|
2021-09-17 18:28:39 +02:00
|
|
|
playing.delete(interaction.channel.id);
|
2021-11-18 18:56:42 +01:00
|
|
|
if (word.length === confirmation.length || guessed) {
|
2021-09-17 18:28:39 +02:00
|
|
|
return interaction.channel.send({
|
|
|
|
content: `You won. The word is **${word}**!`,
|
|
|
|
});
|
2021-11-18 18:56:42 +01:00
|
|
|
}
|
2021-09-17 18:28:39 +02:00
|
|
|
return interaction.channel.send({
|
|
|
|
content: `You lost. The word is **${word}**.`,
|
|
|
|
});
|
2021-06-12 12:53:51 +02:00
|
|
|
} catch (err) {
|
2021-09-17 18:28:39 +02:00
|
|
|
console.log(err);
|
|
|
|
playing.delete(interaction.channel.id);
|
2021-06-12 12:53:51 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|