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条)