Configuration
Replace the parts of the HTTP client.
Reading time:2 min read
Every part of the client can be replaced through clientInjections (or injections, when calling HttpNyxClient.create() yourself):
| Injection | Default | Description |
|---|---|---|
verifier | DefaultSignatureVerifier | Verifies Discord's Ed25519 signatures. |
rest | A REST with the bot's token | The REST client used by api. |
api | An API over rest | The API passed to commands and subscribers. |
emitter | A BasicAwaitableEventEmitter | The emitter interactions and webhook events are emitted on. Its emit() resolves when the listeners finish, which is what handled waits for. |
acknowledgeTimeout | 3_000 | How long to wait for an interaction to be acknowledged, in milliseconds. |
maxTimestampAge | 300_000 | How old (or how far in the future) a request can be before it's rejected, in ms. |
import { } from '@nyx-discord/http';
const = .(() => ({
: .APP_ID,
: .DISCORD_PUBLIC_KEY,
: .TOKEN,
: ,
: true,
: {
: 2_500,
: 60_000,
},
}));Signature verification
The default verifier checks signatures with the Web Crypto API
(crypto.subtle), so it needs no dependencies and works on Node.js, Bun, Deno and Cloudflare Workers.
On runtimes without a global crypto.subtle, like older Node.js versions, pass one yourself:
import { } from 'node:crypto';
import { , } from '@nyx-discord/http';
const = .(() => ({
: .APP_ID,
: .DISCORD_PUBLIC_KEY,
: .TOKEN,
: ,
: true,
: {
: .({
: .DISCORD_PUBLIC_KEY,
: . as ,
}),
},
}));Or implement SignatureVerifier docs to use any other library. For example, with tweetnacl:
import from 'tweetnacl';
import { , type SignatureVerifier } from '@nyx-discord/http';
class implements SignatureVerifier {
protected readonly : ;
protected readonly = new ();
constructor(: string) {
this. = .(, 'hex');
}
public async (: string | , : string, : string) {
// Discord signs the timestamp followed by the raw body
const = typeof === 'string' ? this..() : ;
const = .([this..(), ]);
try {
return ...(, .(, 'hex'), this.);
} catch {
// tweetnacl throws on malformed signatures, which are just invalid requests
return false;
}
}
}
const = .(() => ({
: .APP_ID,
: .DISCORD_PUBLIC_KEY,
: .TOKEN,
: ,
: true,
: {
: new (.DISCORD_PUBLIC_KEY),
},
}));Edit on GitHub
Last updated on
