Product
Socket Now Supports uv.lock Files
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.
@discordjs/rest
Advanced tools
@discordjs/rest is a powerful library designed to facilitate interactions with the Discord API. It provides a streamlined way to make REST API requests to Discord, making it easier to build and manage Discord bots and applications.
Making REST API Requests
This feature allows you to make REST API requests to Discord. In this example, it fetches the bot's user information using the 'get' method.
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');
const rest = new REST({ version: '9' }).setToken('YOUR_BOT_TOKEN');
(async () => {
try {
const data = await rest.get(Routes.user('@me'));
console.log(data);
} catch (error) {
console.error(error);
}
})();
Handling Rate Limits
This feature helps in handling rate limits imposed by the Discord API. The example demonstrates how to listen for rate limit events and log the time to reset.
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');
const rest = new REST({ version: '9' }).setToken('YOUR_BOT_TOKEN');
rest.on('rateLimited', (info) => {
console.log(`Rate limited: ${info.timeToReset}ms`);
});
(async () => {
try {
const data = await rest.get(Routes.user('@me'));
console.log(data);
} catch (error) {
console.error(error);
}
})();
Posting Data
This feature allows you to post data to Discord. The example shows how to send a message to a specific channel using the 'post' method.
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');
const rest = new REST({ version: '9' }).setToken('YOUR_BOT_TOKEN');
(async () => {
try {
const data = await rest.post(Routes.channelMessages('CHANNEL_ID'), { body: { content: 'Hello, world!' } });
console.log(data);
} catch (error) {
console.error(error);
}
})();
discord.js is a powerful library for interacting with the Discord API. It provides a higher-level abstraction compared to @discordjs/rest, including features for managing WebSocket connections, handling events, and more. While @discordjs/rest focuses on REST API requests, discord.js offers a more comprehensive solution for building Discord bots.
Eris is another library for interacting with the Discord API. It is known for its performance and scalability, making it suitable for large bots. Like discord.js, Eris provides a higher-level abstraction and includes features for managing WebSocket connections and handling events. It also supports REST API requests, similar to @discordjs/rest.
discord-api-types is a package that provides TypeScript typings for the Discord API. While it does not handle REST API requests directly, it can be used in conjunction with @discordjs/rest to ensure type safety and better development experience when working with the Discord API.
@discordjs/rest
is a module that allows you to easily make REST requests to the Discord API.
Node.js 18 or newer is required.
npm install @discordjs/rest
yarn add @discordjs/rest
pnpm add @discordjs/rest
bun add @discordjs/rest
Install all required dependencies:
npm install @discordjs/rest discord-api-types
yarn add @discordjs/rest discord-api-types
pnpm add @discordjs/rest discord-api-types
bun add @discordjs/rest discord-api-types
Send a basic message:
import { REST } from '@discordjs/rest';
import { Routes } from 'discord-api-types/v10';
const rest = new REST({ version: '10' }).setToken(TOKEN);
try {
await rest.post(Routes.channelMessages(CHANNEL_ID), {
body: {
content: 'A message via REST!',
},
});
} catch (error) {
console.error(error);
}
Create a thread from an existing message to be archived after 60 minutes of inactivity:
import { REST } from '@discordjs/rest';
import { Routes } from 'discord-api-types/v10';
const rest = new REST({ version: '10' }).setToken(TOKEN);
try {
await rest.post(Routes.threads(CHANNEL_ID, MESSAGE_ID), {
body: {
name: 'Thread',
auto_archive_duration: 60,
},
});
} catch (error) {
console.error(error);
}
Send a basic message in an edge environment:
import { REST } from '@discordjs/rest';
import { Routes } from 'discord-api-types/v10';
const rest = new REST({ version: '10', makeRequest: fetch }).setToken(TOKEN);
try {
await rest.post(Routes.channelMessages(CHANNEL_ID), {
body: {
content: 'A message via REST from the edge!',
},
});
} catch (error) {
console.error(error);
}
Before creating an issue, please ensure that it hasn't already been reported/suggested, and double-check the
documentation.
See the contribution guide if you'd like to submit a PR.
If you don't understand something in the documentation, you are experiencing problems, or you just need a gentle nudge in the right direction, please don't hesitate to join our official discord.js Server.
FAQs
The REST API for discord.js
The npm package @discordjs/rest receives a total of 195,477 weekly downloads. As such, @discordjs/rest popularity was classified as popular.
We found that @discordjs/rest demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.
Research
Security News
Socket researchers have discovered multiple malicious npm packages targeting Solana private keys, abusing Gmail to exfiltrate the data and drain Solana wallets.
Security News
PEP 770 proposes adding SBOM support to Python packages to improve transparency and catch hidden non-Python dependencies that security tools often miss.