Discord4J Documentation

website·Indexed 32 days ago

https://docs.discord4j.com/

A reactive wrapper library for the Discord REST API and Gateway. The documentation covers bot development, including the use of Reactor for threading, sharding, interaction components (Buttons, Select Menus, Text Inputs), and migration guides for versions v2.x through v3.3.

Tokens
31.6K
Snippets
131
Records
222
Agent score
61%

What's inside Discord4J

  1. Overview of the Discord4J Library

    Discord4J is a reactive Java wrapper for the official Discord Bot API. It is designed for developers who want to build Discord bots using reactive programming patterns in Java.
  2. Understand the Discord4J threading model

    Discord4J uses a concurrency-agnostic runtime based on Project Reactor. Its threading model is divided by task type:

    • Network I/O: HTTP (API/Websocket) and UDP interactions use Reactor Netty's event loop (minimum 4 threads).
    • Event Dispatching: Events are dispatched via EventDispatcher using a ForkJoinPoolScheduler to avoid blocking the network event loop.
    • API Responses: REST API responses are returned via the Bounded Elastic Scheduler, which supports downstream blocking operations.
    • Serialized Operations: Exclusive schedulers are used for rate-limiting (GlobalRateLimiter, Gateway IDENTIFY, and per-session outbound limiters).
  3. Architect distributed bots with Discord4J Connect

    Discord4J Connect enables distributed bot architectures across multiple machines using a Leader/Worker topology:

    • Leaders: Establish WebSocket connections to the Discord Gateway, handle lifecycles, and deliver payloads to middleware.
    • Workers: Do not connect to the Gateway; they receive payloads from middleware and process commands.
    • Middleware: Provides distributed functionality including ShardCoordinator (throttling IDENTIFY), GlobalRateLimiter, Router (REST API coordination), a payload broker, and a distributed StoreService (entity cache).

    Common patterns include:

    • Writing Leaders, Reading Stateless Workers: Leaders write to the entity cache; workers read from it and process commands.
    • Thin Leaders, Stateless Workers: Workers handle Gateway payload processing and write to the entity cache, reducing load on leaders.
  4. Understand the reactive paradigm shift in Discord4J v3

    Discord4J v3 moves from a synchronous, blocking paradigm to a fully asynchronous, reactive model powered by Project Reactor. The API uses Mono (for 0 or 1 result) and Flux (for 0 to N results). Unlike v2, simply calling a method does not execute the action; you must 'subscribe' to the returned Mono or Flux for the operation to occur.
  5. Understand Reactor basics in Discord4J

    Discord4J uses Project Reactor, an implementation of the Reactive Streams specification. Reactive programming is a declarative paradigm where data is treated as streams and changes propagate downstream. In the context of Discord, Discord itself acts as the Publisher (source of data), and the bot/application acts as the Subscriber (consumer of data).
  6. Understand the architectural split between DiscordClient and GatewayDiscordClient

    In v3.1, REST API operations are structurally separated from Gateway operations. DiscordClient is now used exclusively for REST API operations and for spawning Gateway connections. It no longer provides access to the Store abstraction for cached entities. To work with Gateway-based entities and caches, you must use GatewayDiscordClient, which is acquired by subscribing to DiscordClient::login or GatewayBootstrap::login after creation via DiscordClient::gateway.
  7. Support for Stage channels, Threads, and Forums in Discord4J v3.3

    Discord4J v3.3 introduces support for new Discord channel types, including Stage channels, Threads, and Forums.
  8. Understand Discord4J Interaction Hierarchy

    Discord sends a single event over the gateway for all interactions. Discord4J simplifies development by breaking this single event down into separate event classes, allowing developers to handle specific interaction types independently.
  9. Handle MESSAGE_CONTENT intent requirements in v3.3

    In v3.3, calling any method that retrieves restricted data (such as message content or embeds) without enabling the MESSAGE_CONTENT intent will now throw an UnsupportedOperationException.