discord.js/README.md

146 lines
5 KiB
Markdown
Raw Normal View History

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>
<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
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
2022-01-08 09:03:00 +13:00
**Node.js 16.9.0 or newer is required.**
```sh
npm install discord.js
2021-08-11 02:27:06 +12:00
yarn add discord.js
pnpm add discord.js
```
2017-04-20 04:14:37 +12:00
### Optional packages
- [zlib-sync](https://www.npmjs.com/package/zlib-sync) for WebSocket data compression and inflation (`npm install zlib-sync`)
- [erlpack](https://github.com/discord/erlpack) for significantly faster WebSocket data (de)serialisation (`npm install discord/erlpack`)
src: sharding cleanup and checkReady rewrite (#3393) * src: Step 1 of who knows how many * src: Remove accidentally committed test file * src: Remove useless added property in package.json * docs: Trailing spaces, come back >.> * src: Buhbye uws, we will miss you..not! * src: Move 'auto' shard selection from totalShardCount to shards * src: tweak * src: Filter out floats from shard IDs You want half of a shard or what? * src: Misc cleanup and bugfix for GUILD_BAN_ADD * src: Rewrite checkReady * src: Misse this while merging master into my branch * typings: Bring these up to date * typings: Forgot allReady event * src: Don't checkReady if the shard isn't waiting for guilds * src: Fix a possible bug for when the ws dies and the session becomes -1 * src: Hopefully fix last edge case that could case a shard to infinitely boot loop * src: Rename totalShardCount to shardCount * src: Small bugfix * src: Correct error message for shardCount being imvalid Co-Authored-By: bdistin <bdistin@gmail.com> * src: Small tweaks * src: If this doesn't fix the issues I'm gonna throw a brick at my PC * src: I swear, STOP BREAKING * src: *groans at a certain snake* * src: Use undefined instead of null on destroy in close event Setting it to null sets the close code to null, which causes a WebSocket error to be thrown. The error is thrown from WebSocket, although there is no connection alive. Fun times! * src: @SpaceEEC's requested changes * src: Remove zucc from discord.js Discord is removing support for it, sooo... Bye bye * src: Missed this * src: Apply @kyranet's suggestions Co-Authored-By: Antonio Román <kyradiscord@gmail.com> * src: @kyranet's suggestions * src: Remove pako, update debug messages - Pako is officially gone from both enviroments Install zlib-sync on node.js if you want it - Improve a few debug messages some more - Discover that internal sharding works in browsers but please don't do that
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
2022-07-29 20:47:30 +12:00
Install discord.js:
2022-01-08 05:18:25 +13:00
```sh
2022-07-29 20:47:30 +12:00
npm install discord.js
yarn add discord.js
pnpm add discord.js
```
Register a slash command against the Discord API:
2022-01-08 05:18:25 +13:00
```js
2022-07-29 20:47:30 +12:00
const { REST, Routes } = require('discord.js');
2022-01-08 05:18:25 +13:00
const commands = [
{
name: 'ping',
description: 'Replies with Pong!',
},
];
const rest = new REST({ version: '10' }).setToken(TOKEN);
(async () => {
2022-01-08 05:18:25 +13:00
try {
console.log('Started refreshing application (/) commands.');
await rest.put(Routes.applicationCommands(CLIENT_ID), { body: commands });
2022-01-08 05:18:25 +13:00
console.log('Successfully reloaded application (/) commands.');
} catch (error) {
console.error(error);
}
})();
```
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
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) => {
if (!interaction.isChatInputCommand()) return;
2022-01-08 05:18:25 +13:00
if (interaction.commandName === 'ping') {
await interaction.reply('Pong!');
}
2017-04-20 04:14:37 +12:00
});
client.login(TOKEN);
2017-04-20 04:14:37 +12:00
```
## Links
2022-10-09 04:55:01 +13:00
- [Website][website] ([source][website-source])
- [Documentation][documentation]
- [Guide][guide] ([source][guide-source])
See also the [Update Guide][guide-update], including updated and removed items in the library.
- [discord.js Discord server][discord]
- [Discord API Discord server][discord-api]
- [GitHub][source]
- [npm][npm]
- [Related libraries][related-libs]
### Extensions
2022-10-09 04:55:01 +13:00
- [RPC][rpc] ([source][rpc-source])
2017-04-20 04:14:37 +12:00
## Contributing
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
2022-10-09 04:55:01 +13:00
[documentation][documentation].
See [the contribution guide][contributing] if you'd like to submit a PR.
2017-04-20 04:14:37 +12:00
## Help
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
2022-10-09 04:55:01 +13:00
nudge in the right direction, please don't hesitate to join our official [discord.js Server][discord].
[website]: https://discord.js.org/
[website-source]: https://github.com/discordjs/discord.js/tree/main/apps/website
2022-10-09 04:55:01 +13:00
[documentation]: https://discord.js.org/#/docs
[guide]: https://discordjs.guide/
[guide-source]: https://github.com/discordjs/guide
[guide-update]: https://discordjs.guide/additional-info/changes-in-v14.html
[discord]: https://discord.gg/djs
[discord-api]: https://discord.gg/discord-api
[source]: https://github.com/discordjs/discord.js/tree/main/packages/discord.js
[npm]: https://www.npmjs.com/package/discord.js
[related-libs]: https://discord.com/developers/docs/topics/community-resources#libraries
[rpc]: https://www.npmjs.com/package/discord-rpc
[rpc-source]: https://github.com/discordjs/RPC
[contributing]: https://github.com/discordjs/discord.js/blob/main/.github/CONTRIBUTING.md