Bun server
@discord0/adapter-bun builds a Bun.serve
server that you run yourself, on any machine or container that can serve
HTTPS.
npx discord0 init my-bot --adapter bunBuild and run
Section titled “Build and run”discord0 deploy builds .discord0/bun/index.js and syncs commands, but
doesn’t upload anything. Start the server from that directory:
cd .discord0/bunDISCORD_PUBLIC_KEY=... DISCORD_BOT_TOKEN=... bun index.jsIt listens on PORT, or 3000, on every interface. Discord connects only over
HTTPS, so put it behind a reverse proxy or load balancer that terminates TLS,
and set https://<your domain>/interactions as the Interactions Endpoint URL.
Use it in your own server
Section titled “Use it in your own server”bun(app) returns a fetch handler for Bun.serve, plus idle(), which
resolves once deferred work has finished:
import { bun } from "@discord0/adapter-bun";
import app from "./index";
const handler = bun(app);Bun.serve({ port: 3000, fetch: handler.fetch });serve(app, { port, hostname, signals, tasks }) starts a server with graceful
shutdown. The built index.js runs it.
Deferred work
Section titled “Deferred work”The server is a long-lived process, so deferred work keeps running after the response with no host limit. Discord’s 15-minute token lifetime is the only bound.
On SIGINT or SIGTERM, the server stops accepting requests, waits for
in-flight requests and deferred work to finish, then exits. A second signal
exits at once. Pass signals: false to serve() to handle signals yourself,
and call close() or the handler’s idle() to wait for pending work.
Deferred work lives in memory, so give your process manager a stop timeout longer than your slowest handler. Otherwise it kills the process before the work finishes:
docker stopwaits 10 seconds by default. Raise it with--timeorstop_grace_period. (docker stop)- Kubernetes waits
terminationGracePeriodSeconds, 30 seconds by default. (Pod termination) - systemd waits
TimeoutStopSec, 90 seconds by default. (systemd.service)
Scheduled tasks
Section titled “Scheduled tasks”serve() runs scheduled tasks with Bun.cron in
the server process, in each task’s time zone, while the server is up. Every
copy of the server runs every task, so if you run more than one, set
DISCORD0_TASKS=off in all but one, or pass tasks: false to serve(). On
shutdown, the server waits for running tasks to finish.