javascript - Am I wrongly handling deferral in this Discord.js slash command? - Stack Overflow

This is a Discord.js command to send embeds to a specific text channel. I'm struggling a little bi

This is a Discord.js command to send embeds to a specific text channel. I'm struggling a little bit with the replies/updates deferral.

When the modal is submitted, the command raises an Interaction has already been acknowledged. error but just sometimes, and I'm not really sure where I'm doing things wrong.

As far as I know, the .showModal method must be called as a first interaction reply since the modal opening cannot be delayed, and .deferUpdate is used to let the server know that you're not closing the interaction yet.

/** Controller for the Messages command. */
export default async function (interaction: ChatInputCommandInteraction) {
    try {
        // Modal fields data
        const fields: {
            id: string
            label: string
            style: TextInputStyle
            required: boolean
        }[] = [
            {
                id: 'title',
                label: 'Título',
                style: TextInputStyle.Short,
                required: false,
            },
            { id: 'url', label: 'URL', style: TextInputStyle.Short, required: false },
            {
                id: 'thumbnail',
                label: 'URL de la miniatura',
                style: TextInputStyle.Short,
                required: false,
            },
            {
                id: 'description',
                label: 'Descripción',
                style: TextInputStyle.Paragraph,
                required: true,
            },
            {
                id: 'image',
                label: 'URL de la Imagen',
                style: TextInputStyle.Short,
                required: false,
            },
        ]

        // Modal instance
        const modal = new ModalBuilder({
            customId: 'new-message-modal',
            title: 'Nuevo mensaje',
            components: fields.map(({ id, label, style, required }) =>
                new ActionRowBuilder<TextInputBuilder>().addComponents(
                    new TextInputBuilder()
                        .setCustomId(id)
                        .setLabel(label)
                        .setStyle(style)
                        .setRequired(required),
                ),
            ),
        })

        await interaction.showModal(modal)
        // THIS IS WHERE THE ERROR IS BEING RISEN
        // We wait for the user to press "submit" or "cancel"
        const modalInteraction = await interaction.awaitModalSubmit({
            filter: (i: ModalSubmitInteraction) =>
                i.customId === modal.data.custom_id && i.user.equals(interaction.user),
            time: 60_000,
        })

        await modalInteraction.deferUpdate()

        // Extract values from the modal
        const embedData = {
            title: modalInteraction.fields.getTextInputValue('title') || null,
            url: modalInteraction.fields.getTextInputValue('url') || null,
            description: modalInteraction.fields.getTextInputValue('description'),
            thumbnail: modalInteraction.fields.getTextInputValue('thumbnail') || null,
            image: modalInteraction.fields.getTextInputValue('image') || null,
        }

        // Create a basic embed using only the description (it's mandatory)
        const newMessageEmbed = createEmbed({
            description: embedData.description
        })

        // Dinamically add the fields based on the values provided
        if (embedData.title) newMessageEmbed.setTitle(embedData.title)
        if (embedData.url) newMessageEmbed.setURL(embedData.url)
        if (embedData.thumbnail) newMessageEmbed.setThumbnail(embedData.thumbnail)
        if (embedData.image) newMessageEmbed.setImage(embedData.image)

        // Get the current channel or the provided one
        const selectedChannel = (interaction.options.getChannel('canal') ||
            interaction.channel) as TextChannel

        // Send message to selected channel
        await selectedChannel.send({ embeds: [newMessageEmbed] })

        // Follow up message
        await modalInteraction.followUp({
            embeds: createArrayedEmbed({
                title: 'Asistente de Creación de Mensajes',
                description: '✅ Mensaje enviado correctamente.',
            }),
            flags: 'Ephemeral',
        })
    } catch (error) {
        console.log(error)
    }
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744306843a4567755.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信