discordjs / discord.js

A powerful JavaScript library for interacting with the Discord API
https://discord.js.org
Apache License 2.0
25.25k stars 3.95k forks source link

Random "Unknown interaction" errors sometimes #7005

Closed ikheetjeff closed 2 years ago

ikheetjeff commented 2 years ago

Issue description

I have a bot with clicks on buttons every second and people executing commands. This generally goes very well, but sometimes I get an "Unknown Interaction" when I defer to it.

I've been debugging for the past three days and can't seem to understand why it's happening. My last possible explanation is an error in Discord.js or on the side of Discord.

I have attached a few screenshots describing the error Screenshot_17 Screenshot_18

I would prefer to know if this is due to my code, or Discord.js or Discord itself.

Code sample

// Button Handler
        if(interaction.isButton()) {
            console.log(customId);
            const button = client.buttons.get(customId);
            if(!button) return;

            let {
                cooldown = -1,
                deferUpdate = false
            } = button;

            if(deferUpdate) {
                console.error('defer button');
                const testHook = interaction;
                console.error(interaction.member.user.username, interaction.member.id)
                await interaction.deferUpdate();
                console.error("doei", interaction.member.user.username, interaction.member.id)

            }

            let buttonName = (customId.startsWith('games-all-')) ? "btn-games-all" : "btn-" + customId;
            console.log(buttonName);
            if(!cooldownButtons.has(buttonName)) {
                console.log(buttonName, 'add cooldown')
                cooldownButtons.set(buttonName, new Collection());
            }

            const currentBtnTime = Date.now();
            const timestamps = await cooldownButtons.get(buttonName);
            const cooldownAmount = (cooldown) * 1000;

            if(timestamps.has(member.id)) {
                console.log(member.id, 'heeft cooldown')
                const expirationTime = timestamps.get(member.id) + cooldownAmount;

                if(currentBtnTime < expirationTime) {
                    console.log(member.id, 'heeft cooldown')
                    const time_left = (expirationTime - currentBtnTime) / 1000;

                    if(deferUpdate) {
                        return interaction.followUp({content: `**Cooldown:** You have to wait ${time_left.toFixed(1)} seconds.`, ephemeral: true});
                    }else{
                        return interaction.followUp({content: `**Cooldown:** You have to wait ${time_left.toFixed(1)} seconds.`, ephemeral: true});
                    }
                }
            }else{
                timestamps.set(member.id, currentBtnTime);
                setTimeout(() => timestamps.delete(member.id), cooldownAmount);

                try{
                    await button.execute(interaction);
                }catch(error) {
                    console.error(error);
                }
            }
        }

discord.js version

13.3.1

Node.js version

16.13.0

Operating system

Debian 10

Priority this issue should have

Medium (should be fixed soon)

Which partials do you have configured?

CHANNEL, GUILD_MEMBER, MESSAGE, REACTION

Which gateway intents are you subscribing to?

GUILDS, GUILD_MEMBERS, GUILD_INVITES, GUILD_VOICE_STATES, GUILD_MESSAGES, GUILD_MESSAGE_REACTIONS, DIRECT_MESSAGES

I have tested this issue on a development release

No response

Yippy commented 2 years ago

I use deferReply() as soon as I got the interaction, yet it fails. As I don't require the 'Bot is thinking', so I just get the interaction and then deferReply soon after I just delete the reply. image

Most of my logic is just managing the message 'after' defer is removed, that doesn't take 3 seconds.

z1haze commented 2 years ago

Defer won't fix it. Most of my commands defer and they still randomly throw this error from time to time

yonilerner commented 2 years ago

The client.user.setActivity call is causing the client to not receive anything from the API for many seconds.

Setting presence is blocking in Discord, that's the intentional gateway behavior

Curious if this is still an issue since https://github.com/discord/discord-api-docs/issues/4073 was resolved

therealbenpai commented 2 years ago

Problem fixed! I didn't know what to do and started doing everything that I can change in my bot. I regenerated my token and it finally worked. Iam assuming that a second bot was running with the same token.

Thank the lord for this. I was using "PM2" and it wouldn't let me stop the code no matter what I did. I ended up regening the token and poof the problem was fixed! Thanks sooo much!

SDCore commented 2 years ago

Thank the lord for this. I was using "PM2" and it wouldn't let me stop the code no matter what I did. I ended up regening the token and poof the problem was fixed! Thanks sooo much!

This seems like more of an issue with either a dev environment using the same token or running nodemon at the same time as PM2, and it created 2 instances of the bot. If you restarted your VPS (assuming you were using one), that probably would've fixed the issue. If you're running it on your own PC, especially Windows, sometimes closing something like VS Code while you still have your bot running doesn't always stop node, and so you still have a rogue bot running in the background. Idk, it's weird, but in the end I don't think this specifically was related to the problem this issue is talking about.

I DO want to point out, I moved my bot to a different VPS, one with a higher clock speed CPU, some better RAM, and an NVMe drive instead of an SSD, and haven't gotten any of these problems once. I'm even able to host multiple discord bots on the same VPS now without any problems at all (aside from a few gateway issues that are a problem with my bot, but nothing with the "Unknown interaction" errors). So I'm not sure if it was just weak or glitched hardware that was causing problems, or if switching things around made whatever problem there was magically go away. Guess we'll never know.

amsam0 commented 2 years ago

happening for me a lot in ubuntu and occasionally in mac, both using pm2, been happening for months and (this is most likely an issue with my code when migrating to v14 dev) happening without pm2 on mac with the latest discord.js dev version

I've been using deferReply almost since I started using interactions so that's not the problem, I get the error instantly after click a button/select menu

haven't tested with modals because I haven't fully migrated to v14 dev

I have multiple bots running with the same token but one of them is purely a bot that sends a message at a specified time and has no events for interactions, messages, etc

TurcanVeli commented 2 years ago

İf asking me, You can't handle this error with just using defer. You also should use defer() some commands beacuse of 3 seconds rule of slash-commands . Not changing presence every 20 second is not the cause of this error. I got this error and I have changed loop time of tasks and fixed it. I mean, it is all about optimization of you app.

therealbenpai commented 2 years ago

İf asking me, You can't handle this error with just using defer. You also should use defer() some commands beacuse of 3 seconds rule of slash-commands . Not changing presence every 20 second is not the cause of this error. I got this error and I have changed loop time of tasks and fixed it. I mean, it is all about optimization of you app.

At that point, you might as well put the defer at the beginning of the command handling code. Then, edit it later. My issue was resolved and at this point, the discussion has gone off-topic

ImRodry commented 1 year ago

I don't understand why this issue was closed because no real solution was found. I'm having the same problem where I'm responding in time, either with a modal or deferring the reply yet I randomly get this error every few hours. I asked in DDevs and they said it could be related to the WebSocket connection and, since that part is done by discord.js alone, I think this should be looked into because, despite it not being something everyone experiences, it's quite annoying for the ones that do as we cannot find a reason for it

didinele commented 1 year ago

The issue was closed because there was nothing to use to consistently reproduce and debug, if you want it to be looked into, you can start by supplying us with a code sample that can actually trigger the bug.

ImRodry commented 1 year ago

There’s no code sample I could possibly give you since this is triggered by a deferReply with nothing before it, or any interaction response for that matter. Like I said the issue is not consistent and thus it’s impossible to replicate without leaving a bot on for hours and waiting for it to happen. I suspect it’s related to when the ws reconnects but I can’t be sure

Syjalo commented 1 year ago

There’s no code sample I could possibly give you since this is triggered by a deferReply with nothing before it, or any interaction response for that matter. Like I said the issue is not consistent and thus it’s impossible to replicate without leaving a bot on for hours and waiting for it to happen. I suspect it’s related to when the ws reconnects but I can’t be sure

Most likely, this is due to the use of promises in the interactionCreate event that resolves after 3 seconds the interaction was created. Like request to a database or another API.

ImRodry commented 1 year ago

I said this already but I’ll repeat it. The error is thrown on the first line of code that is executed, there are no promises before that, only 2 if statements that only do boolean comparisons, the issue is not the code because if it was it would be happening every time and not once every few hours

kyranet commented 1 year ago

Locking the issue since it's already resolved.

If you cannot always ensure a response within 3 seconds, use deferReply, with nothing else. If you do something prior to deferReply, make sure it doesn't delay or prevent the initial response, so you may send a new message later.

This is also most likely not a Discord.js issue, the calls (deferReply included) are dispatched very fast. If you encounter the issue (if any) with precise steps on how to reproduce it consistently even on fast enough networks and machines, please open a new issue with the new details.