From 73766498951a575c5d56bb19d6c13127c5e95c6c Mon Sep 17 00:00:00 2001 From: Ayden Jahola Date: Tue, 24 Sep 2024 12:25:21 +0100 Subject: [PATCH] trivia: add option for a random trivia --- commands/games/trivia.js | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/commands/games/trivia.js b/commands/games/trivia.js index 217b324..6c0881c 100644 --- a/commands/games/trivia.js +++ b/commands/games/trivia.js @@ -276,9 +276,10 @@ module.exports = { .addStringOption((option) => option .setName("category") - .setDescription("Choose a trivia category") + .setDescription("Choose a trivia category or random") .setRequired(true) .addChoices( + { name: "Random", value: "random" }, ...Object.entries(CATEGORY_MAP).map(([value, name]) => ({ name, value, @@ -303,8 +304,19 @@ module.exports = { ACTIVE_GAMES.add(userId); try { - const categoryId = interaction.options.getString("category"); - const categoryName = CATEGORY_MAP[categoryId] || "Video Games"; + let categoryId = interaction.options.getString("category"); + let categoryName; + + if (categoryId === "random") { + // Choose a random category from CATEGORY_MAP + const categoryKeys = Object.keys(CATEGORY_MAP); + const randomKey = + categoryKeys[Math.floor(Math.random() * categoryKeys.length)]; + categoryId = randomKey; // This is now valid + categoryName = CATEGORY_MAP[randomKey]; + } else { + categoryName = CATEGORY_MAP[categoryId] || "Video Games"; + } const { triviaQuestion, source } = await fetchTriviaQuestion( userId,