-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Description
Description
When deploying an HTTP interactions bot on Cloudflare Workers, making a fetch() request to Discord (using the POST method) from a worker does not resolve. The Promise returned by fetch() hangs indefinitely - even though the request is sent (e.g. deferring results in the user seeing the loading state).
This means, for interaction callbacks or any other Discord requests, the callback must be the last line in your worker's fetch() handler, or Cloudflare Workers cannot be reliably used for HTTP interactions bots.
After investigating, it appears this occurs because both Discord and Cloudflare utilize Cloudflare infrastructure (possibly Workers). This issue has been referenced before (see: #7146 ), but this wasn't the subject of that issue - still, it persists.
Recommendation:
- Update docs to note that HTTP interaction bots on Cloudflare Workers are not recommended except for single, final interaction callbacks (the callback is the last line before the worker's fetch handler is completed).
- OR remove the CF Workers example for HTTP interactions altogether.
Steps to Reproduce
- Deploy an HTTP interactions bot to Cloudflare Workers (Can be the example from the docs).
- In your worker, attempt to send an interaction callback with a
fetch()POST request to Discord in a single invocation of thefetch()event handler. - Observe that the interaction callback POST goes through, however the Promise returned by the
fetch()doesn't resolve; any line of code afterfetch()isn't executed; this blocks further execution. - Alternatively, test the same code locally (using ngrok and worker dev mode on your machine): all requests resolve properly and you can have additional logic after e.g. deferring the interaction and editing it later.
Example (JS):
// `interaction` is obtained through the discord-interactions-js package verifying the payload and then parsing the body (like the example from the docs)
const deferRes = await fetch(`https://discord.com/api/v10/interactions/${interaction.id}/${interaction.token}/callback`, {
method: "POST",
headers: {
"Content-Type": "application/json",
authorization: `Bot ${env.DISCORD_TOKEN}`,
},
body: JSON.stringify({
type: InteractionResponseType.DeferredChannelMessageWithSource,
data: {
flags: 64,
},
}),
});
// This point isn't even reached when deployed on CF Workers
console.log("Defer response:", deferRes.status, inspect(Object.entries(deferRes.headers)));
await new Promise((resolve) => setTimeout(resolve, 1000)); // Simulate processing delay
const resRes = await fetch(
`https://discord.com/api/v10/webhooks/${interaction.application_id}/${interaction.token}/messages/@original?with_response=true`,
{
method: "PATCH",
headers: {
"Content-Type": "application/json",
authorization: `Bot ${env.DISCORD_TOKEN}`,
},
body: JSON.stringify({
content: "Done!",
}),
},
);
console.log("Edit response:", resRes.status, inspect(Object.entries(resRes.headers)));Expected Behavior
All HTTP POST requests to Discord from a Cloudflare Worker (using fetch()) should resolve their Promises and complete execution. Any POST in the worker's fetch event handler using fetch() to a Discord API endpoint should resolve.
Current Behavior
All HTTP POST requests to Discord from a Cloudflare Worker (using fetch()) go through, however the Promise returned by the fetch() doesn't resolve; therefore any line of code after the fetch() isn't executed.
Screenshots/Videos
(No screenshots available – issue is best reproduced through code and observable hanging behavior in worker logs.)
Client and System Information
Client: Client doesn't matter, I tested it in browser, on desktop and mobile
Platform: Cloudflare Workers
Testing locally: Worker dev mode (wrangler dev) + ngrok (requests resolve as expected)
Live deployment: Cloudflare Worker wrangler deploy (Discord POST requests hang)
Language: JavaScript/TypeScript
Discord API Version: 10
Discord API endpoints tested: POST interaction callback, POST message (create message)