discord.js

14.26.3 · active · verified Wed Apr 22

Discord.js is a comprehensive, performant, and object-oriented JavaScript library designed for interacting with the Discord API. It simplifies bot development by abstracting complex API calls and WebSocket management, providing a rich set of classes and utilities. The current stable version is 14.26.3, and the library maintains a frequent release cadence, often pushing out minor bug fixes and new features to keep pace with Discord's evolving API, as evidenced by the rapid 14.26.x updates. Key differentiators include its extensive documentation, a large and active community, and a modular ecosystem (e.g., `@discordjs/rest`, `@discordjs/builders`, `@discordjs/voice`) that allows developers to integrate specific functionalities as needed. It supports both CommonJS and ES Modules, with modern usage heavily favoring ESM and TypeScript for type safety and modern JavaScript features.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart initializes a basic Discord bot, logs it in, and makes it respond to 'ping' and 'hello' messages in any channel it has access to. It demonstrates mandatory intents and best practices for token management.

import { Client, Events, GatewayIntentBits } from 'discord.js';

// Create a new client instance with required intents and partials
// Intents are crucial for specifying which events your bot wants to receive from Discord.
// MessageContent is a privileged intent and must be enabled in the Discord Developer Portal.
const client = new Client({
    intents: [
        GatewayIntentBits.Guilds,
        GatewayIntentBits.GuildMessages,
        GatewayIntentBits.MessageContent // Required for reading message content in v14+
    ],
    partials: [
        Partials.Message, 
        Partials.Channel, 
        Partials.Reaction // Example partials, enable as needed
    ]
});

// When the client is ready, run this once.
// The 'ready' event indicates that the bot has successfully logged in.
client.once(Events.ClientReady, readyClient => {
    console.log(`Ready! Logged in as ${readyClient.user.tag}`);
});

// Listen for 'messageCreate' events to respond to messages
client.on(Events.MessageCreate, async message => {
    // Ignore messages from other bots or messages without content
    if (message.author.bot || !message.content) return;

    if (message.content.toLowerCase() === 'ping') {
        await message.reply('Pong!');
    }

    if (message.content.toLowerCase() === 'hello') {
        await message.channel.send(`Hello, ${message.author.username}!`);
    }
});

// Log in to Discord with your client's token
// Make sure to set your bot token in an environment variable named DISCORD_TOKEN
// or provide it directly (not recommended for production).
client.login(process.env.DISCORD_TOKEN ?? '').catch(console.error);

view raw JSON →