@discord-interactions/request

中文文档
0.3.21发布于 2024/08/29Apache-2.0TypeScriptES Module近 30 天下载 3302

A Discord HTTP client that handles global and resource rate limits automatically

原始使用文档

discord-request

Discord server npm version npm downloads Tests status

A Discord HTTP client that handles global and resource rate limits automatically.

Looking for an API Wrapper? Check out @discord-interactions/api instead. This package is low level, and works best in libraries as opposed to applications.

Usage

This package works best in conjuction with discord-api-types. To begin, install both:

npm install discord-api discord-api-types

Then you can combine the two (with or without typing the results):

import Client from "discord-request";
import {
    Routes,
    RESTGetAPIApplicationGuildCommandsResult,
} from "discord-api-types/v10";

const instance = new Client();

const guildCommands = instance.get(
    Routes.applicationGuildCommands(applicationId, guildId)
) as Promise<RESTGetAPIApplicationGuildCommandsResult>;

Options

When creating a new client, you can configure it by passing in optional arguments:

const instance = new Client({
    retries: 0,
    timeout: 1000,
    globalRequestsPerSecond: 100,
    onRateLimit: (data) => console.log({ data }),
});

Every parameter listed below is optional.

  • api: The API URL to use. Defaults to https://discord.com/api.
  • version: The API version to use. Defaults to 10.
  • cdn: The CDN URL to use. Defaults to https://cdn.discordapp.com.
  • headers: An object of additional headers to send with each request.
  • userAgent: The user agent to use. Defaults to Discord Request v0.
  • retries: The number of times to retry a request if it fails. Defaults to 3.
  • timeout: The number of milliseconds to wait before timing out a request. Defaults to 15000 (15 seconds).
  • globalRequestsPerSecond: The number of global requests per second to allow. Defaults to 50.
  • shutdownSignal: An AbortSignal to use when you need to cancel all unfinished requests to shut down the application.
  • queueSweepInterval: The number of milliseconds to wait between sweeping the queue. Defaults to 0 (no sweeping). If you use discord-request in a persistent environment, you should set this value.
  • bucketSweepInterval: The number of milliseconds to wait between sweeping the buckets. Defaults to 0 (no sweeping). If you use discord-request in a persistent environment, you should set this value.
  • onBucketSweep: See callbacks below.
  • onQueueSweep: See callbacks below.
  • onRateLimit: See callbacks below.
  • onRequest: See callbacks below.

Callbacks

onBucketSweep?: (swept: Map<string, Bucket>) => void;

Runs when a bucket sweep finishes. Returns a Map of removed buckets.

onQueueSweep?: (swept: Map<string, Queue>) => void;

Runs when a queue sweep finishes. Returns a Map of removed queues.

onRateLimit?: (data: RateLimitData) => void;

Runs when a rate limit is encountered. Returns information about the rate limit.

onRequest?: (parameters: Route, resource: string, init: RequestInit, options: RequestOptions, retries: number) => void;

Runs when a request is sent to the Discord API. Returns information used to send the request.

Credits

This code is derived from @IanMitchell's Interaction Kit: discord-request & discord-error and distributed under the Apache 2 license. It was changed to merge these packages together.

版本与发布记录

@discord-interactions/request 共发布 13 个版本,首次发布于 2022/07/10;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.3.21latest2024/08/292 年前
0.3.182024/03/132 年前
0.3.162023/03/083 年前
0.3.82022/11/183 年前
0.3.72022/11/173 年前
0.3.62022/08/164 年前

关于 @discord-interactions/request 的常见问题

@discord-interactions/request 是什么?

A Discord HTTP client that handles global and resource rate limits automatically

如何安装 @discord-interactions/request?

在项目目录运行 npm install @discord-interactions/request,或使用 pnpm add / yarn add / bun add。

@discord-interactions/request 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@discord-interactions/request 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@discord-interactions/request 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=16。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@discord-interactions/request 可以商用吗?

@discord-interactions/request 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@discord-interactions/request 的下载量有多少?

根据 npm 官方下载统计,@discord-interactions/request 在 2026/10/13 – 2026/11/11 这 30 天内被下载 3,302 次,日均约 110 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@discord-interactions/request 还在维护吗?

最新版本 0.3.21 发布于 2024/08/29(2 年前);至今共发布 13 个版本,其中过去 12 个月发布了 0 个;首次发布于 2022/07/10。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。