A powerful JavaScript library for interacting with the Discord API
Go to file
2022-07-19 20:11:23 +02:00
.github chore: vercel banner 2022-07-19 18:26:03 +02:00
.husky chore: revert pre-commit hook to not change state (#8200) 2022-06-30 11:47:27 +02:00
.vscode feat(website): unocss 2022-06-08 21:45:16 +02:00
.yarn chore: update yarn 2022-06-04 13:13:52 +02:00
packages chore: use full link for vercel banner in readmes 2022-07-19 19:43:14 +02:00
.commitlintrc.json chore: disable scope-case rule for commitlint (#7507) 2022-02-20 13:35:20 +01:00
.dockerignore feat: proxy container (#8000) 2022-06-17 23:29:50 +02:00
.eslintrc.json feat: website (#8043) 2022-06-08 19:13:31 +02:00
.gitattributes According to the spec just this should work 2017-04-19 17:55:00 +02:00
.gitignore chore: move deps to root and some miscellaneous changes (#8129) 2022-06-21 15:15:38 +02:00
.prettierrc.json chore: monorepo setup (#7175) 2022-01-07 17:18:25 +01:00
.yarnrc.yml chore: update yarn 2022-06-04 13:13:52 +02:00
codecov.yml feat: codecov (#8219) 2022-07-03 15:33:18 +02:00
LICENSE chore: add license to root 2022-07-19 20:11:23 +02:00
package.json ci: fix building before linting/testing 2022-07-18 14:20:11 +02:00
README.md chore: use full link for vercel banner in readmes 2022-07-19 19:43:14 +02:00
tsconfig.eslint.json refactor: move all the config files to root (#8033) 2022-06-07 12:35:19 +02:00
tsconfig.json chore: upgrade deps 2022-06-19 13:19:24 +02:00
tsup.config.ts feat: proxy container (#8000) 2022-06-17 23:29:50 +02:00
turbo.json ci: fix building before generating docs 2022-06-30 15:56:07 +02:00
vitest.config.ts chore: ignore index files in coverage (#8293) 2022-07-17 18:51:03 +02:00
yarn.lock chore: change versions 2022-07-18 12:52:15 +02:00


discord.js


Discord server npm version npm downloads Tests status Code coverage

Vercel

About

discord.js is a powerful Node.js module that allows you to easily interact with the Discord API.

  • Object-oriented
  • Predictable abstractions
  • Performant
  • 100% coverage of the Discord API

Installation

Node.js 16.9.0 or newer is required.

npm install discord.js
yarn add discord.js
pnpm add discord.js

Optional packages

  • zlib-sync for WebSocket data compression and inflation (npm install zlib-sync)
  • erlpack for significantly faster WebSocket data (de)serialisation (npm install discord/erlpack)
  • bufferutil for a much faster WebSocket connection (npm install bufferutil)
  • utf-8-validate in combination with bufferutil for much faster WebSocket processing (npm install utf-8-validate)
  • @discordjs/voice for interacting with the Discord Voice API (npm install @discordjs/voice)

Example usage

Install all required dependencies:

npm install discord.js @discordjs/rest
yarn add discord.js @discordjs/rest
pnpm add discord.js @discordjs/rest

Register a slash command against the Discord API:

const { REST } = require('@discordjs/rest');
const { Routes } = require('discord.js');

const commands = [
	{
		name: 'ping',
		description: 'Replies with Pong!',
	},
];

const rest = new REST({ version: '10' }).setToken('token');

(async () => {
	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);
	}
})();

Afterwards we can create a quite simple example bot:

const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({ intents: [GatewayIntentBits.Guilds] });

client.on('ready', () => {
	console.log(`Logged in as ${client.user.tag}!`);
});

client.on('interactionCreate', async (interaction) => {
	if (!interaction.isChatInputCommand()) return;

	if (interaction.commandName === 'ping') {
		await interaction.reply('Pong!');
	}
});

client.login('token');

Extensions

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.