From 42a42f259430c501f02d642daba9112a2454e40d Mon Sep 17 00:00:00 2001 From: Caleb Fontenot Date: Thu, 6 Apr 2023 18:02:20 -0500 Subject: [PATCH] Implement madlibs --- commands/startMadlib.cjs | 31 +++++++++++++++++++++---------- main.cjs | 17 +++++++++++++---- 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/commands/startMadlib.cjs b/commands/startMadlib.cjs index 180ba83..b6873e4 100644 --- a/commands/startMadlib.cjs +++ b/commands/startMadlib.cjs @@ -31,18 +31,24 @@ module.exports = { .addComponents( new StringSelectMenuBuilder() .setCustomId('selectstory') - .setPlaceholder('Automatically pick a random story') + .setPlaceholder('Select an option') .setMaxValues(global.madlibState.numberOfStories) .addOptions( - buildOptionJSON().toString() + buildOptionJSON()//.toString() ), ); - interaction.reply({ content: 'Select a story', components: [row] }); + var selectedStory; + const channel = client.channels.cache.get(interaction.channel.id); + channel.send({ content: 'Select a story', components: [row], fetchMessage: true }); client.on(Events.InteractionCreate, interaction => { if (!interaction.isStringSelectMenu()) return; + selectedStory = interaction.values[0]; + console.log("User selected: " +selectedStory); + initGame(interaction.channel.id, client, interaction, selectedStory); + //storySelectMessage.delete(); console.log(interaction); }); - initGame(interaction.channel.id, client, interaction); + } } else if (interaction.customId == 'no') { console.log("User selected no"); @@ -57,12 +63,11 @@ module.exports = { console.log("User " + interaction.user.tag + " ran /startmadlib"); }, }; -function initGame(channelId, client, interaction) { +function initGame(channelId, client, interaction, selectedStory) { if(global.madlibState.gameChannel == undefined) { console.log("Starting game in channel " + channelId); - madlibState.gameChannel = channelId; - //let selectedStory = - //global.madlibNextPrompt(client, 0, selectedStory); + madlibState.gameChannel = channelId; + global.madlibNextPrompt(client, 0, selectedStory); } else { const channel = client.channels.cache.get(interaction.channel.id); channel.send("There is currently an active game! Wait for it to be finished before starting a new one."); @@ -71,8 +76,14 @@ function initGame(channelId, client, interaction) { function buildOptionJSON() { const madlib = require("../madlibs/stories.json"); let returnObj = []; - //Object.keys(madlib.stories).length - for (let i = 0; i < 1; ++i) { + { + let entryObj = {}; + entryObj["label"] = "Automatically select random story"; + entryObj["description"] = "Math.random()" + entryObj["value"] = "0" + returnObj.push(entryObj); + } + for (let i = 0; i < Object.keys(madlib.stories).length; ++i) { let entryObj = {}; entryObj["label"] = Object.keys(madlib.stories)[i]; entryObj["description"] = "Story " + (i+1); diff --git a/main.cjs b/main.cjs index e291ce5..c0f5cd5 100644 --- a/main.cjs +++ b/main.cjs @@ -234,11 +234,13 @@ function startMadlib(selectedStory) { const madlib = require("./madlibs/stories.json"); console.log("Madlib story count: " + Object.keys(madlib.stories).length); let storyCount = Object.keys(madlib.stories).length; - if (selectedStory == undefined) { + let storyTitle; + console.log(selectedStory); + if (selectedStory == 0) { //Pick random story: - let storyTitle = Object.keys(madlib.stories)[Math.trunc(Math.random() * storyCount)]; + storyTitle = Object.keys(madlib.stories)[Math.trunc(Math.random() * storyCount)]; } else { - let storyTitle = Object.keys(madlib.stories)[selectedStory]; + storyTitle = Object.keys(madlib.stories)[selectedStory - 1]; } console.log("Current story: " + storyTitle); @@ -269,7 +271,14 @@ global.madlibNextPrompt = function madlibNextPrompt(client, iteration, selectedS console.log(phrase); // Send a message in the gameChannel with the next prompt. const channel = client.channels.cache.get(madlibState.gameChannel); - channel.send("Give me a(n) " + phrase[iteration]); + let aAn; + let verbArray = ["a", "e", "i", "o", "u"]; + if (phrase[iteration].charAt(0).includes(verbArray)) { + aAn = "an "; + } else { + aAn = "a "; + } + channel.send("Give me " + aAn + phrase[iteration] + ":\n(" + (phrase.length - iteration) + " words remain)"); } /* * This function is executed when a player is sending a new message.