From 91784d7d7dd2dbd7441420a4ea3420914a925d5b Mon Sep 17 00:00:00 2001 From: Caleb Fontenot Date: Thu, 6 Apr 2023 15:54:44 -0500 Subject: [PATCH] Implement madlibs --- commands/{drawTriangle.cjs => drawDesign.cjs} | 0 commands/startMadlib.cjs | 94 ++++++++++++++++++ deploy-commands.js | 2 +- madlibs/stories.json | 10 ++ main.cjs | 97 +++++++++++++++++-- nbproject/project.properties | 3 - nbproject/project.xml | 9 -- package.json | 1 + 8 files changed, 197 insertions(+), 19 deletions(-) rename commands/{drawTriangle.cjs => drawDesign.cjs} (100%) create mode 100644 commands/startMadlib.cjs create mode 100644 madlibs/stories.json delete mode 100644 nbproject/project.properties delete mode 100644 nbproject/project.xml diff --git a/commands/drawTriangle.cjs b/commands/drawDesign.cjs similarity index 100% rename from commands/drawTriangle.cjs rename to commands/drawDesign.cjs diff --git a/commands/startMadlib.cjs b/commands/startMadlib.cjs new file mode 100644 index 0000000..2be313e --- /dev/null +++ b/commands/startMadlib.cjs @@ -0,0 +1,94 @@ +const { SlashCommandBuilder, Discord, ActionRowBuilder, ButtonBuilder, ButtonStyle, Events, StringSelectMenuBuilder } = require('discord.js'); +//const { madlibState } = require('../main.cjs'); +//exports.madlibState = madlibState; +//let gameChannel = madlibState; + +module.exports = { + data: new SlashCommandBuilder() + .setName('startmadlib') + .setDescription('Starts a madlib game in the current channel.'), + async execute(interaction, client) { + const row = new ActionRowBuilder() + .addComponents( + new ButtonBuilder() + .setCustomId('yes') + .setLabel('Yes') + .setStyle(ButtonStyle.Primary), + ) + .addComponents( + new ButtonBuilder() + .setCustomId('no') + .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, } }); + client.on(Events.InteractionCreate, interaction => { + if (!interaction.isButton()) return; + if (interaction.customId == 'yes') { + console.log("User selected yes"); + if (interaction.channel.id != undefined) { + initGame(interaction.channel.id, client, interaction); + } + } else if (interaction.customId == 'no') { + console.log("User selected no"); + if (interaction.channel.id != undefined) { + const channel = client.channels.cache.get(interaction.channel.id); + channel.send("Game aborted."); + } + } + message.delete(); + }); + + console.log("User " + interaction.user.tag + " ran /startmadlib"); + }, +}; +function initGame(channelId, client, interaction) { + if(global.madlibState.gameChannel == undefined) { + 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) { + let entryObj = {}; + entryObj["label"] = Object.keys(madlib.stories)[i]; + entryObj["description"] = "Story " + (i+1); + entryObj["value"] = i; + returnObj.push(entryObj); + } + return returnObj; +} +console.log(buildOptionJSON()); + +/* + { + label: 'Select me', + description: 'This is a description', + value: 'first_option', + }, +*/ \ No newline at end of file diff --git a/deploy-commands.js b/deploy-commands.js index 079528f..003d4ff 100644 --- a/deploy-commands.js +++ b/deploy-commands.js @@ -8,7 +8,7 @@ const path = require('node:path'); const commands = []; // Grab all the command files from the commands directory you created earlier const commandsPath = path.join(__dirname, 'commands'); -const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.ts')); +const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.cjs')); // Grab the SlashCommandBuilder#toJSON() output of each command's data for deployment for (const file of commandFiles) { diff --git a/madlibs/stories.json b/madlibs/stories.json new file mode 100644 index 0000000..a9d585d --- /dev/null +++ b/madlibs/stories.json @@ -0,0 +1,10 @@ +{ + "stories": { + "MadLib Gameshow": " on down! You're the next contestant on 's new gameshow, MadLib! Here's how it works. You will over a at a rate of mph. After you jump over that item, you will over to the and press the at the end. time wins! Oh. I forgot to introduce our host. He's a person who can't wait to in to the show. Please welcome, ! Alright players, on your mark. Get set. ! Alright, here goes on the first stage of the competition. And.. WOAH! He just over the thing! That's gonna hurt him tommorow. But it'll be all worth it after he wins the grand prize of dollars! Which he says will go towards . Very investment, but it's his money! Here he comes around the second leg. AND WOAH, AGAIN HE LOOKS ON NETWORK TELEVISION! This is what primetime is all about here on the network of . And, he presses it! He finishes with a time of