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 aDiscordClientBuilder
able to createDiscordClient
instances, using the given token for authentication.static DiscordClient
create(String token)
Create aDiscordClient
with default options, using the given token for authentication.GatewayBootstrap<GatewayOptions>
gateway()
Start bootstrapping a connection to the real-time Discord Gateway.CoreResources
getCoreResources()
Obtain theCoreResources
associated 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 aGatewayDiscordClient
instance and use it in a declarative way, releasing the object once the derived usageFunction
completes, and the underlying shard group disconnects, according toGatewayDiscordClient.onDisconnect()
.-
Methods inherited from class discord4j.rest.RestClient
createGuild, edit, getApplicationId, getApplicationInfo, getApplicationService, getAuditLogService, getAutoModService, getChannelById, getChannelService, getEmojiService, getGatewayService, getGuildById, getGuildEmojiById, getGuilds, getGuildService, getGuildStickerById, getInteractionService, getInvite, getInvite, getInviteService, getMemberById, getMessageById, getRegions, getRestResources, getRoleById, getScheduledEventById, getSelf, getSelfMember, getStickerService, getTemplate, getTemplateService, getUserById, getUserService, getVoiceService, getWebhookById, getWebhookService, restBuilder, restChannel, restGuild, restGuildEmoji, restGuildSticker, restMember, restMessage, restRole, restScheduledEvent, restUser, restWebhook, selfRestMember
-
-
-
-
Method Detail
-
create
public static DiscordClient create(String token)
Create aDiscordClient
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 aDiscordClientBuilder
able to createDiscordClient
instances, using the given token for authentication.- Parameters:
token
- the bot token used for authentication- Returns:
- a
DiscordClientBuilder
-
getCoreResources
public CoreResources getCoreResources()
Obtain theCoreResources
associated with thisDiscordClient
.- 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 derivedGatewayDiscordClient
is capable of managing these shards and providing a singleEventDispatcher
to publish Gateway updates andStore
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 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 aGatewayDiscordClient
instance and use it in a declarative way, releasing the object once the derived usageFunction
completes, 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
GatewayDiscordClient
object using reactive API you can compose within the scope of the givenFunction
.- Parameters:
whileConnectedFunction
- theFunction
to apply the connectedGatewayDiscordClient
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 aGatewayDiscordClient
which groups all connecting shards providing a singleEventDispatcher
to publish Gateway updates andStore
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.
-
-