Expand description
HTTP interactions (D-395, D-398–D-403): answering the interactions Discord posts to the app’s Interactions Endpoint URL with the same routes and handlers as a gateway bot.
An app with an Interactions Endpoint URL receives its commands, components, autocomplete
and modals as HTTP POST requests instead of gateway events, so it needs no long-lived
connection: it can run in any HTTP server, or on a serverless platform such as Cloudflare
Workers. Client::interactions_endpoint makes an
InteractionsEndpoint from a client; its handle takes
a request’s X-Signature-Ed25519 and X-Signature-Timestamp headers and its body, and
returns the HttpResponse to send back. There is no HTTP server in djs-rs: mount
handle in the one the bot already uses.
What handle does, in order:
- Verifies the request (Discord’s security
rules):
the Ed25519 signature of the timestamp and body under the app’s public key, and a
timestamp at most 5 minutes from now against replayed requests (D-401). Otherwise
401. - Answers a
PING(sent when the URL is saved in the Developer Portal) with aPONG. - Dispatches the interaction like a gateway
INTERACTION_CREATE: the cache, the event streams and collectors, theInteractionCreatehandlers, then the routes (Client::commandand the rest). - Returns as soon as a handler gives the initial response (
reply,defer_reply,update,show_modal, …): it becomes the HTTP body, with no REST request (D-399). The handlers’ remaining work (for example the follow-up after a deferral) is the returned future, which the server must run:tokio::spawnnatively,ctx.wait_untilon Cloudflare Workers.
| Status | When | Body |
|---|---|---|
200 | the initial response, or the PONG | its JSON (Content-Type: application/json) |
202 | the initial response had files: it went through REST instead | empty |
400 | the body is not an interaction | empty |
401 | the signature or the timestamp is invalid | empty |
500 | the handlers finished without an initial response | empty |
Every handler runs one after another for the request, whatever the client’s
HandlerMode. Gateway-only parts stay silent: no other event
reaches the handlers, and Context::shard is the guild’s shard
only if the client is also logged in (else a
detached sender, D-403). Collectors only see
the interactions this same client handles, so on a platform that spreads requests over
many instances they may never see the next one.
§On Cloudflare Workers (wasm32-unknown-unknown)
Build djs-rs with default-features = false, features = ["http-interactions"] and give
the client a Rest with a Transport over fetch (the
repository’s examples/cloudflare-workers is a complete bot). Handlers must be Send,
so a JS future awaited in one (a fetch, worker::Delay) is wrapped in
worker::send::SendFuture. tokio’s timers do not run there: collectors and the gateway
requests are not available, and a handler waits with the platform’s timers instead.
discord.js counterpart: none (discord.js only receives interactions over the gateway);
the official discord-interactions package has the verification (verifyKey).
§Examples
use djs_rs::prelude::*;
let client = Client::builder(std::env::var("DISCORD_TOKEN")?).build();
client.command("ping", |ctx, cmd| async move {
cmd.reply(&ctx, "Pong!").await?;
Ok(())
});
let endpoint = client.interactions_endpoint(&std::env::var("DISCORD_PUBLIC_KEY")?)?;
// In the HTTP server's `POST` handler, with the request's headers and body:
let (response, background) = endpoint.handle(signature, timestamp, body).await;
tokio::spawn(background);
// … answer with `response.status` and `response.body`.Structs§
- Http
Response - The response to an HTTP interaction request, returned by
InteractionsEndpoint::handle: sendstatusandbody, withContent-Type: application/jsonwhenbodyis not empty. - Interactions
Endpoint - Answers the interactions Discord posts to the app’s Interactions Endpoint URL with a
Client’s routes and handlers; made byClient::interactions_endpoint. Cheap to clone. - Invalid
Public Key - The error of
Client::interactions_endpoint: the public key is not 64 hexadecimal digits of a valid Ed25519 key.