Class DiscordClient

java.lang.Object
discord4j.rest.RestClient
discord4j.core.DiscordClient

public final class DiscordClient extends RestClient
A high-level abstraction of common Discord4J operations such as entity retrieval through Discord API or the creation of real-time bot clients through Discord Gateway.

Instances can be obtained by using create(String) or through builder(String). A default instance is capable of performing REST API operations locally and connecting to the Gateway by using login(), withGateway(Function) or gateway().

  • Method Details

    • create

      public static DiscordClient create(String token)
      Create a DiscordClient with default options, using the given token for authentication.
      Parameters:
      token - the bot token used for authentication
      Returns:
      a DiscordClient configured with the default options
    • builder

      public static DiscordClientBuilder<DiscordClient,RouterOptions> builder(String token)
      Obtain a DiscordClientBuilder able to create DiscordClient instances, using the given token for authentication.
      Parameters:
      token - the bot token used for authentication
      Returns:
      a DiscordClientBuilder
    • getCoreResources

      public CoreResources getCoreResources()
      Obtain the CoreResources associated with this DiscordClient.
      Returns:
      the current CoreResources for this client
    • login

      public Mono<GatewayDiscordClient> login()
      Login the client to the gateway, using the recommended amount of shards, locally coordinated. The derived GatewayDiscordClient is capable of managing these shards and providing a single EventDispatcher to publish Gateway updates and Store for entity caching.

      To further configure the Gateway connections, such as initial presence, sharding and caching options, see gateway().

      Note: Starting from v3.1, this method will return a Mono of a GatewayDiscordClient, emitting the result once shards have connected. Therefore, calling Mono.block() will now return upon connection instead of disconnection.

      Returns:
      a Mono for a handle to maintain a group of shards connected to real-time Discord Gateway, emitted once at least one connection has been made. This behavior can be configured through gateway().setAwaitConnections(true). If an error is received, it is emitted through the Mono.
    • withGateway

      public Mono<Void> withGateway(Function<GatewayDiscordClient,Publisher<?>> whileConnectedFunction)
      Connect to the Discord Gateway upon subscription to acquire a GatewayDiscordClient instance and use it in a declarative way, releasing the object once the derived usage Function completes, and the underlying shard group disconnects, according to GatewayDiscordClient.onDisconnect().

      To further configure the bot features, refer to using gateway().

      Calling this method is useful when you operate on the GatewayDiscordClient object using reactive API you can compose within the scope of the given Function.

      Parameters:
      whileConnectedFunction - the Function to apply the connected GatewayDiscordClient and trigger a processing pipeline from it.
      Returns:
      an empty Mono completing after all resources have released
    • gateway

      public GatewayBootstrap<GatewayOptions> gateway()
      Start bootstrapping a connection to the real-time Discord Gateway. The resulting builder can be configured to create a GatewayDiscordClient which groups all connecting shards providing a single EventDispatcher to publish Gateway updates and Store for entity caching.

      The following are some of the features configured by this builder:

      • Sharding configuration
      • Initial event listeners and customization
      • Gateway intents
      • Initial bot status
      • Custom entity cache factory
      • Distributed architecture options
      • Member caching options
      • Guild subscriptions
      • Threading model customization
      • Entity fetching strategy
      • Gateway and voice connection options
      Returns:
      a bootstrap to create GatewayDiscordClient instances.