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', }, */