unknown webhook errors discord http bot
Hey guys,
I have a http only bot running on a cf worker and im running into random Unknown webhook problems, mainly for my /meme command, and I have no idea what's wrong or where to go to fix it.
Here's the code for my /meme command: https://sourceb.in/yrwYOIxtyS
The command "handler": https://sourceb.in/pzY6NMCBQt
In my index.ts I just have this https://sourceb.in/n6o9OX2kRH which is just a switch case over all of the types, simple stuff.
I believe it's because im trying to "editReply" before the deferReply has "resolved", but im wondering what I can actually do to help this. I have a button too, which this happens with, basically the same code though.
Thanks.
20 Replies
what's the exact error?
From discord it’s just the code and the message being unknown webhook, let me grab it again
{ message: 'Unknown Webhook', code: 10015 }
Idk if I’ve done something funky in my project, probably that
that sounds like your webhook url is invalid
It doesnt happen all of the time either. One of the mods in ddevs said it could be the worker taking too long to respond but idk
If it was invalid it would happen all the time?
const webhookUrl = https://discord.com/api/v10${Routes.webhookMessage(env.discord_application_id, interaction.token, '@original')}
;
so i would print this out and check what it's returningI removed the “env.discord_application_id” and pasted it with my app id for testing and got the same thing, so I’m sure it isn’t that
Routes is from discord-api-types to make the url
(log) https://discord.com/api/v10/webhooks/1170832155958587423/aW50ZXJhY3Rpb246MTI3MzcxMTI5NjI1NjI4MjcwNTpkUHFxQkFKdmFYTmEyYzE2cHlHZDF4d285QjBMTnZNRmRpWFdRcFRSSlA4UGRzYlhNOEpZSHh1Q2QwdDcyVjN1Zm5wMUJUd0lnZlFvZzVCajBjUVNMUDNhRTR4WWtxd1pEVnp6V1VSd2hna2x2cDJPN2ZJalZGY
https://sourceb.in/ebNpFe9bQ4 this is my entire router.post for interactions too if it helpsWhat do you see on Discord? Do you see the message like "App is thinking..." or something similar?
Yh, it does successfully defer and then I get the unknown webhook error
The fact that you're saying that it sometimes works confuses me
I think that something is wrong with the fetch calls
Like, you're not always passing the content type header and never passing your token
tbh I would suggest you using @discordjs/rest to make API calls to discord as it handles everything
What I do is creating a
rest.ts
file with this content (the hash and handler options were required to make it work in Workers, not sure if they've fixed it now):
And at the start of the fetch
server handler I call rest.setToken(env.DISCORD_TOKEN)
. Then I use the rest whenever I need it by importing it, like in my ping command:
You don’t need to pass the token for an editReply,
https://github.com/discordjs/discord.js/blob/main/packages/discord.js/src/structures/Webhook.js#L341
Ig the only thing I can see now is that they pass the interaction id?
GitHub
discord.js/packages/discord.js/src/structures/Webhook.js at main · ...
A powerful JavaScript library for interacting with the Discord API - discordjs/discord.js
Nvm they don’t it’s still the app id
https://github.com/discordjs/discord.js/blob/main/packages/discord.js/src/structures/InteractionWebhook.js#L9 looking at the wrong one
GitHub
discord.js/packages/discord.js/src/structures/InteractionWebhook.js...
A powerful JavaScript library for interacting with the Discord API - discordjs/discord.js
I mean I can try using that, I was just going more simply and using fetch
Mmh, it's strange because I just tried and it gave me an error about not passing the token
With the editReply route? I mean either way, that’s not my issue since it doesn’t error either unauthorised
I mean I can pop it in there
I need to spin up some code using a library or something, my handling is probably wrong
Also would be good to point out that it only ever happens when you spam it. It seems to happen in servers more often then it does in the dms of the bot
ah
then maybe the defer takes too long to arrive and the edit is impossible as the webhook is deleted after 3 seconds if you don't defer in time
But aren’t cf workers like hella quick on getting the response out? That’s basically the only thing I can think of that makes sense here. The webhook token is invalid 15 mins, or you edit before the defer has been “validated”? That’s why you await a deferReply if you’re using something like djs
It sounds possible that the worker isn’t scaling at the request load you get, causing delays in acking the newer requests coming out of discord. (I’m not familiar with how workers scale.) I didn’t experience that exactly but had some similar response issues, I ended up splitting my bot into two workers, one that acks then passes the interaction to a queue and another that consumes the queue messages and actually processes the interaction
I'm not sure if there is a limit of how many requests can be handled at the same time, because in that case maybe the worker may be waiting for other contexts to be closed before starting to handle the request
I may see if using a library helps. As maybe I’ve done something wrong. Just a bit busy atm but I appreciate all of your responses!