Application command

Question: Hi, i need help with my code, when i click on the mod app it shows the embed with the buttons i need and then and then another with 2 buttons that i also and then last embed with 2 buttons but these 2 buttons the modal is not showing. Is my structure of my code wrong? If it is can i get help with it

Repl link:
https://replit.com/@thomaskaldas21/LCPDFR?v=1#index.js

const { EmbedBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle, ModalBuilder, TextInputBuilder, TextInputStyle } = require("discord.js");

module.exports = {
    name: "apply-embed",
    description: "makes an application embed",

    async execute(interaction, client) {
        try {
            const guild = interaction.guild;
            let userToBan, reason, additionalInfo, userId, baseCustomId, staffChannel;

            const initialEmbed = new EmbedBuilder()
                .setTitle(`${guild.name}`)
                .setDescription("**Application Desk** - BETA \n\n Staff Roles \n\n **Support Team:** Support members with LCPDFR and GTA IV problems. \n\n **Project Team:** Helps with mod projects. \n\n **Chat Moderator:** Deletes and reports messages that violate the rules. \n\n **__Special Roles__** \n\n **Server Partner:** Server owners who partner with our server. \n\n **Mod Developer:** Developers making mods for LCPDFR or GTA IV. \n\n **Content Creator:** Members creating content for LCPDFR or GTA IV. \n\n **Server Promoter:** Members who added the server link to about me. \n\n **__Moderation Applications__** \n\n  **Ban Application:** Start a ban or timeout process. \n\n **Unban Application:** Start an unban or ban appeal process. \n\n **Kick Application:** Start a kick process")
                .setFooter({
                    text: `Copyright Β© 2023 ${interaction.guild.name} All Rights Reserved`
                });

            const staffroles = new ButtonBuilder()
                .setCustomId('staffroles')
                .setLabel('Staff Roles')
                .setStyle(ButtonStyle.Success);

            const specialroles = new ButtonBuilder()
                .setCustomId('specialroles')
                .setLabel('Special Roles')
                .setStyle(ButtonStyle.Success);

            const modapplication = new ButtonBuilder()
                .setCustomId('modapp')
                .setLabel('Mod Application')
                .setStyle(ButtonStyle.Danger);

            const row = new ActionRowBuilder()
                .addComponents(staffroles, specialroles, modapplication);

            await interaction.reply({ embeds: [initialEmbed], components: [row] });

            const filter = i => i.user.id === interaction.user.id;
            const collector = interaction.channel.createMessageComponentCollector({ filter, time: 60000 });

            collector.on('collect', async i => {
                if (i.customId === 'staffroles') {
                    const guild = i.guild; // Assuming `i` is the interaction object
                    const staffRolesEmbed = new EmbedBuilder()
                        .setTitle("**Staff Roles**")
                        .setDescription(`**Requirements:** \n\n **Support Team** \n10 Days In Server \nNo Ads On Profile \nNot Staff On Another ${guild.name} \nLCPDFR and GTA IV Knowledge \n\n **Chat Moderator:** \n10 Days In Server \nNo Ads On Profile \nNot Staff On Another ${guild.name} \n Server Activity \n\n **Statuses** \nSupport Team: Open βœ…: \nChat Moderator: Open βœ…`)
                        .setColor("#00FFFF")
                        .setAuthor({ name: `${guild.name}` })

                    await i.reply({ embeds: [staffRolesEmbed], ephemeral: true });
                } else if (i.customId === 'specialroles') {
                    const guild = i.guild; // Assuming `i` is the interaction object
                    const specialRolesEmbed = new EmbedBuilder()
                        .setTitle("Special Roles")
                        .setDescription(`**Requirements:** \n\n **Server Partner** \n10 Days In Server \nGTA IV Modding Related \nMinimum 15 Members \nActive Community \n\n **Mod Developer:** \n10 Days In Server \nMod Site Page / Official Site \n100 Downloads \n\n **Server Promoter:** \n 10 days in Server \n https://discord.gg/4sE2hSe4XE On About Me \nNo Another Advertisement On Profile \n\n **Statuses** \nServer Partner: Open βœ…: \nMod Developer: Open βœ… \nServer Promoter: Open βœ…`)
                        .setColor("#FF00FF")
                        .setAuthor({ name: `${guild.name}` });
                    await i.reply({ embeds: [specialRolesEmbed], ephemeral: true });

                } else if (i.customId === 'unban') {
                    const unbanEmbed = new EmbedBuilder()
                        .setAuthor({ name: `${guild.name}` })
                        .setTitle("Unban Application")
                        .setDescription("**Unban Application:** Apply for an unban for your main account.\n\n**Ban Appeal:** Appeal a ban that you think is justified or you don't know the reason.")
                        .setColor("#FFFF00");

                    // Create buttons for the unban application
                    const applyUnbanButton = new ButtonBuilder()
                        .setCustomId('unbanapp')
                        .setLabel('Unban Application')
                        .setStyle(ButtonStyle.Success);

                    const appealBanButton = new ButtonBuilder()
                        .setCustomId('appealBan')
                        .setLabel('Appeal Ban')
                        .setStyle(ButtonStyle.Primary);

                    // Create an action row to hold the buttons
                    const unbanButtonRow = new ActionRowBuilder()
                        .addComponents(applyUnbanButton, appealBanButton);

                    await i.reply({ embeds: [unbanEmbed], components: [unbanButtonRow], ephemeral: true });
                } else if (i.customId === 'modapp') {
                    const modAppEmbed = new EmbedBuilder()
                        .setTitle("Moderation Application")
                        .setDescription("Choose an option below to proceed with the moderation application.")
                        .setColor("#FFFF00");

                    const banButton = new ButtonBuilder()
                        .setCustomId('ban')
                        .setLabel('Ban Application')
                        .setStyle(ButtonStyle.Danger);

                    const unbanButton = new ButtonBuilder()
                        .setCustomId('unban')
                        .setLabel('Unban Application')
                        .setStyle(ButtonStyle.Success);

                    const modAppRow = new ActionRowBuilder()
                        .addComponents(banButton, unbanButton);

                    await i.reply({ embeds: [modAppEmbed], components: [modAppRow], ephemeral: true });

                } else if (i.customId === 'ban' || i.customId === 'unban') {
                    const modal = new ModalBuilder()
                        .setCustomId(`${i.customId}ApplicationModal`)
                        .setTitle(`${i.customId.charAt(0).toUpperCase() + i.customId.slice(1)} Application`);

                    const userIdInput = new TextInputBuilder()
                        .setCustomId('userId')
                        .setLabel("User ID")
                        .setStyle(TextInputStyle.Short)
                        .setPlaceholder("Enter the user ID here")
                        .setRequired(true);

                    const reasonInput = new TextInputBuilder()
                        .setCustomId(`${i.customId}Reason`)
                        .setLabel("Reason")
                        .setStyle(TextInputStyle.Paragraph)
                        .setPlaceholder(`Enter the reason for the ${i.customId} here`)
                        .setRequired(true);

                    const messageInput = new TextInputBuilder()
                        .setCustomId(`messagelink`)
                        .setLabel("Message Link")
                        .setStyle(TextInputStyle.Paragraph)
                        .setPlaceholder(`If the reason for the ban is a message, enter the link`)
                        .setRequired(true);

                    const additionalInput = new TextInputBuilder()
                        .setCustomId(`additioninfo`)
                        .setLabel("Additional Info")
                        .setStyle(TextInputStyle.Paragraph)
                        .setPlaceholder(`Add any additional info that would help us with the process of ban`)
                        .setRequired(false);

                    const attachmentInput = new TextInputBuilder()
                        .setCustomId(`attachments`)
                        .setLabel("Attachment")
                        .setStyle(TextInputStyle.Paragraph)
                        .setPlaceholder(`Enter the reason for the ${i.customId} here`)
                        .setRequired(true);

                    const actionRow1 = new ActionRowBuilder().addComponents(userIdInput);
                    const actionRow2 = new ActionRowBuilder().addComponents(reasonInput);
                    const actionRow3 = new ActionRowBuilder().addComponents(messageInput);
                    const actionRow4 = new ActionRowBuilder().addComponents(additionalInput);
                    const actionRow5 = new ActionRowBuilder().addComponents(attachmentInput);

                    modal.addComponents(actionRow1, actionRow2, actionRow3, actionRow4, actionRow5);

                    await i.showModal(modal);
                }
            });

            client.on('interactionCreate', async interaction => {
                if (interaction.isModalSubmit()) {
                    baseCustomId = interaction.customId.replace('ApplicationModal', '');

                    if (baseCustomId === 'ban' || baseCustomId === 'unban') {
                        userId = interaction.fields.getTextInputValue('userId');
                        if (!/^\d+$/.test(userId)) {
                            await interaction.reply({ content: `The provided User ID "${userId}" is not valid. Please ensure you're entering a numeric ID.`, ephemeral: true });
                            return;
                        }
                        userToBan = await client.users.fetch(userId);

                        reason = interaction.fields.getTextInputValue(`${baseCustomId}Reason`);
                        additionalInfo = interaction.fields.getTextInputValue('additioninfo');

                        const approveButton = new ButtonBuilder()
                            .setCustomId('approveUnban')
                            .setLabel('Approve')
                            .setStyle(ButtonStyle.Success);

                        const denyButton = new ButtonBuilder()
                            .setCustomId('denyUnban')
                            .setLabel('Deny')
                            .setStyle(ButtonStyle.Danger);

                        const buttonRow = new ActionRowBuilder()
                            .addComponents(approveButton, denyButton);

                        staffChannel = client.channels.cache.get(baseCustomId === 'ban' ? '1227919929215418408' : '1227931262853845032');

                        if (!staffChannel) {
                            console.error(`Staff channel for ${baseCustomId} requests not found.`);
                            await interaction.reply({ content: `An error occurred while submitting your ${baseCustomId} request.`, ephemeral: true });
                            return;
                        }

                        const requestEmbed = new EmbedBuilder()
                            .setTitle(`${baseCustomId.charAt(0).toUpperCase() + baseCustomId.slice(1)} Request`)
                            .setDescription(`User ID: ${userId}\nReason: ${reason}\nAdditional Info: ${additionalInfo}`)
                            .setColor('#FFFF00');

                        await staffChannel.send({ embeds: [requestEmbed], components: [buttonRow] });
                        await interaction.reply({ content: `Your ${baseCustomId} request has been submitted for review.`, ephemeral: true });

                        client.on('interactionCreate', async interaction => {
                            try {
                                if (interaction.isButton()) {
                                    let modal;
                        
                                    if (interaction.customId === 'unbanapp') {
                                        // Create a modal for the Unban Application
                                        modal = new ModalBuilder()
                                            .setCustomId('unbanApplicationModal')
                                            .setTitle('Unban Application');
                        
                                        // Create the text input components
                                        const userIdInput = new TextInputBuilder()
                                            .setCustomId('userId')
                                            .setLabel("User ID")
                                            .setStyle(TextInputStyle.Short)
                                            .setPlaceholder("Enter the user ID here")
                                            .setRequired(true);
                        
                                        const reasonInput = new TextInputBuilder()
                                            .setCustomId('unbanReason')
                                            .setLabel("Reason for Unban")
                                            .setStyle(TextInputStyle.Paragraph)
                                            .setPlaceholder("Enter the reason for your unban request")
                                            .setRequired(true);
                        
                                        // Add inputs to the modal
                                        const firstActionRow = new ActionRowBuilder().addComponents(userIdInput);
                                        const secondActionRow = new ActionRowBuilder().addComponents(reasonInput);
                        
                                        modal.addComponents(firstActionRow, secondActionRow);
                        
                                    } else if (interaction.customId === 'appealBan') {
                                        // Create a modal for the Ban Appeal
                                        modal = new ModalBuilder()
                                            .setCustomId('banAppealModal')
                                            .setTitle('Ban Appeal');
                        
                                        // Create the text input components
                                        const userIdInput = new TextInputBuilder()
                                            .setCustomId('userIdAppeal')
                                            .setLabel("User ID")
                                            .setStyle(TextInputStyle.Short)
                                            .setPlaceholder("Enter the user ID here")
                                            .setRequired(true);
                        
                                        const appealReasonInput = new TextInputBuilder()
                                            .setCustomId('appealReason')
                                            .setLabel("Reason for Appeal")
                                            .setStyle(TextInputStyle.Paragraph)
                                            .setPlaceholder("Enter the reason for your ban appeal")
                                            .setRequired(true);
                        
                                        // Add inputs to the modal
                                        const firstActionRow = new ActionRowBuilder().addComponents(userIdInput);
                                        const secondActionRow = new ActionRowBuilder().addComponents(appealReasonInput);
                        
                                        modal.addComponents(firstActionRow, secondActionRow);
                                    }
                        
                                    // Show the modal to the user
                                    if (modal) await interaction.showModal(modal);
                                }
                            } catch (error) {
                                console.error(error);
                            }
                        });
                    }
                }
            })
        } catch (error) {
            console.error(error);
        }
    } 
}                        

1 Like

Your code sets up a Discord bot command to handle applications with interactive embeds and buttons.

Key points for improvement include:

β€’ defining variables closer to their use, reusing existing guild variable instead of redeclaring it,

β€’ enhancing error handling particularly around network requests, and restructuring nested event listeners to prevent potential memory leaks and ensure clean management of interactions.

Errors:
β€’ Unused variables
β€’ Duplicate Guild Declaration
β€’ Error handling - around network requests
β€’ Potential Memory leak - careful with event listeners inside other event handlers. Should restructure somewhere else in the project to avoid deep nesting.

β€’ Component ID - make sure it’s consistent.