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
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
BradyDaLlama .*˖
npm list discord.js = discord.js@14.7.1 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.
ShompiFlen
ShompiFlen16mo ago
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
BradyDaLlama .*˖
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
ShompiFlen
ShompiFlen16mo ago
hmmmm. I guess what you can try is check if interaction.isRepliable() although Im not sure if this works for this use case
BradyDaLlama .*˖
what 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"
ShompiFlen
ShompiFlen16mo ago
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?
BradyDaLlama .*˖
yeah sure give me like 5 minutes because I'm testing out that interaction.isRepliable() idea too
ShompiFlen
ShompiFlen16mo ago
sure np
BradyDaLlama .*˖
alright here's the original problem:
0|index | /root/discord/node_modules/discord.js/node_modules/@discordjs/rest/dist/index.js:667
0|index | throw new DiscordAPIError(data, "code" in data ? data.code : data.error, status, method, url, requestData);
0|index | ^
0|index | DiscordAPIError[10062]: Unknown interaction
0|index | at SequentialHandler.runRequest (/root/discord/node_modules/discord.js/node_modules/@discordjs/rest/dist/index.js:667:15)
0|index | at processTicksAndRejections (node:internal/process/task_queues:96:5)
0|index | at async SequentialHandler.queueRequest (/root/discord/node_modules/discord.js/node_modules/@discordjs/rest/dist/index.js:464:14)
0|index | at async REST.request (/root/discord/node_modules/discord.js/node_modules/@discordjs/rest/dist/index.js:910:22)
0|index | at async ButtonInteraction.update (/root/discord/node_modules/discord.js/src/structures/interfaces/InteractionResponses.js:233:5) {
0|index | requestBody: {
0|index | files: [],
0|index | json: {
0|index | type: 7,
0|index | data: {
0|index | content: undefined,
0|index | tts: false,
0|index | nonce: undefined,
0|index | embeds: [
0|index | {
0|index | color: 16571531,
0|index | title: 'About Llama Loot RPG',
0|index | description: 'Created by: BradyDaLlama',
0|index | thumbnail: [Object],
0|index | fields: [Array]
0|index | }
0|index | ],
0|index | components: [ { type: 1, components: [Array] } ],
0|index | username: undefined,
0|index | avatar_url: undefined,
0|index | allowed_mentions: undefined,
0|index | flags: undefined,
0|index | message_reference: undefined,
0|index | attachments: undefined,
0|index | sticker_ids: undefined,
0|index | thread_name: undefined
0|index | }
0|index | }
0|index | },
0|index | rawError: { message: 'Unknown interaction', code: 10062 },
0|index | code: 10062,
0|index | status: 404,
0|index | method: 'POST',
0|index | url: 'https://discord.com/api/v10/interactions/1076739657343184957/aW50ZXJhY3Rpb246MTA3NjczOTY1NzM0MzE4NDk1NzpkUEI3NEZ2ZUdkTnhtbVVZejlYYndOdjNLTlVvSE9aUXdENjhZZ0Q1ckQxNktpdWtZWVRib2lodUdZS2JHZXlwc2F5blIzUG9yNUNNUWJGY2EzemR6OWg2TW1SMVJSWkRkMlZORkpsMUlNOGV6RmJnb25qdmtPTndXOE1ET0hwMg/callback'
0|index | }
0|index | Node.js v17.9.0
0|index | /root/discord/node_modules/discord.js/node_modules/@discordjs/rest/dist/index.js:667
0|index | throw new DiscordAPIError(data, "code" in data ? data.code : data.error, status, method, url, requestData);
0|index | ^
0|index | DiscordAPIError[10062]: Unknown interaction
0|index | at SequentialHandler.runRequest (/root/discord/node_modules/discord.js/node_modules/@discordjs/rest/dist/index.js:667:15)
0|index | at processTicksAndRejections (node:internal/process/task_queues:96:5)
0|index | at async SequentialHandler.queueRequest (/root/discord/node_modules/discord.js/node_modules/@discordjs/rest/dist/index.js:464:14)
0|index | at async REST.request (/root/discord/node_modules/discord.js/node_modules/@discordjs/rest/dist/index.js:910:22)
0|index | at async ButtonInteraction.update (/root/discord/node_modules/discord.js/src/structures/interfaces/InteractionResponses.js:233:5) {
0|index | requestBody: {
0|index | files: [],
0|index | json: {
0|index | type: 7,
0|index | data: {
0|index | content: undefined,
0|index | tts: false,
0|index | nonce: undefined,
0|index | embeds: [
0|index | {
0|index | color: 16571531,
0|index | title: 'About Llama Loot RPG',
0|index | description: 'Created by: BradyDaLlama',
0|index | thumbnail: [Object],
0|index | fields: [Array]
0|index | }
0|index | ],
0|index | components: [ { type: 1, components: [Array] } ],
0|index | username: undefined,
0|index | avatar_url: undefined,
0|index | allowed_mentions: undefined,
0|index | flags: undefined,
0|index | message_reference: undefined,
0|index | attachments: undefined,
0|index | sticker_ids: undefined,
0|index | thread_name: undefined
0|index | }
0|index | }
0|index | },
0|index | rawError: { message: 'Unknown interaction', code: 10062 },
0|index | code: 10062,
0|index | status: 404,
0|index | method: 'POST',
0|index | url: 'https://discord.com/api/v10/interactions/1076739657343184957/aW50ZXJhY3Rpb246MTA3NjczOTY1NzM0MzE4NDk1NzpkUEI3NEZ2ZUdkTnhtbVVZejlYYndOdjNLTlVvSE9aUXdENjhZZ0Q1ckQxNktpdWtZWVRib2lodUdZS2JHZXlwc2F5blIzUG9yNUNNUWJGY2EzemR6OWg2TW1SMVJSWkRkMlZORkpsMUlNOGV6RmJnb25qdmtPTndXOE1ET0hwMg/callback'
0|index | }
0|index | Node.js v17.9.0
I pressed a button to pull up the about menu before it logged in
ShompiFlen
ShompiFlen16mo ago
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...
BradyDaLlama .*˖
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
ShompiFlen
ShompiFlen16mo ago
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
BradyDaLlama .*˖
yes I was actually confused on that too. Is it not normal?
ShompiFlen
ShompiFlen16mo ago
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
BradyDaLlama .*˖
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
ShompiFlen
ShompiFlen16mo ago
did you wait like a minute for it to disconnect
BradyDaLlama .*˖
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
ShompiFlen
ShompiFlen16mo ago
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
BradyDaLlama .*˖
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
ShompiFlen
ShompiFlen16mo ago
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 ASthink is it doing it automatically? you shouldn't do that, you should try only refreshing commands when you make changes to them
BradyDaLlama .*˖
that's just what's in the ready event
ShompiFlen
ShompiFlen16mo ago
yea, shouldn't do that, deploying commands should be on its own script or be manually triggered
BradyDaLlama .*˖
im thinking I misread the Thqnkqng
ShompiFlen
ShompiFlen16mo ago
mostly because you are just wasting requests
BradyDaLlama .*˖
oh actually I may have gotten it off a YT tutorial well regardless I'll move the / refreshes to their own thing
ShompiFlen
ShompiFlen16mo ago
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
BradyDaLlama .*˖
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
ShompiFlen
ShompiFlen16mo ago
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
BradyDaLlama .*˖
oh okay gotcha I gotta read up on those cuz I only have one for command interactions monbreyFacepalm 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!! bradyd5Salute
ShompiFlen
ShompiFlen16mo ago
okay luck
BradyDaLlama .*˖
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! WurmpleParty