discord/cloudflare-sample-app

Deferring interactions

Opened this issue · 3 comments

First of all, thank you very much for sharing this example with Cloudflare Workers, it is a great start for building interaction-based apps.

Now, sometimes we may need to do additional operations in our command, which would exceed the limit of 3 seconds for an interaction response. To prevent the interaction from failing, we can defer it by sending a deferred channel message with source payload and then we can do our asynchronous magic under the hood and edit the response when we're ready.
Sadly, Cloudflare seems to stop any further asynchronous action after a response has been sent.

So, do you have any idea about how to defer an interaction or is it just not possible?
Thank you so much!

For anyone wondering, I solved this problem by using context.waitUntil(). Would love to see an example of this pretty common use case in the repository!

Can you explain how you used the waitUntil? Looking to use it for my bot as well that has a slow backend API call.

Can you explain how you used the waitUntil? Looking to use it for my bot as well that has a slow backend API call.

This is how I implemented it in response to a user interacting with a button:

router.post('/', async (request, env, ctx) => {
  if (type === InteractionType.MESSAGE_COMPONENT) {
      const response = new JsonResponse({
          type: InteractionResponseType.UPDATE_MESSAGE,
          data: { content, components: [] }, // set empty components array to remove buttons
      });

      ctx.waitUntil(cloudflareWorker(env, interaction, data));

      return response;
  }
}

I also made sure to edit the interaction message within my cloudflareWorker function.