Encountering an obscure issue when using Discord.js v14 after attempting to cancel and resubmit a modal

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?

Answer №1

After reading a helpful comment on the Discord.js GitHub page, I was able to identify and solve the issue.

When a modal submission is cancelled, no interactions are sent to the modal submit collector. This means that if a second modal submit interaction occurs after the first one was cancelled, both the first and second collectors will try to handle the interaction.

To address this issue, I modified the code to use the interaction's unique ID to create the modal's custom ID and apply filters based on that:

 // Code snippet showing modification

By making these changes, I eliminated the "Unknown interaction" error log. Although the InteractionCollectorError timeout error may still occur, it can be handled as needed (I personally chose to ignore it).

I hope this solution proves helpful to others facing the same issue.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Comparing the use of jQuery's data() method versus directly accessing attributes with native JavaScript using get

I want to retrieve general information from my website using javascript. I have a few different options available: I could use an html element: <input type="hidden" value="MyValue"/> Another option is to use a custom attribute in an existing h ...

Removing data from a table using an identifier in Typescript

Recently, I have made the switch from using Javascript to TypeScript. However, I am facing an issue while trying to delete data from a table in my code. Whenever I attempt to delete data, I encounter this error message: Property 'id' does not e ...

How to Use JavaScript Function to Rotate an Entire Webpage

For my final project in a web design class, we were tasked with creating a website that showcases our skills. I've completed the assignment and now I want to add some interesting features to make it stand out. I'm interested in using -webkit-tra ...

Using PHP to convert JSON data into the Google Chart API

I am facing an issue with parsing my PHP generated JSON array into a Google chart. I have integrated JQuery, but for some reason, I am struggling to parse the JSON data. I execute a MySQL query to fetch data from the database. Then, I encode the query resu ...

Implementing a design aesthetic to installed React components

After downloading a react multiselect component created by an individual, I installed it using npm install .... and incorporated the component in my react code with <MultiSelect .../>. The dropdown checkbox 'menu' is designed to allow users ...

Learn how to effectively utilize templateURL in an express and angular project

Our project utilizes Express without any view engine. To set up static directories, we have the following: app.use(express.static(__dirname + '/public')); app.use(express.static(__dirname + '/view')); app.use(express.static(__dirname + ...

What is the reason Node.js only prints one item at a time?

Currently, I am using node.js to extract items from a text file. Right now, the code prints out the items in the terminal, but I want it to be able to accept multiple parameters and print each of them individually instead of just the last one. // Checki ...

Assign the ng-repeat item to a variable in the controller's scope

Can anyone help me figure out how to pass a particular item from my view to a function in my controller? Here is the code: The view where I want to pass p.id <tr ng-repeat=" p in projetsListe"> <td>{{p.NomProjet}}</td> <td>{{c ...

Having trouble with table sorting in Jquery?

I am a beginner in the realm of Jquery and web programming. Recently, I attempted to implement the tablesorter jquery plugin for one of my projects but encountered issues with making it work properly. In search of a solution, I turned to Stack Overflow. C ...

Encountering difficulties displaying a webpage in Express.js following a successful DELETE request

While working on a URL shortening project using Node.js, MongoDB, and ejs, I encountered an issue when trying to render a page after successfully handling a DELETE request. Although the 'deletion' page is loaded in the response preview in the bro ...

Adding a collection of items to an array in preparation for organizing

Realizing that sorting objects is not feasible - despite the necessity of having the object values sorted - I conclude that the only option is to move the object array content into a new array for sorting purposes. The following code generates the output ...

What is the reason behind this code's ability to detect vowels as well as other

I'm currently working on debugging a specific portion of code for my class. I'm having trouble understanding why this JavaScript code is counting all letters instead of just vowels. var text, i, sLength, myChar; var count; var text = prompt("Ple ...

Using cURL in PHP to post a message to a Discord channel

I am currently attempting to send a message to my Discord channel using cURL with a POST request. The problem I'm encountering is a 401 error indicating that I am unauthorized. My PHP code is running on a webserver using xampp localhost. Additionally, ...

The then() function in Node.js is triggered before the promise is fully resolved

I'm struggling to get my Promise function working as intended. Here's what I need to accomplish: I am receiving file names from stdout, splitting them into lines, and then copying them. Once the copy operation is complete, I want to initiate oth ...

Is there a way to simulate AWS Service Comprehend using Sinon and aws-sdk-mock?

As a newcomer to Typescript mocking, I am trying to figure out how to properly mock AWS.Comprehend in my unit tests. Below is the code snippet where I am utilizing the AWS Service Comprehend. const comprehend = new AWS.Comprehend(); export const handler ...

Unable to insert a JSON object into an array using JavaScript and MongoDB

I'm encountering an issue when trying to push data into my Student model with the following schema: var StudentSchema = new Schema({ firstName: { type: String, trim: true, default: '' //validate: [validat ...

What is the best approach for managing _app.js props when transitioning from a page router to an app router?

Recently, in the latest version of next.js 13.4, the app router has been upgraded to stable. This update prompted me to transition my existing page router to utilize the app router. In _app.jsx file, it is expected to receive Component and pageProps as pr ...

Creating a function for loading dynamic content in XSLT can be achieved by following these steps

When I call the function collapseandexpand() for static elements only, it does not work. Now, how can I create the same function to handle dynamic content in xslt? Javascript Code: <script language="javascript" type="text/javascript> <xsl:text&g ...

How can I remove the outline when focusing with the mouse, but still retain it when focusing with the

Is there a way in JavaScript to detect if an element received focus from the keyboard or mouse? I only want the outline to show when it's via the keyboard, not the mouse. If this is possible, how can I override the browser's default outlining be ...

Encountering a Typescript issue when linking a class component with the reducer

Within my class component that is linked to the redux rootReducer, I am encountering a TypeScript error specifically related to the mapPropsToState section. The error message reads: Property 'unit' does not exist on type 'DefaultRootState&ap ...