Implement madlibs

master
Chloe Fontenot 🏳️‍⚧️ 2023-04-06 18:02:20 +07:00
parent 48fcfc375c
commit 42a42f2594
2 changed files with 34 additions and 14 deletions

@ -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);
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);

@ -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.