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 => {
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' )
. setPlaceholder ( 'Automatically pick a random story' )
. setMaxValues ( global . madlibState . numberOfStories )
. addOptions (
buildOptionJSON ( ) . toString ( )
) ,
) ;
interaction . reply ( { content : 'Select a story' , components : [ row ] } ) ;
client . on ( Events . InteractionCreate , interaction => {
if ( ! interaction . isStringSelectMenu ( ) ) return ;
console . log ( interaction ) ;
} ) ;
2023-04-06 15:54:44 -05:00
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 =
//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 = [ ] ;
2023-04-06 16:28:29 -05:00
//Object.keys(madlib.stories).length
for ( let i = 0 ; i < 1 ; ++ 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' ,
} ,
* /