games: add source and few changes to scramble

This commit is contained in:
Ayden Jahola 2024-09-08 20:41:23 +01:00
parent c298a22257
commit 916f150a5f
No known key found for this signature in database
GPG key ID: 71DD90AE4AE92742

View file

@ -23,7 +23,7 @@ const scrambleWord = (word) => {
.join(""); .join("");
}; };
const createScrambleEmbed = (scrambledWord, guild, timeLimit) => { const createScrambleEmbed = (scrambledWord, guild, timeLimit, source) => {
return new EmbedBuilder() return new EmbedBuilder()
.setColor("#0099ff") .setColor("#0099ff")
.setTitle("Word Scramble") .setTitle("Word Scramble")
@ -32,7 +32,9 @@ const createScrambleEmbed = (scrambledWord, guild, timeLimit) => {
) )
.setTimestamp() .setTimestamp()
.setFooter({ .setFooter({
text: `${guild.name} | Answer within ${timeLimit / 1000} seconds`, text: `${guild.name} | Answer within ${
timeLimit / 1000
} seconds | Source: ${source}`,
iconURL: guild.iconURL(), iconURL: guild.iconURL(),
}); });
}; };
@ -79,6 +81,23 @@ const handleScrambleAnswer = async (
} }
}; };
const getUniqueWord = async () => {
let originalWord;
let source = "API";
while (true) {
originalWord = await fetchRandomWord();
const existingWord = await ScrambledWord.findOne({ word: originalWord });
if (!existingWord) {
// Word is unique, break the loop
break;
}
}
return { originalWord, source };
};
module.exports = { module.exports = {
data: new SlashCommandBuilder() data: new SlashCommandBuilder()
.setName("scramble") .setName("scramble")
@ -100,7 +119,7 @@ module.exports = {
ACTIVE_GAMES.add(userId); ACTIVE_GAMES.add(userId);
try { try {
const originalWord = await fetchRandomWord(); const { originalWord, source } = await getUniqueWord();
const scrambledWord = scrambleWord(originalWord); const scrambledWord = scrambleWord(originalWord);
// Save to database // Save to database
@ -113,7 +132,8 @@ module.exports = {
const scrambleEmbed = createScrambleEmbed( const scrambleEmbed = createScrambleEmbed(
scrambledWord, scrambledWord,
guild, guild,
timeLimit timeLimit,
source
); );
await interaction.reply({ embeds: [scrambleEmbed] }); await interaction.reply({ embeds: [scrambleEmbed] });