#Issue with deferring replies

1 messages · Page 1 of 1 (latest)

elder rose
#

I haven't yet, I'm not sure whether it's to do with the host server having high ping (it usually has about 200ms as I use a UK based host) or whether Discord's having any issues (I don't think they are). It's weird that it's only tonight and also that I've not changed any code

strong juniper
#

same issue man i am using digital ocean and tonight only i am having this issues

#

sometimes it not even register

elder rose
#

Two things to say about deferred replies though:

1: Make sure there's nothing in the brackets (like: interaction.deferReply()) as I've already said

2: Make sure that deferring the reply is the first thing you do in code. Example here:

async execute(interaction) {
await interaction.deferReply()

<some code here>

interaction.editReply("Some content here") 
}
strong juniper
#

like this as i have some variables before it

elder rose
#

No, I have it right at the very top. Just to avoid as many issues as possible. Although I'm using deferReply right(must be doing as I've had no issues before this) and still having some issues

#

But try that first

strong juniper
#

done lets see that

#

at the host i am trying to run it not even showing that it logged in

elder rose
#

Just wait a bit first. I really want to know whether this is more widespread and an issue with Discord or not

strong juniper
elder rose
#

Weird. My host has been doing that where I start and it does that a couple of times, I have to restart. Then it just fixes. And then if I have to restart again then it does it again

#

Just tonight as well

strong juniper
#

yep same process

#

really give me a stress for rest of the day :c

#

you're using digital ocean?

#

i am using digital ocean standardised plan

elder rose
#

Even more stress for me as I'm waiting for my bot to be approved for top.gg. Error only happened after I submitted as well. I'm worried that it'll cause issues when being tested

elder rose
strong juniper
#

idk what should i do now

elder rose
#

I've switched some of my commands to just reply (No defer and edit) and it works kinda, for the time being until I can fix it

strong juniper
#

however i am using pm2 and forever to make it active

elder rose
#

I'm not using those and still having issues

formal briar
#

have you tried using your old code (without changing to reply) in another machine to see if it works just fine?

#

that's what happens with my (similar) issue. it works perfectly ok, same code, if i run it on my dev machine over my raspberry pi

elder rose
#

Yep, works fine on my local machine with my testing bot (same code as other one but with editReply), just not working on a host I use

strong juniper
#

if i try to see from my testing bot it works fine no issue but it only happens with the bot that is hosted

humble stone
#

Add logs into your code to see what’s taking so long. The issue is literally either too much compute and not done quick enough to reach the reply, or your connection just isn’t strong enough to initially reply

#

There’s no other reason

elder rose
strong juniper
#

okay so i think it is working now

#

what i did was i reboot my host

#

and without defer my bot will broke because it needs it to generate response as it will take time on that

humble stone
#

That’s the thing, things which need time to do you need to accommodate for

#

Anything which requires a db fetch I defer

elder rose
#

I added logs. There's like a 600 millisecond difference between the log below where I have deferReply and the one just before I edit the reply, but it gets stuck on defer. I also have a log just after the edit reply and that one isn't sending. For some reason when on the host it doesn't edit, when on my PC it works fine. It also worked on the host fine until tonight

humble stone
#

Do you await the defer

#

Show the code if you can please

formal briar
elder rose
humble stone
#

Something is taking too long somewhere

#

The api won’t die on you that easily 😉

elder rose
#

Now it shows all 4 logs, even the one after editReply but it doesn't edit. But I'll add some in the interactionCreate

humble stone
#

It just doesn’t edit?

elder rose
#

It defers, it also logs the time after the edit but doesn't edit. I just did a test now, it takes about 4 seconds between the first and last log though

humble stone
#

maybe also await the editReply due to it being a promise and may not resolve

elder rose
#

maybe

humble stone
#

4 seconds is weirdly… Hugh

#

High*

#

Just as the other guy, that’s just showing an embed and some buttons which is weird

elder rose
#

For a help command too. No issues before tonight too with no code changes. The host usually has high ping (about 200ms) due to it being UK-based but that's never caused any issues before

strong juniper
#

it is happening again now 😭 man should i giveup?

humble stone
#

Show the code

#

You reply after you have replied

#

Do you have any error handling which responds with a reply

elder rose
#

Now 5 seconds with another test, same code. It seems varied. Although final log still not gone through

humble stone
elder rose
#

I changed it, the code didn't save properly

humble stone
#

Ah

elder rose
humble stone
#

Does ur edit?

#

It

#

Why my phone autocorrecting like that

elder rose
#

no, it still doesn't edit. Last log doesn't come through. Still a delay I think between first and second last log though for some reason

strong juniper
humble stone
elder rose
#

Just another thing to note, might be unrelated but it does this for a couple of tries, then restarts like a third time then doesn't crash but does the no edit reply thing

#

Like it does that, restarts, does it, restarts then works with no crash but no editReply

humble stone
#

Something isn’t saved

#

As that’s an unknown interaction coming from a reply…

#

What’s help.js line 48

digital root
#

hmmGe now I'm confused with what the problem is

#

is it that it's deferring and not editing

elder rose
humble stone
#

Nah that’s an error coming from a reply

#

help.js line 48

#

Something either isn’t saving on your end or it’s not the code we are seeing

strong juniper
#

Idk why its not working

#

it was just tonight it has to go this way

digital root
#

I don't even know the problem dogeHaHa

elder rose
#

If I change it to reply, not editReply and no defer it works. But when I do defer and edit, it doesn't work. Like the other guy said, only tonight

digital root
#

so it's deferring, but not editing the reply?

elder rose
#

yeah

humble stone
spice harborBOT
#

Please add the following code to your code base outside of any other event listeners and provide the full log output relevant to your issue.

client
    .on("debug", console.log)
    .on("warn", console.log)

• Note: if you initialize your Client as bot or other identifiers you need to use these instead of client
• If the output is too long to post consider using a bin instead: gist | paste.gg | sourceb.in | hastebin

digital root
#

have you done this

elder rose
#

no

digital root
#

do it, send output

elder rose
digital root
#

outside of any other event listeners

humble stone
#

Wherever you have access to your client, probs index.js

elder rose
#

ok

digital root
#

do it right after creating the client

strong juniper
#

done

humble stone
#

Output?

strong juniper
#

yes

humble stone
elder rose
humble stone
#

Send the output

strong juniper
digital root
#

send the output

elder rose
#

long delay by looks of it

strong juniper
humble stone
#

1066ms Awk

digital root
#

is that after the command is run

strong juniper
elder rose
#

no, I've just started the bot with no commands. Every interval now it's saying about 200ms, what I usually see on the host as its in the UK so far from Discord

strong juniper
#

after running the command it shows 300ms which i assume normal

humble stone
elder rose
#

It's finally replying. I'll do some more testing. Although pretty big delay (like 12 seconds for a help command...)

strong juniper
formal briar
spice harborBOT
#

DiscordAPIError: Interaction has already been acknowledged
[InteractionAlreadyReplied]: The reply to this interaction has already been sent or deferred.

You have already replied to the interaction.
• Use <Interaction>.followUp() to send a new message
• If you deferred reply it's better to use <Interaction>.editReply()
• Responding to slash commands / buttons / select menus

formal briar
#

i was and removing that made everything work like normal after

#

i lose logging but now i don't need to change all my code

elder rose
formal briar
#

no, that's not what i mean. do you do literally anything with the interactions before executing the commands?

humble stone
formal briar
#

despite the fact i awaited the command execution, my logging took so long that i blocked the actual important reply

elder rose
formal briar
#

i had to comment out my logging and now it works like it used to

#

i don't timeout to handle interactions anymore, despite this not being awaited and also being placed BELOW the await execute command

formal briar
elder rose
#

yes

strong juniper
formal briar
# elder rose yes

i understand. sorry for not being able to help you any further then

humble stone
#

You definitely aren’t

strong juniper
#
module.exports = {
    data: new SlashCommandBuilder()
        .setName('askme') // set the name of the command to /askme
        .setDescription('Replies with a response generated using the OpenAI API')
        .addStringOption(option =>
            option.setRequired(true)
                  .setName('input')
                  .setDescription('The input to use to generate the response')),
    async execute(interaction) {
        // send the initial "thinking" message using deferReply
        await interaction.deferReply();

        // get the value of the input option
        const input = interaction.options.getString('input');

        prompt += `You: ${input}\n`;

        // use the OpenAI API to generate a response based on the input using Chat GPT - 3
        const response = await openai.createCompletion({
            model: "text-davinci-003",
            prompt: prompt,
            max_tokens: 360,
            temperature: 0.5,
            top_p: 1.0,
        });

        await interaction.reply('pong')

          // reply to the user with the generated response
        //   await interaction.reply(`**input: ${input}**\n\n${response.data.choices[0].text.substring(5)}`);
          prompt += `${response.data.choices[0].text}\n`;

          console.log(prompt);
        },
};

this is the code that is doin the thing

#

now what i am doing wrong there?

elder rose
#

you have to edit replies too with defer, you did await interaction.reply("pong"), not editReply("pong")

humble stone
#

So you reply like 3 times

elder rose
#

It's annoying because it just worked then but then I tried again and it didn't edit

strong juniper
#

thanks for the help guys we fix the error by creating another error and fixing it

#

weird that i am getting error in the console but things are working fine

elder rose
#

good that it's kind of fixed for you. Still no clue what's wrong for me though

#

I did a bit of testing without .editReply or .deferReply and just with .reply as it works with that and found that with .deferReply and .editReply between the first and last log it had a delay of 600ms to a couple of seconds and with reply it had a delay of 3-4ms. So with defer and edit it had a way bigger delay, no other code changes

elder rose
#

Surely that's more of an increase of delay by just putting defer in than normal, or does defer and edit do that (make a big delay)?

elder rose
#

I asked in the main Discord Devs server whether it was an issue with them or they knew how to fix it and the only thing they said was that I could try editing with https://discord.com/api/webhooks/${interaction.application_id}/${interaction.token}/messages/@original using the patch method. How would I do this in djs? I'll also continue to monitor to see whether it fixes tomorrow

humble stone
#

Iirc that’s just interaction.webhook.editMessage()

#

Would also be nice to know your exact djs version

blissful tartan
humble stone
#

Thanks Qjuh

elder rose
blissful tartan
#

What, where?

#

This thread is rather long by now, I only joined now

blissful tartan
elder rose
#

No, only the first

#

It occasionally works but only very occasionally

humble stone
#

Can you show how you execute commands inside your interactionCreate file

#

Or event rather

blissful tartan
elder rose
humble stone
#

Can you show how you execute commands

#

Inside interactionCreate

blissful tartan
elder rose
#

No, it just gets stuck on defer (not is thinking...)

blissful tartan
#

Then you catch the rejection somewhere…

elder rose
blissful tartan
#

That is a catch and an error handler right there

elder rose
#

Oh, I thought you meant a whole file just for handling errors

#

Yeah I have one then

blissful tartan
#

Does the followUp happen?

#

Or the console.log of the error?

elder rose
#

No, but in most cases it does

#

Just not with this error

blissful tartan
#

How long are you waiting for an error before turning the bot off again?

elder rose
#

I waited nearly an hour over dinner while it was running and no error

blissful tartan
#

But the deferReply was still there? No editReply happened either?

elder rose
#

Yeah it stayed on defer reply for that hour with no edit, and no error in console

#

We'll tbh I'm pretty sure it times out after 15mins with defer so it timed out

blissful tartan
elder rose
#

I got no error in console if that's what you mean. It stayed on defer

blissful tartan
#

That… shouldn’t happen ever… you should either get a rejection from the editReply call or it should resolve (and have successfully edited the reply by then). Did you await the editReply always?

elder rose
#

Yeah I awaited the editReply

blissful tartan
#

As I Said, then this should be impossible…

spice harborBOT
#

Please add the following code to your code base outside of any other event listeners and provide the full log output relevant to your issue.

client
    .on("debug", console.log)
    .on("warn", console.log)

• Note: if you initialize your Client as bot or other identifiers you need to use these instead of client
• If the output is too long to post consider using a bin instead: gist | paste.gg | sourceb.in | hastebin

elder rose
#

I've done that, it's pinging back around 200-300ms each time

blissful tartan
#

That’s not what I‘m interested in… the heartbeats are irrelevant… are there any other debug logs besides the heartbeats happening?

#

Oh, might have to add it on client.rest to get the interesting ones

humble stone
#

This is all that was printed iirc

blissful tartan
#

client.rest.on('response', console.log)

#

Those will spam your console, so be prepared😂

blissful tartan
elder rose
humble stone
blissful tartan
elder rose
#

What should I send? All of it?

humble stone
#

Probably lmao. There’ll be a lot

elder rose
#

Shit token leaked I think

#

Wait a sec

humble stone
#

Probs only me who saw it and tbh I don’t care lol

elder rose
#

Ok, still I'll reset and send

blissful tartan
#

You should reset either way

#

And only the requests with interaction in its URL should be relevant

elder rose
#

Ok

#

There's one, the other two don't seem helpful

humble stone
#

set-cookie lol

elder rose
#

Here's what happens if I run the command. Stuck on defer again

blissful tartan
#

Did the editReply succeed?

elder rose
#

Oh it did now. I'll try it again a couple of times

humble stone
blissful tartan
humble stone
#

Ahhh ok thank you

#

Imagine it just works now and I just bought Qjuh here for no reason 😭

blissful tartan
#

I‘m not for sale😳

elder rose
#

Oh, it broke again...

humble stone
#

Did the logs show anything juicy 😋

elder rose
#

Well one cookie expired on the Unix epoch, I wouldn't eat that now

blissful tartan
#

Expire=0 means just never

elder rose
#

No it says expires Jan 1st 1970. Very old cookie

#

Anyway I'll read logs more

humble stone
#

Probably better to show them here haha

blissful tartan
#

That’s just how the logger parses that. Undici should know better how to interpret 0 I hope

humble stone
#

I’m just confused as this has to be something to do with their code, right? No offence lads. Because this error would have 100% happened before

elder rose
#

I do get this every couple of restarts but then it just goes and works (apart from the defer thing) until it does it again

#

But I restart a couple of times and it goes

blissful tartan
#

You could also filter the response for response.route.includes('webhooks') to reduce the amount you need to look through

humble stone
#

Ngl these are errors I typically get whenever I double host my bot…

blissful tartan
blissful tartan
humble stone
#

||Wheb I leave my laptop open with vs code and my host on||

#

Ah

humble stone
#

Oh yea not been sent

blissful tartan
#

Those you have there are classic replies too late errors. Those are common and need to be expected and handled. The one where it defers but never edits, that one is weird

elder rose
#

I'm gonna have to come back to this in the morning as I don't have access to my pc rn and its late

humble stone
#

No worries dude

elder rose
elder rose
#

The replies too late ones

blissful tartan
#

As it seems to catch the ones where you reply too late

elder rose
#

It crashes the bot when that happens for some reason

blissful tartan
#

It does, you just try to followUp to them which produces another error… don’t followUp if interaction.replied is false

#

Rule of thumb: don’t try to use the mechanism that causes your error to do something else with it…

elder rose
#

So same error, even when uploading my local files which work to the host but it did log this

blissful tartan
#

And did it successfully edit the reply? And what does the status on that response say (scroll down)?

elder rose
#

This? And no it didn't edit

blissful tartan
#

Even below that. I‘m looking for statusCode and the response itself

elder rose
#

Status code: 429

blissful tartan
#

Ah, you‘re ratelimited… you do that too much

#

You hit a Cloudflare ratelimit

elder rose
#

What's the cooldown until I'm not? And is this an issue with the host or just my server on the host (I'm assuming just my server)

blissful tartan
#

Change the response listener to rateLimited listener. And this looks like it already passed (was about 3 seconds only)

elder rose
#

Ok I changed that and I'm pretty sure it now works, it's editing them but it's just taking ages (like 5 seconds). The replies too late errors are also being handled now

#

Not sure if it's working with all commands though, I'm still testing that

#

It worked for a tiny bit and then I added both the rateLimited and response listeners and it said 429 again and won't edit. It like logs, says status code 204 then logs again without me putting in another command saying 429

#

When I use interaction.reply() and no defer it logs status code 204, no 429

So:
interaction.deferReply() and interaction.editReply() = status code 429
interaction.reply() = status code 204

blissful tartan
blissful tartan
elder rose
blissful tartan
elder rose
#

It's not a VPS, no, it's a host that uses shared nodes with Pterodactyl

blissful tartan
#

Just looked into it, doesn’t look like something I‘d trust

#

So you get ratelimited because of all the other bots running on the same machine

#

Nothing to do with djs

elder rose
#

I've been using it for a while and it works fine. It only started happening last night. Also I checked and its the defer (method POST) gives 204, edit (method PATCH) gives 429

blissful tartan
#

Then probably someone started a new bot on that node that spams the API too much

elder rose
#

I'll check in the host's support server and see if they can have a look

blissful tartan
#

That’s the problem with shared hosts like that…

elder rose
#

Yeah, any other free host I've tried doesn't work too well but obviously paid would be way better

blissful tartan
#

Well, nothing’s free in this world… so if it tells you it’s free there’s always a catch

elder rose
#

yeah. I could look into some other hosts but this one's been this best one I've tried so far. Well thanks for your help and I'll ask in their server to check for any abusers/api spammers

formal briar
#

i don't seem to be rate limited

#

i don't get it

blissful tartan
#

Okay? How is that relevant to them getting ratelimited?

formal briar
#

oh, you didn't follow that we were having similar problems, right

#

it's not your job to know everything

#

i thought whatever solution they got would be applicable to me as well, but no luck so far

#

all i get are 404s for interactions not existing, but messages get posted just fine

blissful tartan
#

Messages as in the interaction‘s replies? Or regular Channel#send() ones?

formal briar
#

if i can't find the fix for this problem, I'll be forced to answer every interaction with 'interaction.channel.send()' instead of 'interaction.reply()'

strong juniper
#

man these errors are getting on my nerves

#

it was working very smoothly again smh but till this night

#

can't we have a proper permanent solution?

humble stone
#

reply sooner

formal briar
#

i'm rewriting my bot to not try replying to the interactions, it will just send messages in chat instead

strong juniper
#

maybe that will be the best option i see

digital root
#

That is API spam

strong juniper
digital root
#

It spams api requests

strong juniper
#

but how could i achieve with discord.js

#

i am too noob to get what you're saying tho

digital root
#

It spams API with requests, which we will not support

strong juniper
#

oh

strong juniper
#

how i can make it reply sooner when it is generating the response by itself

elder rose
#

You can't always just 'reply sooner'. Some stuff like using an API to get stuff or image modifiers or something like that might just take longer than the 3 seconds non-deferred replies get, especially if the bot is a bit slow at the time. Although generally this shouldn't be an issue if you have relatively basic commands

formal briar
#

if you reply or defer at the very top of your command, you won't have issues. problem just arises when not even that is enough, like my case

strong juniper
#

That's what I am having it works sometime but not most of the time

formal briar
#

my issue went away entirely 4 days ago so i've just reverted back to interactions