What's new in v3.1
Welcome to this next major release in Discord4J, a reactive wrapper library for the Discord REST API and real-time Bot Gateway with voice capabilities.
After a year of feedback from users since releasing v3.0, we collected a series of changes to provide and support more use cases and bot configurations. While some of these changes could make their way into following v3.0 minor releases, some involve deeper design work that would have an impact on present releases in terms of binary and behavior changes.
In addition, since Discord is an evolving platform, some of its changes also have impact in the type structure that makes it difficult to implement every change in minor releases. As such, a new major release is the opportunity to include all of these changes and prepare the library structure for future additions more rapidly.
Maintenance mode for v3.0
After v3.1.0 releases, only bugfixes will be accepted in the 3.0.x branch.
Architectural changes
DiscordClient is split from Gateway actions
In order to support more use cases, such as bots that don't use the Gateway to perform some actions, components related to REST API operations are now structurally separated from the Gateway operations.
This means a DiscordClient no longer gives you access to the Store abstraction for cached entities from the Gateway and is now used to spawn Gateway connections and operating the REST API directly.
Moving forward, the new GatewayDiscordClient represents the place to fetch and work with Gateway-based entities. You now acquire this instance every time to subscribe to DiscordClient::login or GatewayBootstrap::login after creating it with DiscordClient::gateway.
Shard group creates a GatewayDiscordClient
One of the main goals for this release is to lay the groundwork to build distributed bot architectures, a model where bot components are split across multiple processes.
Since many configurations exist, we decided to orient the creation of GatewayDiscordClient towards "shard groups" that encapsulate caches and events around an arbitrary number of shards. A brand new GatewayBootstrap API provides creating simple, sharded, monolithic or distributed bots from a single place.
One EventDispatcher publishes for a shard group
Building a shard group creates a context that aggregates payloads from joining Gateway connections, publishing Event instances to a single EventDispatcher, simplifying managing a sharded bot within a process.
For this reason, Event instances are now aware of the shard they were created by.
Caches are shared across a shard group
Since Gateway updates are processed within a shard group, Stores, our entity cache abstraction, are created once per group. This eases management for single-process sharded bots.
Important API changes
For a more exhaustive list, please look at API changes.
Promote Snowflake class to common module
We moved the discord4j.core.object.util.Snowflake type to discord4j.common.util.Snowflake in order to support all modules across the Discord4J project using it. We feel it is a great abstraction to handle IDs and the primary token of addressing particular Discord entities across the lifetime of your application.
Other types affected by module promotion: Image, Permission, PermissionSet. See Moved types.
Behavior changes
Login completes on connect instead of disconnect
An important change in this major update is the change of the login() method existing in DiscordClient. We feel that the method makes most sense if it returns a Mono that completes as login succeeds, instead of logout, and to return a GatewayDiscordClient handle for the underlying gateway connections.
Spec code is run on subscription time
Previously in v3.0, Spec consumers were called on assembly time, meaning when a reactive pipeline was declared. This could lead to unexpected code execution so moving forward this was changed in alignment to the expectation of reactive programming where no significant work should happen until you subscribe.
New features
Gateway intents support
Add support to establish Gateway connections using the Intent system, reducing the inbound event load on your bot. See GatewayBootstrap::setEnabledIntents or GatewayBootstrap::setDisabledIntents
Wait until Gateway connections are ready
Allow customizing the behavior whether a GatewayDiscordClient should be obtained immediately (similar to v3.0 behavior) or to wait until all shards in the group have connected (Discord "READY" status). Check GatewayBootstrap::setAwaitConnections
Custom EventDispatcher
Allow changing the EventDispatcher implementation for event handling. Check the class for builders and factories.
Entity retrieval strategy
Allow modifying the behavior of the getXById methods in GatewayDiscordClient. By default, like in v3.0, if a requested entity is not present in the Store, a REST API request is made. Since this might not be desired, you can globally change the behavior at GatewayBootstrap::setEntityRetrievalStrategy or per-call through GatewayDiscordClient::withRetrievalStrategy.
Custom Reactor resources used by REST, Gateway and Voice
All Reactor resources used by Discord4J can be configured, across REST, Gateway and Voice operations. You can now control almost every aspect of Discord4J threading model.
Guild member request behavior
Allow providing a custom MemberRequestFilter to more explicitly control member requests should be automatically issued. Check GatewayBootstrap::setMemberRequestFilter.
On-demand guild member request (lazy loading)
Add methods to directly request for guild members, such as GatewayDiscordClient::requestMembers.
REST-only entities
Allow working with Discord4J without an active Gateway connection. Just create a DiscordClient and perform calls through the getXById methods to use REST API directly.
You can also obtain a "REST entity" which wraps a particular entity with its ID (no actual data) and can be used to perform actions without unneeded API requests. These are available directly through one of the entity types supported: RestChannel, RestEmoji, RestGuild, RestInvite, RestMember, RestMessage, RestRole, RestUser, RestWebhook.
Obtaining a RestClient can be done through GatewayDiscordClient::rest or any DiscordClient instance, capable of directly querying the API or creating such REST entities.
Normalized internal data representation
A large effort is made into converging our many internal data structures into a more maintainable approach. We decide to use Immutables library to model the Discord domain through Data classes. All are created with a Builder that can simplify their usage when interacting with some API, particularly through REST-only entities.
ShardCoordinator abstraction
A new abstraction is created to support coordination of Gateway IDENTIFY requests across multiple processes.
More sharding configurations supported
A builder to configure how a shard group creates shards is available through ShardingStrategy and can be set at GatewayBootstrap. The total shard count can be fixed or used as recommended by Discord, connect specific shards by index, and support a sharding concurrency value or "very large bot sharding system".
Improved rate limiting implementations
Provide a new approach to rate limit which increases throughput without locking structures. This is used across all of Discord4J: REST request queue, REST global rate limiter, Gateway identify limit and Gateway outbound limit.
Custom maximum missed heartbeat ACK
Bring back a setting from v2 to help deal with connection instability by avoiding detecting zombie connections too early.