Node.js server
@discord0/adapter-node builds a Node.js HTTP server that you run yourself,
on any machine or container that can serve HTTPS.
npx discord0 init my-bot --adapter nodeBuild and run
Section titled “Build and run”discord0 deploy builds .discord0/node/index.mjs and syncs commands, but
doesn’t upload anything. Start the server from that directory:
cd .discord0/nodeDISCORD_PUBLIC_KEY=... DISCORD_BOT_TOKEN=... node index.mjsIt 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”To mount the app in an existing server, use node(app) as a request
listener:
import { createServer } from "node:http";
import { node } from "@discord0/adapter-node";
import app from "./index";
createServer(node(app)).listen(3000);In an Express-style server, mount it before any body parser, since the app verifies Discord’s signature against the raw body.
serve(app, { port, hostname, signals, tasks }) starts a server with graceful
shutdown. The built index.mjs 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 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.