Class DefaultShardManager
- java.lang.Object
-
- net.dv8tion.jda.api.sharding.DefaultShardManager
-
- All Implemented Interfaces:
ShardManager
public class DefaultShardManager extends java.lang.Object implements ShardManager
JDA's defaultShardManagerimplementation. To create new instances use theDefaultShardManagerBuilder.- Since:
- 3.4
- Author:
- Aljoscha Grebe
-
-
Field Summary
Fields Modifier and Type Field Description static java.util.concurrent.ThreadFactoryDEFAULT_THREAD_FACTORYstatic org.slf4j.LoggerLOG
-
Constructor Summary
Constructors Constructor Description DefaultShardManager(java.lang.String token)DefaultShardManager(java.lang.String token, java.util.Collection<java.lang.Integer> shardIds)DefaultShardManager(java.lang.String token, java.util.Collection<java.lang.Integer> shardIds, net.dv8tion.jda.internal.utils.config.sharding.ShardingConfig shardingConfig, net.dv8tion.jda.internal.utils.config.sharding.EventConfig eventConfig, net.dv8tion.jda.internal.utils.config.sharding.PresenceProviderConfig presenceConfig, net.dv8tion.jda.internal.utils.config.sharding.ThreadingProviderConfig threadingConfig, net.dv8tion.jda.internal.utils.config.sharding.ShardingSessionConfig sessionConfig, net.dv8tion.jda.internal.utils.config.sharding.ShardingMetaConfig metaConfig, ChunkingFilter chunkingFilter)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddEventListener(java.lang.Object... listeners)Adds all provided listeners to the event-listeners that will be used to handle events.voidaddEventListeners(java.util.function.IntFunction<java.lang.Object> eventListenerProvider)Adds listeners provided by the listener provider to each shard to the event-listeners that will be used to handle events.java.util.EnumSet<GatewayIntent>getGatewayIntents()TheGatewayIntentsfor the JDA sessions of this shard manager.GuildgetGuildById(long id)This returns theGuildwhich has the same id as the one provided.ShardCacheViewgetShardCache()UnifiedShardCacheViewof all cachedJDAbound to this ShardManager instance.intgetShardsQueued()Returns the amount of shards queued for (re)connecting.intgetShardsTotal()Returns the amount of shards managed by thisShardManager.voidlogin()Initializes and starts all shards.voidremoveEventListener(java.lang.Object... listeners)Removes all provided listeners from the event-listeners and no longer uses them to handle events.voidremoveEventListenerProvider(java.util.function.IntFunction<java.lang.Object> eventListenerProvider)Remove a listener provider.voidrestart()Restarts all shards, shutting old ones down first.voidrestart(int shardId)Restarts the shards with the given id only.voidsetActivityProvider(java.util.function.IntFunction<? extends Activity> activityProvider)Sets provider that provider theActivityfor all shards.voidsetIdleProvider(java.util.function.IntFunction<java.lang.Boolean> idleProvider)Sets the provider that decides for all shards whether they should be marked as afk or not.voidsetPresenceProvider(java.util.function.IntFunction<OnlineStatus> statusProvider, java.util.function.IntFunction<? extends Activity> activityProvider)Sets the provider that provides theOnlineStatusandActivityfor all shards.voidsetStatusProvider(java.util.function.IntFunction<OnlineStatus> statusProvider)Sets the provider that provides theOnlineStatusfor all shards.voidshutdown()Shuts down all JDA shards, closing all their connections.voidshutdown(int shardId)Shuts down the shard with the given id only.voidstart(int shardId)Adds a new shard with the given id to this ShardManager and starts it.-
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface net.dv8tion.jda.api.sharding.ShardManager
getAverageGatewayPing, getCategories, getCategoriesByName, getCategoryById, getCategoryById, getCategoryCache, getEmoteById, getEmoteById, getEmoteCache, getEmotes, getEmotesByName, getGuildById, getGuildCache, getGuildChannelById, getGuildChannelById, getGuildChannelById, getGuildChannelById, getGuilds, getGuildsByName, getMutualGuilds, getMutualGuilds, getPrivateChannelById, getPrivateChannelById, getPrivateChannelCache, getPrivateChannels, getRoleById, getRoleById, getRoleCache, getRoles, getRolesByName, getShardById, getShardById, getShards, getShardsRunning, getStatus, getStatuses, getStoreChannelById, getStoreChannelById, getStoreChannelCache, getStoreChannels, getTextChannelById, getTextChannelById, getTextChannelCache, getTextChannels, getUserById, getUserById, getUserByTag, getUserByTag, getUserCache, getUsers, getVoiceChannelById, getVoiceChannelById, getVoiceChannelCache, getVoiceChannels, removeEventListeners, retrieveApplicationInfo, retrieveUserById, retrieveUserById, setActivity, setGame, setIdle, setPresence, setStatus
-
-
-
-
Constructor Detail
-
DefaultShardManager
public DefaultShardManager(@Nonnull java.lang.String token)
-
DefaultShardManager
public DefaultShardManager(@Nonnull java.lang.String token, @Nullable java.util.Collection<java.lang.Integer> shardIds)
-
DefaultShardManager
public DefaultShardManager(@Nonnull java.lang.String token, @Nullable java.util.Collection<java.lang.Integer> shardIds, @Nullable net.dv8tion.jda.internal.utils.config.sharding.ShardingConfig shardingConfig, @Nullable net.dv8tion.jda.internal.utils.config.sharding.EventConfig eventConfig, @Nullable net.dv8tion.jda.internal.utils.config.sharding.PresenceProviderConfig presenceConfig, @Nullable net.dv8tion.jda.internal.utils.config.sharding.ThreadingProviderConfig threadingConfig, @Nullable net.dv8tion.jda.internal.utils.config.sharding.ShardingSessionConfig sessionConfig, @Nullable net.dv8tion.jda.internal.utils.config.sharding.ShardingMetaConfig metaConfig, @Nullable ChunkingFilter chunkingFilter)
-
-
Method Detail
-
getGatewayIntents
@Nonnull public java.util.EnumSet<GatewayIntent> getGatewayIntents()
Description copied from interface:ShardManagerTheGatewayIntentsfor the JDA sessions of this shard manager.- Specified by:
getGatewayIntentsin interfaceShardManager- Returns:
EnumSetof active gateway intents
-
addEventListener
public void addEventListener(@Nonnull java.lang.Object... listeners)Description copied from interface:ShardManagerAdds all provided listeners to the event-listeners that will be used to handle events.Note: when using the
InterfacedEventListener(default), the given listener must be an instance ofEventListener!- Specified by:
addEventListenerin interfaceShardManager- Parameters:
listeners- The listener(s) which will react to events.
-
removeEventListener
public void removeEventListener(@Nonnull java.lang.Object... listeners)Description copied from interface:ShardManagerRemoves all provided listeners from the event-listeners and no longer uses them to handle events.- Specified by:
removeEventListenerin interfaceShardManager- Parameters:
listeners- The listener(s) to be removed.
-
addEventListeners
public void addEventListeners(@Nonnull java.util.function.IntFunction<java.lang.Object> eventListenerProvider)Description copied from interface:ShardManagerAdds listeners provided by the listener provider to each shard to the event-listeners that will be used to handle events. The listener provider gets a shard id applied and is expected to return a listener.Note: when using the
InterfacedEventListener(default), the given listener must be an instance ofEventListener!- Specified by:
addEventListenersin interfaceShardManager- Parameters:
eventListenerProvider- The provider of listener(s) which will react to events.
-
removeEventListenerProvider
public void removeEventListenerProvider(@Nonnull java.util.function.IntFunction<java.lang.Object> eventListenerProvider)Description copied from interface:ShardManagerRemove a listener provider. This will stop further created / restarted shards from getting a listener added by that provider. Default is a no-op for backwards compatibility, see implementations likeremoveEventListenerProvider(IntFunction)for actual code- Specified by:
removeEventListenerProviderin interfaceShardManager- Parameters:
eventListenerProvider- The provider of listeners that shall be removed.
-
getShardsQueued
public int getShardsQueued()
Description copied from interface:ShardManagerReturns the amount of shards queued for (re)connecting.- Specified by:
getShardsQueuedin interfaceShardManager- Returns:
- The amount of shards queued for (re)connecting.
-
getShardsTotal
public int getShardsTotal()
Description copied from interface:ShardManagerReturns the amount of shards managed by thisShardManager. This includes shards currently queued for a restart.- Specified by:
getShardsTotalin interfaceShardManager- Returns:
- The managed amount of shards.
-
getGuildById
public Guild getGuildById(long id)
Description copied from interface:ShardManagerThis returns theGuildwhich has the same id as the one provided.
If there is no connected guild with an id that matches the provided one, this will returnnull.- Specified by:
getGuildByIdin interfaceShardManager- Parameters:
id- The id of theGuild.- Returns:
- Possibly-null
Guildwith matching id.
-
getShardCache
@Nonnull public ShardCacheView getShardCache()
Description copied from interface:ShardManagerUnifiedShardCacheViewof all cachedJDAbound to this ShardManager instance.- Specified by:
getShardCachein interfaceShardManager- Returns:
- Unified
ShardCacheView
-
login
public void login() throws javax.security.auth.login.LoginExceptionDescription copied from interface:ShardManagerInitializes and starts all shards. This should only be called once.- Specified by:
loginin interfaceShardManager- Throws:
javax.security.auth.login.LoginException- If the provided token is invalid.
-
restart
public void restart(int shardId)
Description copied from interface:ShardManagerRestarts the shards with the given id only.
If there is no shard with the given Id, this method acts likeShardManager.start(int).- Specified by:
restartin interfaceShardManager- Parameters:
shardId- The id of the target shard
-
restart
public void restart()
Description copied from interface:ShardManagerRestarts all shards, shutting old ones down first.As all shards need to connect to discord again this will take equally long as the startup of a new ShardManager (using the 5000ms + backoff as delay between starting new JDA instances).
- Specified by:
restartin interfaceShardManager
-
shutdown
public void shutdown()
Description copied from interface:ShardManagerShuts down all JDA shards, closing all their connections. After this method has been called the ShardManager instance can not be used anymore.
This will shutdown the internal queue worker for (re-)starts of shards. This meansShardManager.restart(int),ShardManager.restart(), andShardManager.start(int)will throwRejectedExecutionException.This will interrupt the default JDA event thread, due to the gateway connection being interrupted.
- Specified by:
shutdownin interfaceShardManager
-
shutdown
public void shutdown(int shardId)
Description copied from interface:ShardManagerShuts down the shard with the given id only.
If there is no shard with the given id, this will do nothing.- Specified by:
shutdownin interfaceShardManager- Parameters:
shardId- The id of the shard that should be stopped
-
start
public void start(int shardId)
Description copied from interface:ShardManagerAdds a new shard with the given id to this ShardManager and starts it.- Specified by:
startin interfaceShardManager- Parameters:
shardId- The id of the shard that should be started
-
setActivityProvider
public void setActivityProvider(java.util.function.IntFunction<? extends Activity> activityProvider)
Description copied from interface:ShardManagerSets provider that provider theActivityfor all shards.
A Activity can be retrieved viaActivity.playing(String). For streams you provide a valid streaming url as second parameter.This will also change the provider for shards that are created in the future.
- Specified by:
setActivityProviderin interfaceShardManager- Parameters:
activityProvider- Provider for anActivityinstance or null to reset- See Also:
Activity.playing(String),Activity.streaming(String, String)
-
setIdleProvider
public void setIdleProvider(@Nonnull java.util.function.IntFunction<java.lang.Boolean> idleProvider)Description copied from interface:ShardManagerSets the provider that decides for all shards whether they should be marked as afk or not.This will also change the provider for shards that are created in the future.
- Specified by:
setIdleProviderin interfaceShardManager- Parameters:
idleProvider- Provider for a boolean
-
setPresenceProvider
public void setPresenceProvider(java.util.function.IntFunction<OnlineStatus> statusProvider, java.util.function.IntFunction<? extends Activity> activityProvider)
Description copied from interface:ShardManagerSets the provider that provides theOnlineStatusandActivityfor all shards.This will also change the status for shards that are created in the future.
- Specified by:
setPresenceProviderin interfaceShardManager- Parameters:
statusProvider- TheOnlineStatusto be used (OFFLINE/null -> INVISIBLE)activityProvider- AActivityinstance or null to reset- See Also:
Activity.playing(String),Activity.streaming(String, String)
-
setStatusProvider
public void setStatusProvider(java.util.function.IntFunction<OnlineStatus> statusProvider)
Description copied from interface:ShardManagerSets the provider that provides theOnlineStatusfor all shards.This will also change the provider for shards that are created in the future.
- Specified by:
setStatusProviderin interfaceShardManager- Parameters:
statusProvider- TheOnlineStatusto be used (OFFLINE/null -> INVISIBLE)
-
-