2023-04-06 15:54:44 -05:00
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 ) ,
) ;
2023-04-06 16:28:29 -05:00
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 ] } ) ;
2023-04-06 15:54:44 -05:00
client . on ( Events . InteractionCreate , interaction => {
2023-04-06 20:19:39 -05:00
interaction . deferUpdate ( ) ;
2023-04-06 15:54:44 -05:00
if ( ! interaction . isButton ( ) ) return ;
if ( interaction . customId == 'yes' ) {
console . log ( "User selected yes" ) ;
if ( interaction . channel . id != undefined ) {
2023-04-06 16:28:29 -05:00
const row = new ActionRowBuilder ( )
. addComponents (
new StringSelectMenuBuilder ( )
. setCustomId ( 'selectstory' )
2023-04-06 18:02:20 -05:00
. setPlaceholder ( 'Select an option' )
2023-04-06 20:29:23 -05:00
. setMaxValues ( 1 )
2023-04-06 16:28:29 -05:00
. addOptions (
2023-04-06 18:02:20 -05:00
buildOptionJSON ( ) //.toString()
2023-04-06 16:28:29 -05:00
) ,
) ;
2023-04-06 18:02:20 -05:00
var selectedStory ;
const channel = client . channels . cache . get ( interaction . channel . id ) ;
channel . send ( { content : 'Select a story' , components : [ row ] , fetchMessage : true } ) ;
2023-04-06 16:28:29 -05:00
client . on ( Events . InteractionCreate , interaction => {
2023-04-07 10:49:44 -05:00
//interaction.deferUpdate();
2023-04-06 16:28:29 -05:00
if ( ! interaction . isStringSelectMenu ( ) ) return ;
2023-04-06 18:02:20 -05:00
selectedStory = interaction . values [ 0 ] ;
console . log ( "User selected: " + selectedStory ) ;
initGame ( interaction . channel . id , client , interaction , selectedStory ) ;
//storySelectMessage.delete();
2023-04-07 10:49:44 -05:00
//console.log(interaction);
//interaction.channel.send("Started game!");
2023-04-06 20:43:27 -05:00
//interaction.deleteReply();
2023-04-06 16:28:29 -05:00
} ) ;
2023-04-06 18:02:20 -05:00
2023-04-06 15:54:44 -05:00
}
} 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." ) ;
}
}
2023-04-06 20:19:39 -05:00
//message.delete();
2023-04-06 15:54:44 -05:00
} ) ;
console . log ( "User " + interaction . user . tag + " ran /startmadlib" ) ;
} ,
} ;
2023-04-06 18:02:20 -05:00
function initGame ( channelId , client , interaction , selectedStory ) {
2023-04-06 15:54:44 -05:00
if ( global . madlibState . gameChannel == undefined ) {
console . log ( "Starting game in channel " + channelId ) ;
2023-04-06 18:02:20 -05:00
madlibState . gameChannel = channelId ;
global . madlibNextPrompt ( client , 0 , selectedStory ) ;
2023-04-06 15:54:44 -05:00
} 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 = [ ] ;
2023-04-06 18:02:20 -05:00
{
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 ) {
2023-04-06 15:54:44 -05:00
let entryObj = { } ;
entryObj [ "label" ] = Object . keys ( madlib . stories ) [ i ] ;
entryObj [ "description" ] = "Story " + ( i + 1 ) ;
2023-04-06 16:28:29 -05:00
entryObj [ "value" ] = ( i + 1 ) . toString ( ) ;
2023-04-06 15:54:44 -05:00
returnObj . push ( entryObj ) ;
}
return returnObj ;
}
2023-04-06 16:28:29 -05:00
//console.log(buildOptionJSON());
2023-04-06 15:54:44 -05:00
/ *
{
label : 'Select me' ,
description : 'This is a description' ,
value : 'first_option' ,
} ,
* /