From 48fcfc375c964d0af83ccc1444aa9d0e0674cf70 Mon Sep 17 00:00:00 2001 From: Caleb Fontenot Date: Thu, 6 Apr 2023 16:28:29 -0500 Subject: [PATCH] Implement madlibs --- commands/startMadlib.cjs | 42 +++++++++++++++++++--------------------- main.cjs | 6 ++++-- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/commands/startMadlib.cjs b/commands/startMadlib.cjs index 2be313e..180ba83 100644 --- a/commands/startMadlib.cjs +++ b/commands/startMadlib.cjs @@ -21,12 +21,27 @@ module.exports = { .setLabel('No') .setStyle(ButtonStyle.Danger), ); - const message = await interaction.reply({ content: "You have requested to start a madlib game in the current channel. I will intercept any message sent in this channel as game input until the game is over. Please refrain from using this in a busy channel, as it can be annoying for others. Would you like to continue?", fetchReply: true, components: [row], allowedMentions: { repliedUser: false, } }); + const message = await interaction.reply({ content: "You have requested to start a madlib game in the current channel. I will intercept any message sent in this channel as game input until the game is over. Please refrain from using this in a busy channel, as it can be annoying for others. Would you like to continue?", fetchReply: true, components: [row]}); client.on(Events.InteractionCreate, interaction => { if (!interaction.isButton()) return; if (interaction.customId == 'yes') { console.log("User selected yes"); if (interaction.channel.id != undefined) { + const row = new ActionRowBuilder() + .addComponents( + new StringSelectMenuBuilder() + .setCustomId('selectstory') + .setPlaceholder('Automatically pick a random story') + .setMaxValues(global.madlibState.numberOfStories) + .addOptions( + buildOptionJSON().toString() + ), + ); + interaction.reply({ content: 'Select a story', components: [row] }); + client.on(Events.InteractionCreate, interaction => { + if (!interaction.isStringSelectMenu()) return; + console.log(interaction); + }); initGame(interaction.channel.id, client, interaction); } } else if (interaction.customId == 'no') { @@ -47,43 +62,26 @@ function initGame(channelId, client, interaction) { console.log("Starting game in channel " + channelId); madlibState.gameChannel = channelId; //let selectedStory = - selectStory(interaction); //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."); } } -async function selectStory(interaction) { - const row = new ActionRowBuilder() - .addComponents( - new StringSelectMenuBuilder() - .setCustomId('selectStory') - .setPlaceholder('Automatically pIck a random story') - .addOptions( - buildOptionJSON() - ), - ); - await interaction.reply({ content: 'Select a story', components: [row] }); - client.on(Events.InteractionCreate, interaction => { - if (!interaction.isStringSelectMenu()) return; - console.log(interaction); - }); - } function buildOptionJSON() { const madlib = require("../madlibs/stories.json"); let returnObj = []; - for (let i = 0; i < Object.keys(madlib.stories).length; ++i) { + //Object.keys(madlib.stories).length + for (let i = 0; i < 1; ++i) { let entryObj = {}; entryObj["label"] = Object.keys(madlib.stories)[i]; entryObj["description"] = "Story " + (i+1); - entryObj["value"] = i; + entryObj["value"] = (i+1).toString(); returnObj.push(entryObj); } return returnObj; } -console.log(buildOptionJSON()); +//console.log(buildOptionJSON()); /* { diff --git a/main.cjs b/main.cjs index 4f2971d..e291ce5 100644 --- a/main.cjs +++ b/main.cjs @@ -5,9 +5,11 @@ const Discord = require('discord.js'); const { clientId, guildId, token } = require('./key.json'); let fs = require('node:fs'); let path = require('node:path'); -global.madlibState = { gameChannel: undefined, storyTitle:undefined, currentStory: undefined, storyIterate: 0, storyReplaceContent: [], storyLength: undefined}; +global.madlibState = { gameChannel: undefined, storyTitle:undefined, currentStory: undefined, storyIterate: 0, storyReplaceContent: [], storyLength: undefined, numberOfStories: undefined}; //export {madlibState}; - +//Get number of stories +const madlib = require("./madlibs/stories.json"); +global.madlibState.numberOfStories = Object.keys(madlib.stories).length; // Create a new client instance const client = new Discord.Client({