107 lines
3.8 KiB
JavaScript
107 lines
3.8 KiB
JavaScript
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]});
|
|
client.on(Events.InteractionCreate, interaction => {
|
|
interaction.deferUpdate();
|
|
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('Select an option')
|
|
.setMaxValues(1)
|
|
.addOptions(
|
|
buildOptionJSON()//.toString()
|
|
),
|
|
);
|
|
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 => {
|
|
//interaction.deferUpdate();
|
|
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);
|
|
//interaction.channel.send("Started game!");
|
|
//interaction.deleteReply();
|
|
});
|
|
|
|
}
|
|
} 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, selectedStory) {
|
|
if(global.madlibState.gameChannel == undefined) {
|
|
console.log("Starting game in channel " + channelId);
|
|
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.");
|
|
}
|
|
}
|
|
function buildOptionJSON() {
|
|
const madlib = require("../madlibs/stories.json");
|
|
let returnObj = [];
|
|
{
|
|
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);
|
|
entryObj["value"] = (i+1).toString();
|
|
returnObj.push(entryObj);
|
|
}
|
|
return returnObj;
|
|
}
|
|
//console.log(buildOptionJSON());
|
|
|
|
/*
|
|
{
|
|
label: 'Select me',
|
|
description: 'This is a description',
|
|
value: 'first_option',
|
|
},
|
|
*/ |