1

I am trying to create a simple /giveaway command and came up with this:

const { ActionRowBuilder, ModalBuilder, SlashCommandBuilder, TextInputBuilder, TextInputStyle } = require('discord.js');

module.exports = {
    data: new SlashCommandBuilder().setName('giveaway').setDescription("Starts a giveaway."),
    run: async ({ interaction }) => {
        try {
            console.log('Starting giveaway command');

            const modal = new ModalBuilder()
                .setCustomId('giveawayModal')
                .setTitle('Start a Giveaway');

            const prizeInput = new TextInputBuilder()
                .setCustomId('prizeInput')
                .setLabel("What is the prize for this giveaway?")
                .setStyle(TextInputStyle.Short);

            const durationInput = new TextInputBuilder()
                .setCustomId('durationInput')
                .setLabel("Giveaway duration (in minutes)")
                .setStyle(TextInputStyle.Short);

            const firstActionRow = new ActionRowBuilder().addComponents(prizeInput);
            const secondActionRow = new ActionRowBuilder().addComponents(durationInput);

            modal.addComponents(firstActionRow, secondActionRow);

            console.log('Showing modal to user');
           
            const modalData = await interaction.showModal(modal);
            console.log('Modal data:', modalData);

            if (modalData) {
                
                const prize = modalData.prizeInput;
                const duration = parseInt(modalData.durationInput) * 60; // Convert minutes to seconds

                const data = { prize, duration };
                console.log('Giveaway data:', data);

                console.log('Starting giveaway');
                const announcementMessage = await interaction.channel.send(`The giveaway for "${data.prize}" has started! React with  to enter. It will end in ${data.duration / 60} minutes.`);

                await announcementMessage.react('');

                await new Promise(resolve => setTimeout(resolve, data.duration * 1000));

                const reaction = announcementMessage.reactions.cache.get('');
                const users = await reaction.users.fetch();

                const participants = users.filter(user => !user.bot).array();
                console.log('Participants:', participants);
                
                const winner = participants[Math.floor(Math.random() * participants.length)];
                console.log('Winner:', winner);

                await interaction.channel.send(`Congratulations, ${winner}! You won the "${data.prize}" giveaway! `);
            } else {
                // Handle the case where modalData is undefined
            }
        } catch (error) {
            console.error(error);
        }
    },
};

I am not getting any console errors, only a "Something went wrong. Try again" error at the top of the modal.

I have tried updating to the latest versions of node.js, discord.js, etc. I tried switching around the code slightly, but its hard to fix something when I don't know what's wrong...

This is the only thing in my console log

Starting giveaway command
Showing modal to user
Modal data: undefined
delyrious
  • 11
  • 3

0 Answers0