What is @discordjs/rest?
@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.
What are @discordjs/rest's main functionalities?
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);
}
})();
Other packages similar to @discordjs/rest
discord.js
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
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
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.
About
@discordjs/rest
is a module that allows you to easily make REST requests to the Discord API.
Installation
Node.js 16.9.0 or newer is required.
Note: native fetch (not recommended) is unavailable in this node version, either use a newer node version or use the more performant undiciRequest
strategy (default)
npm install @discordjs/rest
yarn add @discordjs/rest
pnpm add @discordjs/rest
Examples
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
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);
}
Links
Contributing
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.
Help
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.