jack3898 / discord-youtube-bot-2

A powerful Discord YouTube Bot.
0 stars 0 forks source link

Crash by "DiscordAPIError: Invalid Webhook Token" #10

Closed DheathNone closed 2 years ago

DheathNone commented 2 years ago

When using the bot commands the bot seems to crash randomly.

Logs:

...
bot      | 2022-02-07T19:51:19.421941567Z Started refreshing application slash commands.
bot      | 2022-02-07T19:52:00.609401585Z 19 unique commands have been found on the file system.
bot      | 2022-02-07T19:52:00.991632802Z Successfully reloaded application slash commands.
bot      | 2022-02-07T19:59:42.110835235Z WARNING: ytdl-core is out of date! Update with "npm install ytdl-core@latest".
bot      | 2022-02-07T20:59:14.470198592Z /home/node/node_modules/discord.js/src/rest/RequestHandler.js:350
bot      | 2022-02-07T20:59:14.470548327Z       throw new DiscordAPIError(data, res.status, request);
bot      | 2022-02-07T20:59:14.470623274Z             ^
bot      | 2022-02-07T20:59:14.506939730Z DiscordAPIError: Invalid Webhook Token
bot      | 2022-02-07T20:59:14.507166655Z     at RequestHandler.execute (/home/node/node_modules/discord.js/src/rest/RequestHandler.js:350:13)
bot      | 2022-02-07T20:59:14.507232800Z     at runMicrotasks (<anonymous>)
bot      | 2022-02-07T20:59:14.507298840Z     at processTicksAndRejections (node:internal/process/task_queues:96:5)
bot      | 2022-02-07T20:59:14.507363996Z     at async RequestHandler.push (/home/node/node_modules/discord.js/src/rest/RequestHandler.js:51:14)
bot      | 2022-02-07T20:59:14.507498786Z     at async InteractionWebhook.editMessage (/home/node/node_modules/discord.js/src/structures/Webhook.js:325:15)
bot      | 2022-02-07T20:59:14.507563577Z     at async CommandInteraction.editReply (/home/node/node_modules/discord.js/src/structures/interfaces/InteractionResponses.js:139:21)
bot      | 2022-02-07T20:59:14.507625242Z     at async EventEmitter.<anonymous> (/home/node/src/commands/modules/Controls.ts:40:6) {
bot      | 2022-02-07T20:59:14.507687898Z   method: 'patch',
bot      | 2022-02-07T20:59:14.507751126Z   path: '/webhooks/928362049258520576/aW50ZXJhY3Rpb246OTQwMzQzOTQzMzk1MTgwNTk0Ong4WUhGb2FwZ2U1RGU3N3JpMW53UXFOWXVhMmhnTlV2TzlRTGVOUEdWbnh6OWFMZGxzZWtQN3dHZFkyMUh0bkZES09pOTlCYldpck03RUx1czdvaVJvcnBxY21BUUdKTXNXTEpGMm5nNW14aXdIVFBSbkQ0Z2QxdDJDaHhEYnkz/messages/@original',
bot      | 2022-02-07T20:59:14.507817740Z   code: 50027,
bot      | 2022-02-07T20:59:14.507875552Z   httpStatus: 401,
bot      | 2022-02-07T20:59:14.507935082Z   requestData: {
bot      | 2022-02-07T20:59:14.507992633Z     json: {
bot      | 2022-02-07T20:59:14.508051279Z       content: undefined,
bot      | 2022-02-07T20:59:14.508108257Z       tts: false,
bot      | 2022-02-07T20:59:14.508167215Z       nonce: undefined,
bot      | 2022-02-07T20:59:14.508224870Z       embeds: [Array],
bot      | 2022-02-07T20:59:14.508283984Z       components: [],
bot      | 2022-02-07T20:59:14.508345754Z       username: undefined,
bot      | 2022-02-07T20:59:14.508409868Z       avatar_url: undefined,
bot      | 2022-02-07T20:59:14.508468669Z       allowed_mentions: undefined,
bot      | 2022-02-07T20:59:14.508528512Z       flags: undefined,
bot      | 2022-02-07T20:59:14.508586115Z       message_reference: undefined,
bot      | 2022-02-07T20:59:14.508648562Z       attachments: undefined,
bot      | 2022-02-07T20:59:14.508706947Z       sticker_ids: undefined
bot      | 2022-02-07T20:59:14.508766478Z     },
bot      | 2022-02-07T20:59:14.508823091Z     files: []
bot      | 2022-02-07T20:59:14.508879862Z   }
bot      | 2022-02-07T20:59:14.508938298Z }

Note: Running under Rasberry pi 3 and it seems to be mostly working.

jack3898 commented 2 years ago

Hi @DheathNone thanks for the issue!

From the error report this could be a discord.js issue. Could you post an issue on their GitHub at https://github.com/discordjs/discord.js instead? I think they could be of more help than me.

If anything crops up that is a coding issue on my bot, please let me know here!

I will keep this issue open for as long as possible.

DheathNone commented 2 years ago

The answer from there was basically that this is because of editing a interaction reply that is already dead or non existent. Exactly similar error happens with Controls.ts:37:29 instead of Controls.ts:40:6. At least in this case the error happens when changing songs. Approximately once per 4 songs.

jack3898 commented 2 years ago

Can you give me instructions on how to replicate this issue? :)

Jack

DheathNone commented 2 years ago

It seems that 15 minutes is the limit here. This is fine:

/play query: 14 min song
/play query: tutturuu

This fails always:

/play query: 15 min song
/play query: tutturuu

Here the commands are send together and when the next song is going to start the bot fails at editReply.

I happened to stumble upon the Discord js documentation when looking into this: https://discordjs.guide/interactions/replying-to-slash-commands.html#editing-responses After the initial response, an interaction token is valid for 15 minutes, so this is the timeframe in which you can edit the response and send follow-up messages.

jack3898 commented 2 years ago

Thanks so much for the info!

I am looking into this now. I feel confident I can get a solution pushed soon. 🙂

jack3898 commented 2 years ago

Sorry for the delay, I have been busy recently!

My solution to this has been added in commit 1d3c1e7414582753fe7be0a049b67a393b579927

I could handle the message to get around the 15 minute timer, however, it started to get very hacky and would most likely open a can of worms when it comes to the stability of the bot. So, I just made it so that before the 15 minutes are up, any interactive messages will have its contents edited to display a notice of expiry. This was the best way to handle it in my opinion, even if it isn't the nicest.

Hopefully the solution is OK! :)

Feel free to reopen if any related issues arise.