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 throughbuilder(String). A default instance is capable of performing REST API operations locally and connecting to the Gateway by usinglogin(),withGateway(Function)orgateway().
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static DiscordClientBuilder<DiscordClient,RouterOptions>builder(String token)Obtain aDiscordClientBuilderable to createDiscordClientinstances, using the given token for authentication.static DiscordClientcreate(String token)Create aDiscordClientwith default options, using the given token for authentication.GatewayBootstrap<GatewayOptions>gateway()Start bootstrapping a connection to the real-time Discord Gateway.CoreResourcesgetCoreResources()Obtain theCoreResourcesassociated with thisDiscordClient.Mono<GatewayDiscordClient>login()Login the client to the gateway, using the recommended amount of shards, locally coordinated.Mono<Void>withGateway(Function<GatewayDiscordClient,Publisher<?>> whileConnectedFunction)Connect to the Discord Gateway upon subscription to acquire aGatewayDiscordClientinstance and use it in a declarative way, releasing the object once the derived usageFunctioncompletes, and the underlying shard group disconnects, according toGatewayDiscordClient.onDisconnect().-
Methods inherited from class discord4j.rest.RestClient
createGuild, edit, getApplicationId, getApplicationInfo, getApplicationService, getAuditLogService, getChannelById, getChannelService, getEmojiService, getGatewayService, getGuildById, getGuildEmojiById, getGuilds, getGuildService, getInteractionService, getInvite, getInviteService, getMemberById, getMessageById, getRegions, getRestResources, getRoleById, getSelf, getSelfMember, getTemplate, getTemplateService, getUserById, getUserService, getVoiceService, getWebhookById, getWebhookService, restBuilder, restChannel, restGuild, restGuildEmoji, restMember, restMessage, restRole, restUser, restWebhook, selfRestMember
-
-
-
-
Method Detail
-
create
public static DiscordClient create(String token)
Create aDiscordClientwith default options, using the given token for authentication.- Parameters:
token- the bot token used for authentication- Returns:
- a
DiscordClientconfigured with the default options
-
builder
public static DiscordClientBuilder<DiscordClient,RouterOptions> builder(String token)
Obtain aDiscordClientBuilderable to createDiscordClientinstances, using the given token for authentication.- Parameters:
token- the bot token used for authentication- Returns:
- a
DiscordClientBuilder
-
getCoreResources
public CoreResources getCoreResources()
Obtain theCoreResourcesassociated with thisDiscordClient.- Returns:
- the current
CoreResourcesfor this client
-
login
public Mono<GatewayDiscordClient> login()
Login the client to the gateway, using the recommended amount of shards, locally coordinated. The derivedGatewayDiscordClientis capable of managing these shards and providing a singleEventDispatcherto publish Gateway updates andStorefor 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
Monoof aGatewayDiscordClient, emitting the result once shards have connected. Therefore, callingMono.block()will now return upon connection instead of disconnection.
-
withGateway
public Mono<Void> withGateway(Function<GatewayDiscordClient,Publisher<?>> whileConnectedFunction)
Connect to the Discord Gateway upon subscription to acquire aGatewayDiscordClientinstance and use it in a declarative way, releasing the object once the derived usageFunctioncompletes, and the underlying shard group disconnects, according toGatewayDiscordClient.onDisconnect().To further configure the bot features, refer to using
gateway().Calling this method is useful when you operate on the
GatewayDiscordClientobject using reactive API you can compose within the scope of the givenFunction.- Parameters:
whileConnectedFunction- theFunctionto apply the connectedGatewayDiscordClientand trigger a processing pipeline from it.- Returns:
- an empty
Monocompleting 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 aGatewayDiscordClientwhich groups all connecting shards providing a singleEventDispatcherto publish Gateway updates andStorefor 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
GatewayDiscordClientinstances.
-
-