2017-04-20 04:14:37 +12:00
< div align = "center" >
2022-07-20 04:26:03 +12:00
< br / >
< p >
< a href = "https://discord.js.org" > < img src = "https://discord.js.org/static/logo.svg" width = "546" alt = "discord.js" / > < / a >
< / p >
< br / >
< p >
< a href = "https://discord.gg/djs" > < img src = "https://img.shields.io/discord/222078108977594368?color=5865F2&logo=discord&logoColor=white" alt = "Discord server" / > < / a >
< a href = "https://www.npmjs.com/package/discord.js" > < img src = "https://img.shields.io/npm/v/discord.js.svg?maxAge=3600" alt = "npm version" / > < / a >
< a href = "https://www.npmjs.com/package/discord.js" > < img src = "https://img.shields.io/npm/dt/discord.js.svg?maxAge=3600" alt = "npm downloads" / > < / a >
< a href = "https://github.com/discordjs/discord.js/actions" > < img src = "https://github.com/discordjs/discord.js/actions/workflows/test.yml/badge.svg" alt = "Tests status" / > < / a >
< a href = "https://codecov.io/gh/discordjs/discord.js" > < img src = "https://codecov.io/gh/discordjs/discord.js/branch/main/graph/badge.svg?precision=2" alt = "Code coverage" / > < / a >
< / p >
< p >
2022-07-24 09:38:49 +12:00
< a href = "https://vercel.com/?utm_source=discordjs&utm_campaign=oss" > < img src = "https://raw.githubusercontent.com/discordjs/discord.js/main/.github/powered-by-vercel.svg" alt = "Vercel" / > < / a >
2022-07-20 04:26:03 +12:00
< / p >
2017-04-20 04:14:37 +12:00
< / div >
## About
2020-03-01 02:35:57 +13:00
2019-02-10 12:53:47 +13:00
discord.js is a powerful [Node.js ](https://nodejs.org ) module that allows you to easily interact with the
2020-06-19 21:46:59 +12:00
[Discord API ](https://discord.com/developers/docs/intro ).
2017-04-20 04:14:37 +12:00
- Object-oriented
- Predictable abstractions
- Performant
- 100% coverage of the Discord API
## Installation
2020-03-01 02:35:57 +13:00
2022-01-08 09:03:00 +13:00
**Node.js 16.9.0 or newer is required.**
2021-06-12 08:30:09 +12:00
```sh-session
npm install discord.js
2021-08-11 02:27:06 +12:00
yarn add discord.js
pnpm add discord.js
2021-06-12 08:30:09 +12:00
```
2017-04-20 04:14:37 +12:00
### Optional packages
2020-03-01 02:35:57 +13:00
2020-02-14 10:48:36 +13:00
- [zlib-sync ](https://www.npmjs.com/package/zlib-sync ) for WebSocket data compression and inflation (`npm install zlib-sync`)
2020-06-19 21:46:29 +12:00
- [erlpack ](https://github.com/discord/erlpack ) for significantly faster WebSocket data (de)serialisation (`npm install discord/erlpack`)
2019-12-16 08:45:27 +13:00
- [bufferutil ](https://www.npmjs.com/package/bufferutil ) for a much faster WebSocket connection (`npm install bufferutil`)
- [utf-8-validate ](https://www.npmjs.com/package/utf-8-validate ) in combination with `bufferutil` for much faster WebSocket processing (`npm install utf-8-validate`)
2022-01-10 07:35:18 +13:00
- [@discordjs/voice ](https://www.npmjs.com/package/@discordjs/voice ) for interacting with the Discord Voice API (`npm install @discordjs/voice `)
2017-04-20 04:14:37 +12:00
## Example usage
2020-03-01 02:35:57 +13:00
2021-08-11 02:25:59 +12:00
Install all required dependencies:
2022-01-08 05:18:25 +13:00
2021-08-11 02:25:59 +12:00
```sh-session
2022-06-14 07:42:28 +12:00
npm install discord.js @discordjs/rest
yarn add discord.js @discordjs/rest
pnpm add discord.js @discordjs/rest
2021-08-11 02:25:59 +12:00
```
Register a slash command against the Discord API:
2022-01-08 05:18:25 +13:00
2021-08-02 03:58:41 +12:00
```js
const { REST } = require('@discordjs/rest');
2022-06-14 07:42:28 +12:00
const { Routes } = require('discord.js');
2021-08-02 03:58:41 +12:00
2022-01-08 05:18:25 +13:00
const commands = [
{
name: 'ping',
description: 'Replies with Pong!',
},
];
2021-08-02 03:58:41 +12:00
2022-03-27 01:42:13 +13:00
const rest = new REST({ version: '10' }).setToken('token');
2021-08-02 03:58:41 +12:00
(async () => {
2022-01-08 05:18:25 +13:00
try {
console.log('Started refreshing application (/) commands.');
await rest.put(Routes.applicationGuildCommands(CLIENT_ID, GUILD_ID), { body: commands });
console.log('Successfully reloaded application (/) commands.');
} catch (error) {
console.error(error);
}
2021-08-02 19:43:36 +12:00
})();
2021-08-02 03:58:41 +12:00
```
Afterwards we can create a quite simple example bot:
2022-01-08 05:18:25 +13:00
2017-04-20 04:14:37 +12:00
```js
2022-01-29 07:14:20 +13:00
const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
2017-04-20 04:14:37 +12:00
client.on('ready', () => {
2022-01-08 05:18:25 +13:00
console.log(`Logged in as ${client.user.tag}!`);
2017-04-20 04:14:37 +12:00
});
2022-01-08 05:18:25 +13:00
client.on('interactionCreate', async (interaction) => {
2022-06-11 07:19:44 +12:00
if (!interaction.isChatInputCommand()) return;
2021-08-02 03:58:41 +12:00
2022-01-08 05:18:25 +13:00
if (interaction.commandName === 'ping') {
await interaction.reply('Pong!');
}
2017-04-20 04:14:37 +12:00
});
2018-01-19 13:59:21 +13:00
client.login('token');
2017-04-20 04:14:37 +12:00
```
## Links
2020-03-01 02:35:57 +13:00
2022-06-09 05:13:31 +12:00
- [Website ](https://discord.js.org/ ) ([source](https://github.com/discordjs/discord.js/tree/main/packages/website))
2021-08-08 11:08:00 +12:00
- [Documentation ](https://discord.js.org/#/docs )
2021-08-02 22:58:21 +12:00
- [Guide ](https://discordjs.guide/ ) ([source](https://github.com/discordjs/guide))
2022-07-25 21:09:58 +12:00
See also the [Update Guide ](https://discordjs.guide/additional-info/changes-in-v14.html ), including updated and removed items in the library.
2021-10-30 06:53:54 +13:00
- [discord.js Discord server ](https://discord.gg/djs )
2020-03-01 02:35:57 +13:00
- [Discord API Discord server ](https://discord.gg/discord-api )
- [GitHub ](https://github.com/discordjs/discord.js )
2021-11-02 06:36:26 +13:00
- [npm ](https://www.npmjs.com/package/discord.js )
2021-06-12 10:41:49 +12:00
- [Related libraries ](https://discord.com/developers/docs/topics/community-resources#libraries )
2017-10-29 06:02:12 +13:00
### Extensions
2020-03-01 02:35:57 +13:00
- [RPC ](https://www.npmjs.com/package/discord-rpc ) ([source](https://github.com/discordjs/RPC))
2017-04-20 04:14:37 +12:00
## Contributing
2020-03-01 02:35:57 +13:00
2017-04-20 04:14:37 +12:00
Before creating an issue, please ensure that it hasn't already been reported/suggested, and double-check the
[documentation ](https://discord.js.org/#/docs ).
2021-08-08 11:08:00 +12:00
See [the contribution guide ](https://github.com/discordjs/discord.js/blob/main/.github/CONTRIBUTING.md ) if you'd like to submit a PR.
2017-04-20 04:14:37 +12:00
## Help
2020-03-01 02:35:57 +13:00
2017-04-20 04:14:37 +12:00
If you don't understand something in the documentation, you are experiencing problems, or you just need a gentle
2021-10-30 06:53:54 +13:00
nudge in the right direction, please don't hesitate to join our official [discord.js Server ](https://discord.gg/djs ).