Skip to content

Commit

Permalink
Added setPresence to ShardManager and improved documentation consiste…
Browse files Browse the repository at this point in the history
…ncy (#811)
  • Loading branch information
averen authored and MinnDevelopment committed Nov 17, 2018
1 parent 2b0eff5 commit de8a7d1
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,15 @@ public void setIdleProvider(IntFunction<Boolean> idleProvider)
this.idleProvider = idleProvider;
}

@Override
public void setPresenceProvider(IntFunction<OnlineStatus> statusProvider, IntFunction<? extends Game> gameProvider)
{
ShardManager.super.setPresenceProvider(statusProvider, gameProvider);

this.statusProvider = statusProvider;
this.gameProvider = gameProvider;
}

@Override
public void setStatusProvider(IntFunction<OnlineStatus> statusProvider)
{
Expand Down
111 changes: 85 additions & 26 deletions src/main/java/net/dv8tion/jda/bot/sharding/ShardManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ default void addEventListeners(final IntFunction<Object> eventListenerProvider)
* The provider takes shard ids, and returns a collection of listeners that shall be removed from the respective
* shards.
*
* @param eventListenerProvider
* gets shard ids applied and is expected to return a collection of listeners that shall be removed from
* the respective shards
* @param eventListenerProvider
* Gets shard ids applied and is expected to return a collection of listeners that shall be removed from
* the respective shards
*
* @throws java.lang.IllegalArgumentException
* If the provided event listeners provider is {@code null}.
Expand Down Expand Up @@ -224,9 +224,11 @@ default List<Category> getCategories()
* The name to check
* @param ignoreCase
* Whether to ignore case on name checking
* @return Immutable list of all categories matching the provided name
*
* @throws java.lang.IllegalArgumentException
* If the provided name is {@code null}
*
* @return Immutable list of all categories matching the provided name
*/
default List<Category> getCategoriesByName(final String name, final boolean ignoreCase)
{
Expand All @@ -252,9 +254,11 @@ default Category getCategoryById(final long id)
*
* @param id
* The snowflake ID of the wanted Category
* @return Possibly-null {@link net.dv8tion.jda.core.entities.Category Category} for the provided ID.
*
* @throws java.lang.IllegalArgumentException
* If the provided ID is not a valid {@code long}
*
* @return Possibly-null {@link net.dv8tion.jda.core.entities.Category Category} for the provided ID.
*/
default Category getCategoryById(final String id)
{
Expand All @@ -279,8 +283,9 @@ default SnowflakeCacheView<Category> getCategoryCache()
*
* @param id
* The id of the requested {@link net.dv8tion.jda.core.entities.Emote}.
*
* @return An {@link net.dv8tion.jda.core.entities.Emote Emote} represented by this id or null if none is found in
* our cache.
* our cache.
*/
default Emote getEmoteById(final long id)
{
Expand All @@ -294,10 +299,12 @@ default Emote getEmoteById(final long id)
*
* @param id
* The id of the requested {@link net.dv8tion.jda.core.entities.Emote}.
* @return An {@link net.dv8tion.jda.core.entities.Emote Emote} represented by this id or null if none is found in
* our cache.
*
* @throws java.lang.NumberFormatException
* If the provided {@code id} cannot be parsed by {@link Long#parseLong(String)}
*
* @return An {@link net.dv8tion.jda.core.entities.Emote Emote} represented by this id or null if none is found in
* our cache.
*/
default Emote getEmoteById(final String id)
{
Expand Down Expand Up @@ -343,6 +350,7 @@ default List<Emote> getEmotes()
* @param ignoreCase
* Whether to ignore case or not when comparing the provided name to each {@link
* net.dv8tion.jda.core.entities.Emote#getName()}.
*
* @return Possibly-empty list of all the {@link net.dv8tion.jda.core.entities.Emote Emotes} that all have the same
* name as the provided name.
*/
Expand Down Expand Up @@ -421,7 +429,7 @@ default List<Guild> getGuilds()
* Gets all {@link net.dv8tion.jda.core.entities.Guild Guilds} that contain all given users as their members.
*
* @param users
* The users which all the returned {@link net.dv8tion.jda.core.entities.Guild Guilds} must contain.
* The users which all the returned {@link net.dv8tion.jda.core.entities.Guild Guilds} must contain.
*
* @return Unmodifiable list of all {@link net.dv8tion.jda.core.entities.Guild Guild} instances which have all {@link net.dv8tion.jda.core.entities.User Users} in them.
*/
Expand Down Expand Up @@ -553,6 +561,7 @@ default PrivateChannel getPrivateChannelById(final long id)
*
* @param id
* The id of the {@link net.dv8tion.jda.core.entities.PrivateChannel PrivateChannel}.
*
* @throws java.lang.NumberFormatException
* If the provided {@code id} cannot be parsed by {@link Long#parseLong(String)}
*
Expand Down Expand Up @@ -591,6 +600,7 @@ default List<PrivateChannel> getPrivateChannels()
*
* @param id
* The id of the searched Role
*
* @return Possibly-null {@link net.dv8tion.jda.core.entities.Role Role} for the specified ID
*/
default Role getRoleById(final long id)
Expand All @@ -605,9 +615,11 @@ default Role getRoleById(final long id)
*
* @param id
* The id of the searched Role
* @return Possibly-null {@link net.dv8tion.jda.core.entities.Role Role} for the specified ID
*
* @throws java.lang.NumberFormatException
* If the provided {@code id} cannot be parsed by {@link Long#parseLong(String)}
*
* @return Possibly-null {@link net.dv8tion.jda.core.entities.Role Role} for the specified ID
*/
default Role getRoleById(final String id)
{
Expand Down Expand Up @@ -646,6 +658,7 @@ default List<Role> getRoles()
* The name for the Roles
* @param ignoreCase
* Whether to use {@link String#equalsIgnoreCase(String)}
*
* @return Immutable List of all Roles matching the parameters provided.
*/
default List<Role> getRolesByName(final String name, final boolean ignoreCase)
Expand Down Expand Up @@ -708,7 +721,7 @@ default List<JDA> getShards()
* @param shardId
* The id of the shard.
*
* @return The {@link net.dv8tion.jda.core.JDA.Status JDA.Status} of the shard with the given shardId or
* @return The {@link net.dv8tion.jda.core.JDA.Status JDA.Status} of the shard with the given shardId or
* {@code null} if no shard has the given id
*/
default JDA.Status getStatus(final int shardId)
Expand Down Expand Up @@ -858,7 +871,8 @@ default List<User> getUsers()
* <br>If there is no known {@link net.dv8tion.jda.core.entities.VoiceChannel VoiceChannel} with an id that matches the provided
* one, then this returns {@code null}.
*
* @param id The id of the {@link net.dv8tion.jda.core.entities.VoiceChannel VoiceChannel}.
* @param id
* The id of the {@link net.dv8tion.jda.core.entities.VoiceChannel VoiceChannel}.
*
* @return Possibly-null {@link net.dv8tion.jda.core.entities.VoiceChannel VoiceChannel} with matching id.
*/
Expand Down Expand Up @@ -918,8 +932,8 @@ default List<VoiceChannel> getVoiceChannels()
* @param id
* The id of the target shard
*
* @throws IllegalArgumentException
* if shardId is negative or higher than maxShardId
* @throws java.lang.IllegalArgumentException
* If shardId is negative or higher than maxShardId
*/
void restart(int id);

Expand Down Expand Up @@ -967,7 +981,7 @@ default void setGameProvider(final IntFunction<? extends Activity> gameProvider)
*
* <p>This will also change the value for shards that are created in the future.
*
* @param idle
* @param idle
* boolean
*/
default void setIdle(final boolean idle)
Expand All @@ -980,7 +994,7 @@ default void setIdle(final boolean idle)
*
* <p>This will also change the provider for shards that are created in the future.
*
* @param idleProvider
* @param idleProvider
* boolean
*/
default void setIdleProvider(final IntFunction<Boolean> idleProvider)
Expand All @@ -989,16 +1003,61 @@ default void setIdleProvider(final IntFunction<Boolean> idleProvider)
}

/**
* Sets the {@link net.dv8tion.jda.core.OnlineStatus OnlineStatus} for all shards.
* Sets the {@link net.dv8tion.jda.core.OnlineStatus OnlineStatus} and {@link net.dv8tion.jda.core.entities.Game Game} for all shards.
*
* <p>This will also change the status for shards that are created in the future.
*
* @throws IllegalArgumentException
* if the provided OnlineStatus is {@link net.dv8tion.jda.core.OnlineStatus#UNKNOWN UNKNOWN}
* @param status
* The {@link net.dv8tion.jda.core.OnlineStatus OnlineStatus}
* to be used (OFFLINE/null {@literal ->} INVISIBLE)
* @param game
* A {@link net.dv8tion.jda.core.entities.Game Game} instance or null to reset
*
* @throws java.lang.IllegalArgumentException
* If the provided OnlineStatus is {@link net.dv8tion.jda.core.OnlineStatus#UNKNOWN UNKNOWN}
*
* @see net.dv8tion.jda.core.entities.Game#playing(String)
* @see net.dv8tion.jda.core.entities.Game#streaming(String, String)
*/
default void setPresence(final OnlineStatus status, final Game game)
{
this.setPresenceProvider(id -> status, id -> game);
}

/**
* Sets the provider that provides the {@link net.dv8tion.jda.core.OnlineStatus OnlineStatus} and
* {@link net.dv8tion.jda.core.entities.Game Game} for all shards.
*
* <p>This will also change the status for shards that are created in the future.
*
* @param statusProvider
* The {@link net.dv8tion.jda.core.OnlineStatus OnlineStatus}
* to be used (OFFLINE/null {@literal ->} INVISIBLE)
* @param gameProvider
* A {@link net.dv8tion.jda.core.entities.Game Game} instance or null to reset
*
* @throws java.lang.IllegalArgumentException
* If the provided OnlineStatus is {@link net.dv8tion.jda.core.OnlineStatus#UNKNOWN UNKNOWN}
*
* @see net.dv8tion.jda.core.entities.Game#playing(String)
* @see net.dv8tion.jda.core.entities.Game#streaming(String, String)
*/
default void setPresenceProvider(final IntFunction<OnlineStatus> statusProvider, final IntFunction<? extends Game> gameProvider)
{
this.getShardCache().forEach(jda -> jda.getPresence().setPresence(statusProvider.apply(jda.getShardInfo().getShardId()), gameProvider.apply(jda.getShardInfo().getShardId())));
}

/**
* Sets the {@link net.dv8tion.jda.core.OnlineStatus OnlineStatus} for all shards.
*
* <p>This will also change the status for shards that are created in the future.
*
* @param status
* the {@link net.dv8tion.jda.core.OnlineStatus OnlineStatus}
* The {@link net.dv8tion.jda.core.OnlineStatus OnlineStatus}
* to be used (OFFLINE/null {@literal ->} INVISIBLE)
*
* @throws java.lang.IllegalArgumentException
* If the provided OnlineStatus is {@link net.dv8tion.jda.core.OnlineStatus#UNKNOWN UNKNOWN}
*/
default void setStatus(final OnlineStatus status)
{
Expand All @@ -1010,12 +1069,12 @@ default void setStatus(final OnlineStatus status)
*
* <p>This will also change the provider for shards that are created in the future.
*
* @throws IllegalArgumentException
* if the provided OnlineStatus is {@link net.dv8tion.jda.core.OnlineStatus#UNKNOWN UNKNOWN}
*
* @param statusProvider
* the {@link net.dv8tion.jda.core.OnlineStatus OnlineStatus}
* The {@link net.dv8tion.jda.core.OnlineStatus OnlineStatus}
* to be used (OFFLINE/null {@literal ->} INVISIBLE)
*
* @throws java.lang.IllegalArgumentException
* If the provided OnlineStatus is {@link net.dv8tion.jda.core.OnlineStatus#UNKNOWN UNKNOWN}
*/
default void setStatusProvider(final IntFunction<OnlineStatus> statusProvider)
{
Expand All @@ -1032,15 +1091,15 @@ default void setStatusProvider(final IntFunction<OnlineStatus> statusProvider)
* Shuts down the shard with the given id only.
* <br> This does nothing if there is no shard with the given id.
*
* @param shardId
* @param shardId
* The id of the shard that should be stopped
*/
void shutdown(int shardId);

/**
* Adds a new shard with the given id to this ShardManager and starts it.
*
* @param shardId
* @param shardId
* The id of the shard that should be started
*/
void start(int shardId);
Expand Down

0 comments on commit de8a7d1

Please sign in to comment.