public class ShardingClientBuilder extends Object
DiscordClient
each dedicated to a shard, while
allowing resource sharing and login coordination.
Creates multiple Discord clients with many configurable options. It uses a recommended amount of shards but can be
overridden through setShardCount(Integer)
.
Constructor and Description |
---|
ShardingClientBuilder(String token)
Initialize a new builder with the given token.
|
Modifier and Type | Method and Description |
---|---|
Flux<DiscordClientBuilder> |
build()
Create a sequence of
DiscordClientBuilder s each representing a shard, up to the resulting shard count,
filtering out values not matching the predicate given by getShardIndexFilter() . |
RouterFactory |
getRouterFactory()
Deprecated.
|
RouterOptions |
getRouterOptions()
Deprecated.
|
Integer |
getShardCount()
Deprecated.
|
Predicate<Integer> |
getShardIndexFilter()
Deprecated.
|
ShardingStoreRegistry |
getShardingStoreRegistry()
Deprecated.
|
String |
getToken()
Deprecated.
|
ShardingClientBuilder |
setRouterFactory(RouterFactory routerFactory)
Set a new
RouterFactory used to create a Router that executes Discord REST API requests and
will be shared across all sharding clients created by this builder. |
ShardingClientBuilder |
setRouterOptions(RouterOptions routerOptions)
Sets a new
RouterOptions used to configure a RouterFactory . |
ShardingClientBuilder |
setShardCount(Integer shardCount)
Change the shard count value given to each resulting shard.
|
ShardingClientBuilder |
setShardIndexFilter(Predicate<Integer> shardIndexFilter)
Set a new
Predicate of shard index, indicating whether a shard should be created. |
ShardingClientBuilder |
setShardingStoreRegistry(ShardingStoreRegistry shardingStoreRegistry)
Set a new
ShardingStoreRegistry used to coordinate stores across shards. |
ShardingClientBuilder |
setToken(String token)
Change the token stored in this builder.
|
public ShardingClientBuilder(String token)
token
- the bot token used to authenticate to Discord@Deprecated public String getToken()
public ShardingClientBuilder setToken(String token)
token
- the new bot token@Nullable @Deprecated public Integer getShardCount()
null
, in which case use the recommended shard count given by the
Routes
endpoint.public ShardingClientBuilder setShardCount(@Nullable Integer shardCount)
null
to use the
recommended value from the Routes
endpoint, that is calculated from
the number of guilds this token is in.shardCount
- the shard count, can be null
, in which case use a recommended value@Nullable @Deprecated public RouterFactory getRouterFactory()
RouterFactory
used to create a
Router
that executes Discord REST API requests.public ShardingClientBuilder setRouterFactory(@Nullable RouterFactory routerFactory)
RouterFactory
used to create a Router
that executes Discord REST API requests and
will be shared across all sharding clients created by this builder.
Make sure the given factory is capable of coordinating rate limits across shards, otherwise use the default value as it is shard aware.
routerFactory
- a new RouterFactory to create a Router that performs API requests. Pass null
to
use the default value@Nullable @Deprecated public RouterOptions getRouterOptions()
RouterOptions
used to configure RouterFactory
instances.RouterOptions
used by this clientpublic ShardingClientBuilder setRouterOptions(@Nullable RouterOptions routerOptions)
RouterOptions
used to configure a RouterFactory
.
RouterOptions
instances provide a way to override the Scheduler
used for retrieving API responses
and scheduling rate limiting actions. It also allows changing the behavior associated with API errors through
RouterOptions.Builder#onClientResponse(ResponseFunction)
.
If you use a default RouterFactory
, it will use the supplied RouterOptions
to configure itself
while building this client.
routerOptions
- a new RouterOptions
to configure a RouterFactory
@Nullable @Deprecated public ShardingStoreRegistry getShardingStoreRegistry()
ShardingStoreRegistry
used to coordinate stores across shards.public ShardingClientBuilder setShardingStoreRegistry(@Nullable ShardingStoreRegistry shardingStoreRegistry)
ShardingStoreRegistry
used to coordinate stores across shards. The default value is to use
ShardingJdkStoreRegistry
. If you wish to disable this feature, set the resulting client stores to any
non-coordinating value through
DiscordClientBuilder.setStoreService(discord4j.store.api.service.StoreService)
in order to
override the default value that attempts to communicate with this registry.shardingStoreRegistry
- a new store registry to coordinate shards. Can be null
to use the
default value@Nullable @Deprecated public Predicate<Integer> getShardIndexFilter()
Predicate
used to include only a set of shards in the creation process. By default
all shards given by the shard count are created.null
public ShardingClientBuilder setShardIndexFilter(@Nullable Predicate<Integer> shardIndexFilter)
Predicate
of shard index, indicating whether a shard should be created. Can be used in
scenarios when you want to partially colocate the shards given by shard count.shardIndexFilter
- the new predicate of shard index to use in order to filter shard creationpublic Flux<DiscordClientBuilder> build()
DiscordClientBuilder
s each representing a shard, up to the resulting shard count,
filtering out values not matching the predicate given by getShardIndexFilter()
.
This sequence can be further customized on a per-shard basis until you call
DiscordClient.login()
:
new ShardingClientBuilder(token) .build() .map(builder -> builder.setInitialPresence( Presence.online(Activity.playing("Shard " + builder.getShardIndex())))) .map(DiscordClientBuilder::build) .flatMap(DiscordClient::login) .blockLast();
Login will be coordinated across this sequence, that is, a shard will identify to the gateway only when the previous one has connected successfully and the mandatory time between login attempt has passed (~5 seconds).
DiscordClient
based on this builder parameters