Bot crashes if there's an interaction before it refreshes / commands
When my bot is trying to start up, if an interaction happens before it reloads global commands logs in, it will finish loading global commands and then crash again. Is there any info on what to do about this? It stays in a loop until it happens to finish reloadinglogin before an interaction happens
(the error is unknown interaction, if that helps)
Also, from what I understand, the error message connects to not responding within 3 seconds. However it can't reply if it is still trying to get online, right?
32 Replies
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
npm list discord.js = [email protected]
node -v = v17.9.0
I even tried using a command where I deferred the response but it still crashes it. What's going on?
it seems to be between login and ready (tends to take about 16 seconds). If an interaction happens between those points it will crash.
your bot is queueing the interaction until it is able to actually respond to it, what you should do is if you have to do anything before the bot actually can log in, then don't do it in the ready event, and login your client after the bot finishes with everything else it has to do
you can make a promise that takes care of the start up process and wait for it to resolve, then login your client so it connects to discord, or just structure your code so the last thing the bot does is logging in
right now in my index.js file the last thing it does is login, is that not structuring it so the last thing it does is log in? /gen. it runs the command handler then event handler, then logs in after that.
I really appreciate the response as well this issue is driving me crazy
hmmmm. I guess what you can try is check if
interaction.isRepliable()
although Im not sure if this works for this use casewhat do you mean in this message? " if you have to do anything before the bot actually can log in, then don't do it in the ready event, and login your client after the bot finishes with everything else it has to do"
At first i though one possiblity was that you were loading commands or doing some stuff after your bot entered the ready state but I realized that is just not the case
by the way if you get the error again can you share the console log of it?
yeah sure give me like 5 minutes because I'm testing out that interaction.isRepliable() idea too
sure np
alright here's the original problem:
I pressed a button to pull up the about menu before it logged in
hm... yea its because the interaction token already expired when your bot tried to reply
I mean, a kind of easy fix for this would be to just use a try catch block in your interaction handler, so if one interaction fails it doesn't crash your bot but you still get the info about the error in console
this only happens when your bot is starting up so...
I've found that most of my crashes are from small bugs that lurk in my functions, not from an interaction failing. Is it possible to just ignore interactions for the time it's booting up?
I just don't want to rely on my bot not crashing instead of fixing it because it may crash for other reasons and I want it to be able to get back online asap
I dont think you can really do that directly within the library, but you can try implementing a timer that just makes the handlers return if x time has not passed yet
I still don't really understand how are you getting interactions while your bot is "booting up", thats why i suggested to try and make the bot login when everything else was ready
yes I was actually confused on that too. Is it not normal?
through a promise that takes care of reading file or something that you can await and after that is done you call client.login
I mean discord only sends the interactions once
if you bot is offline I don't think it queues them
but at the same time I dont know how if you bot trying to reply to that interaction after x time has passed
a little weird
I think because the bot always stays online unless the digitalocean droplet crashes
I think even if I pm2 stop it it might stay online ngl
yup stays online
did you wait like a minute for it to disconnect
hmm a bit of time but I'll wait for longer
Okay ive stopped it now I'll wait a minute or two
Still online even if I click the profile
also let me get this right, when you say "booting up" you mean you stopped the process and then start it up again? or you mean like the bot is just re-reading files and doing something else while still being connected
ope there it goes
booting up as in it crashes and pm2 starts it back up
like when it "boots up" it logs:
Launched shard 0
app listening
(about 16 seconds later)
Started refreshing / commands
Reloaded / commands
Ah... okay then that makes a little more sense, discord is waiting for your bot to reconnect so it then replays events i guess, thats why you might be getting expired interactions. There is nothing much you can do about that specifically. You must locate the reason why your bot is crashing in the first place causing it to be restarted by pm2
also why does it take 16 seconds to refresh commands is it doing it automatically? you shouldn't do that, you should try only refreshing commands when you make changes to them
that's just what's in the ready event
yea, shouldn't do that, deploying commands should be on its own script or be manually triggered
im thinking I misread the
mostly because you are just wasting requests
oh actually I may have gotten it off a YT tutorial
well regardless I'll move the / refreshes to their own thing
okay but leaving that aside, do you have any idea what is crashing your bot in the first place? because one thing is getting unknown interactions when your bot is booting up, but you said that apparently is after your bot crashes for the first time, then what made it crash in the first place
well there's one bug I do know of, but like any time there's a bug in any of my functions it would crash
that is just bad error handling, you should use try {} catch blocks and handle errors properly. If your bot has an error it will not crash, it doesn't mean that you shouldn't do anything about that error but at least is not going to crash
oh okay gotcha
I gotta read up on those cuz I only have one for command interactions i cannot get it to work but I know my goal now is to just make it not crash, not to ignore interactions before it logs in. Thanks so much for your help!!
luck
you might not care too much but after setting up a proper error handler for everything, it even catches that unknown interaction error so it doesn't crash again. Now I have no crashes from bugs and fixed my initial question!