Skip to content

Cloudflare Workers

@discord0/adapter-cloudflare is the default adapter. discord0 deploy bundles your app as a module Worker and uploads it with Wrangler.

Terminal window
npx discord0 init my-bot # or: --adapter cloudflare

.discord0/cloudflare contains worker.mjs, a package.json, and a wrangler.json that:

  • names the Worker after your package.json name,
  • enables nodejs_compat, which exposes secrets through process.env, and
  • serves the Worker on its *.workers.dev URL.

Workers always deploy live, so --prod has no effect. Wrangler asks you to log in on the first deploy.

Terminal window
npx wrangler secret put DISCORD_PUBLIC_KEY --name my-bot
npx wrangler secret put DISCORD_BOT_TOKEN --name my-bot

Your endpoint is https://my-bot.<your-subdomain>.workers.dev/interactions, which deploy prints.

If you deploy the Worker yourself with your own Wrangler config, you can read bindings explicitly instead of through process.env. Pass cloudflare() a factory, and make the file that exports it the Worker’s main. discord0 deploy doesn’t support this form. It wraps your entry point’s default export in cloudflare() again, so the factory never gets the bindings.

The factory runs on each request, so cache the app if you want one Worker isolate to reuse its in-memory rate-limit state. To share rate limits across isolates, pass a shared rateLimitStore, as in examples/rate-limit-store. This factory caches the app in a module variable:

import { cloudflare } from "@discord0/adapter-cloudflare";
import { Discord } from "discord0";
interface Env {
DISCORD_PUBLIC_KEY: string;
DISCORD_BOT_TOKEN: string;
}
let app: Discord | undefined;
export default cloudflare<Env>((env) => {
app ??= new Discord({
publicKey: env.DISCORD_PUBLIC_KEY,
token: env.DISCORD_BOT_TOKEN,
});
return app;
});

The adapter passes deferred work to the Worker’s ctx.waitUntil().

Worker cold starts are typically tens of milliseconds, so the adapter keeps the global 2000ms autoDeferAfterMs default when you leave it unset. Set autoDeferAfterMs on new Discord() to override. See Deferred work.

  • 30 seconds after the response. waitUntil extends a request for up to 30 seconds after the response is sent. Promises still pending after that are cancelled. The 30 seconds are shared by all waitUntil work in the request. The app responds when it defers, 2 seconds in by default, so a slow handler has about 32 seconds in total.
  • CPU time. Workers on the Free plan get 10 ms of CPU time per request. Paid plans default to 30 seconds and can raise it to 5 minutes with limits.cpu_ms. Waiting on fetch() and other I/O doesn’t count toward CPU time, so most handlers that wait on APIs use very little. (Limits)
  • HTTP-triggered Workers have no wall-clock duration limit of their own. (Limits)

For work that takes longer than 30 seconds, send it to Cloudflare Queues or Workflows and edit the reply from there within Discord’s 15-minute window.

Each scheduled task’s expression becomes a Cron Trigger in wrangler.json, and the Worker’s scheduled() handler runs every task on the trigger that fired. Cron Triggers run in UTC, so tasks must use UTC. Tasks that share an expression share one trigger. Workers Free allows 5 Cron Triggers per account and 10 ms of CPU per run, which a task that does much work can exceed. Workers Paid allows 1,000. Trigger changes can take up to 15 minutes to apply.