#Unknown interaction, sometimes

38 messages · Page 1 of 1 (latest)

quick prawn
#

The problem is that sometimes it works and sometimes it generates an error, and I've already tried it without using defer, same result.

How my Main class register commands

export default class MyClient extends Client {
    constructor(options) {
        super(options);

        this.slashCommandsArray = [];
        this.loadEvents();
        this.loadSlashCommands();
    };

    async registerCommands() {
        await this.application.commands.set(this.slashCommandsArray, "guild_id");
    };
}```
My interactionCreate Class
```js
export default class InteractionCreate extends ReadyClient {
    constructor(client) {
        super(client, {
            name: "interactionCreate"
        })
    }
    execute = async(interaction) => {
        const commandName = interaction.commandName;
        const command = this.client.slashCommandsArray.find(cmd => cmd.name === commandName);
        
        if(!command) return;

        await interaction.deferReply();
        await command.execute(interaction);
    }
}

my test command

export default class Ping extends SlashCommands {
    constructor(client) {
        super(client, {
            data: new SlashCommandBuilder()
            .setName("ping")
            .setDescription("Reply with pong")
        });
    };
    execute = async(interaction) => {
        await interaction.editReply("Pong!");
    };
};
rich groveBOT
#
  • What's your exact discord.js npm list discord.js and node node -v version?
  • Not a discord.js issue? Check out #1081585952654360687.
  • Consider reading #how-to-get-help to improve your question!
  • Explain what exactly your issue is.
  • Post the full error stack trace, not just the top part!
  • Show your code!
  • Issue solved? Press the button!
mild belfry
#

If that errors with Unknown interaction (and the interaction also fails in discord) it looks like your network/host is slow at times and/or something blocking the event loop. Because there is nothing before the deferReply that would take time…

quick prawn
#

Isn't it an error related to interactionCreate or something like that?

#

in theory even if my connection was very bad, with defer it wouldn't be a problem

mild belfry
#

Why wouldn’t it be a problem? It obviously is as you can see from you having that error. Your error suggests that the deferReply (assuming that‘s what shows in the error‘s stacktrace) reaches discord more than 3 seconds after discord sent the interaction to your bot. So if your connection is so bad that it takes 2 seconds to even reach you (and 2 more to send defer back) that would be a problem

quick prawn
#
{
  requestBody: { files: undefined, json: { type: 5, data: { flags: undefined } } },
  rawError: { message: 'Unknown interaction', code: 10062 },
  code: 10062,
  status: 404,
  method: 'POST',
....

defer is not the problem, I tried without it too, just with await interaction.reply in the ping command file and I get the same result, sometimes it works and sometimes it gives the error

mild belfry
#

Yes, that won‘t magically make your response time faster. The only thing defer does is tell discord „hello, I‘m thinking, I‘ll tell you the result later“. Both deferReply or reply need to happen within 3s to not fail

#

And that is not a stacktrace you showed, that’s only (part of) the request that got sent to discord

quick prawn
#
node:events:492
      throw er; // Unhandled 'error' event
      ^

DiscordAPIError[10062]: Unknown interaction
    at handleErrors (/home/junior-light/Desktop/discord-js-test-0/node_modules/.pnpm/@[email protected]/node_modules/@discordjs/rest/dist/index.js:722:13)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async BurstHandler.runRequest (/home/junior-light/Desktop/discord-js-test-0/node_modules/.pnpm/@[email protected]/node_modules/@discordjs/rest/dist/index.js:826:23)
    at async _REST.request (/home/junior-light/Desktop/discord-js-test-0/node_modules/.pnpm/@[email protected]/node_modules/@discordjs/rest/dist/index.js:1266:22)
    at async ChatInputCommandInteraction.deferReply (/home/junior-light/Desktop/discord-js-test-0/node_modules/.pnpm/[email protected]/node_modules/discord.js/src/structures/interfaces/InteractionResponses.js:69:5)
    at async MyClient.execute (file:///home/junior-light/Desktop/discord-js-test-0/src/events/Interaction/InteractionCreate.js:20:9)
Emitted 'error' event on MyClient instance at:
    at emitUnhandledRejectionOrErr (node:events:397:10)
    at process.processTicksAndRejections (node:internal/process/task_queues:84:21) {
  requestBody: { files: undefined, json: { type: 5, data: { flags: undefined } } },
  rawError: { message: 'Unknown interaction', code: 10062 },
  code: 10062,
  status: 404,
  method: 'POST',
  url: 'https://discord.com/api/v10/interactions/1224707594568994958/aW50ZXJhY3Rpb246MTIyNDcwNzU5NDU2ODk5NDk1ODp4U0t4YkxBS3VxZUxlT2xONklCN1NtUmxCOEE1M1ZmMGZqejZXOE42bjdMcXpXU1gzcWw3eURYMzg1RFR0QWNGZFVsbHdJTkxDTVQ4U1R4S2NQMnpOVTVkQ0t2ZlhLZnpvQVZZdFE5b1hUR0g2N0pBS3hyUHZTZHhmdElMZXJ5Tg/callback'
}

#

I'm still thinking it strange that the problem is my connection

mild belfry
quick prawn
#

yes

#

sometimes

mild belfry
#

Then something causes it to take more than 3s between discord sending the interaction and discord receiving the deferReply response. Either network or your host (or both) are slow (or discord‘s host, but they‘d tell you on #discord-api-status )

#

Unless you have hidden async tasks in your event handler for some reason

#

Wait, why does your InteractionCreate class extend your client?

quick prawn
#

oh, just name is wrong, its extends simple EventMap class

export default class EventMap {
    constructor(client, options) {
        this.client = client,
        this.name = options.name,
        this.once = options.once || false
    };
};

not is the problem

mild belfry
#

Show your event handler then. Probably your loadEvents method in MyClient?

quick prawn
#

    async loadEvents(src="./src/events") {
        const eventsFolders = readdirSync(src, { withFileTypes: true }).reduce((previousValue, nextValue) => {
            return nextValue.isDirectory() ? [...previousValue, nextValue.name] : previousValue;
        }, []);
        for(const eventFolder of eventsFolders) {
            const eventsFiles = readdirSync(`${src}/${eventFolder}`).filter(eventFile => eventFile.endsWith(".js"));
            for(const eventFile of eventsFiles) {
                const eventPath = join(process.cwd(), src, eventFolder, eventFile);
                const { default: EventMap } = await import(eventPath);
                const event = new EventMap(this);
                if(event.once) {
                    this.once(event.name, event.execute);
                };
                if(!event.once) {
                    this.on(event.name, event.execute);
                }
            }
        }
    };
#

yes, in MyClient

mild belfry
#

Any other sync tasks like that readdirSync you do in your code other than at startup? Because sync I/O can and will block your event loop

#

Not an issue at startup (since you‘re not connected to the gateway and handling events yet) but definitely relevant during runtime

#

Oh, and you call that async loadEvents but don’t (and can’t) await it. But you do sync I/O in it notLikeCat

#

That’s prone to race conditions

quick prawn
mild belfry
#

Yes, you call it, but since it‘s async and not awaited that Promise will not be resolved by the time you go on. And you can’t await in a constructor

quick prawn
quick prawn
mild belfry
#

And do you receive the Unknown Interaction errors close to your bot start or later on?

mild belfry
#

And await it there

#

Or store the promises in a private property and await them in the login call before actually logging in

quick prawn
mild belfry
#

Monitor your resource usage for those times it happens. If CPU spikes: host issue. If network speed goes flat: network issue. Etc.

quick prawn
quick prawn
#

Then I'll try to upload it to a VPS or another machine and leave feedback here

glacial mist