I'm currently working on a Discord bot using modals in Discord.js v14. These modals appear after the user clicks a button, and an .awaitModalSubmit()
collector is triggered to handle one modal submission interaction by applying certain logic. The .awaitModalSubmit()
method includes a simple filter
and time
parameters to specifically fetch modal submit interactions within a defined time frame (one minute) that correspond to the same user initiating the interaction and contain the correct customId
.
Below is a condensed and replicable snippet of my code:
await interaction.showModal(
new ModalBuilder()
.setTitle("Echo Modal")
.setCustomId("echomodal")
.setComponents([
new ActionRowBuilder<TextInputBuilder>().addComponents([
new TextInputBuilder()
.setCustomId("echomodaltext")
.setLabel("Echo Text")
.setStyle(TextInputStyle.Short),
]),
])
);
const result = await interaction.awaitModalSubmit({
filter: async (i) => {
await i.deferReply();
return i.user.id === interaction.user.id && i.customId === "echomodal";
},
time: 60_000,
});
await result.editReply(result.fields.getTextInputValue("echomodaltext"));
This workflow functions smoothly, but encounters issues when a user selects the Cancel button on a Discord modal:
https://i.sstatic.net/c7ulN.png
Subsequently, if another modal is displayed during the one-minute timeframe, and data is submitted this time, it continues to work as expected; however, an unhandled exception error is logged:
Error: DiscordAPIError[10062] - Unknown interaction
Furthermore, after one minute has passed since the initial cancelled interaction, the following error log is generated:
Error: Error [InteractionCollectorError] - Collector received no interactions before ending with reason: time
It appears that the collector isn't correctly terminated upon cancellation of a modal submission by the user, leading to these errors. At present, I'm unsure how to resolve this issue.
Do you have any suggestions?