@Override public List<JDA> getElementsByName(String name, boolean ignoreCase) { Checks.notEmpty(name, "Name"); if (elements.isEmpty()) return Collections.emptyList(); List<JDA> list = new LinkedList<>(); for (JDA elem : elements.valueCollection()) { String elementName = elem.getShardInfo().getShardString(); if (elementName != null) { if (ignoreCase) { if (elementName.equalsIgnoreCase(name)) list.add(elem); } else { if (elementName.equals(name)) list.add(elem); } } } return list; }
/** * Sets the provider that decides for all shards whether they should be marked as afk or not. * * <p>This will also change the provider for shards that are created in the future. * * @param idleProvider * boolean */ default void setIdleProvider(final IntFunction<Boolean> idleProvider) { this.getShardCache().forEach(jda -> jda.getPresence().setIdle(idleProvider.apply(jda.getShardInfo().getShardId()))); }
/** * Sets the provider that provides the {@link net.dv8tion.jda.core.OnlineStatus OnlineStatus} for all shards. * * <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} * to be used (OFFLINE/null {@literal ->} INVISIBLE) */ default void setStatusProvider(final IntFunction<OnlineStatus> statusProvider) { this.getShardCache().forEach(jda -> jda.getPresence().setStatus(statusProvider.apply(jda.getShardInfo().getShardId()))); }
/** * Sets provider that provider the {@link net.dv8tion.jda.core.entities.Game Game} for all shards. * <br>A Game can be retrieved via {@link net.dv8tion.jda.core.entities.Game#playing(String)}. * For streams you provide a valid streaming url as second parameter. * * <p>This will also change the provider for shards that are created in the future. * * @param gameProvider * A {@link net.dv8tion.jda.core.entities.Game Game} instance or null to reset * * @see net.dv8tion.jda.core.entities.Game#playing(String) * @see net.dv8tion.jda.core.entities.Game#streaming(String, String) */ default void setGameProvider(final IntFunction<? extends Game> gameProvider) { this.getShardCache().forEach(jda -> jda.getPresence().setGame(gameProvider.apply(jda.getShardInfo().getShardId()))); }
/** * Remove listeners from shards by their id. * 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 * * @throws java.lang.IllegalArgumentException * If the provided event listeners provider is {@code null}. */ default void removeEventListeners(final IntFunction<Collection<Object>> eventListenerProvider) { Checks.notNull(eventListenerProvider, "event listener provider"); this.getShardCache().forEach(jda -> jda.removeEventListener(eventListenerProvider.apply(jda.getShardInfo().getShardId())) ); }
/** * Adds 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. * * <p>Note: when using the {@link net.dv8tion.jda.core.hooks.InterfacedEventManager InterfacedEventListener} (default), * given listener <b>must</b> be instance of {@link net.dv8tion.jda.core.hooks.EventListener EventListener}! * * @param eventListenerProvider * The provider of listener(s) which will react to events. * * @throws java.lang.IllegalArgumentException * If the provided listener provider or any of the listeners or provides are {@code null}. */ default void addEventListeners(final IntFunction<Object> eventListenerProvider) { Checks.notNull(eventListenerProvider, "event listener provider"); this.getShardCache().forEach(jda -> { Object listener = eventListenerProvider.apply(jda.getShardInfo().getShardId()); if (listener != null) jda.addEventListener(listener); }); }
/** * Get the shard ID of a JDA instance, if the JDA instance doesn't have ShardInfo (aka not sharded) then it will * return 0. * * @param jda The JDA instance of a certain shard. * @return The JDA shard ID as an integer. */ public static int getShardId(JDA jda) { if (jda.getShardInfo() == null) return 0; return jda.getShardInfo().getShardId(); }
@Override public void onGenericEvent(Event e) { shardEventTime.put(e.getJDA().getShardInfo() == null ? 0 : e.getJDA().getShardInfo().getShardId(), System.currentTimeMillis()); }
@Override public void onStatusChange(StatusChangeEvent event) { if (FlareBot.EXITING.get()) return; String statusHook = FlareBot.getStatusHook(); if (statusHook == null) return; Request.Builder request = new Request.Builder().url(statusHook); RequestBody body = RequestBody.create(WebUtils.APPLICATION_JSON, new JSONObject() .put("content", String.format("onStatusChange: %s -> %s SHARD: %d", event.getOldStatus(), event.getNewStatus(), event.getJDA().getShardInfo() != null ? event.getJDA().getShardInfo().getShardId() : null)).toString()); WebUtils.postAsync(request.post(body)); }
/** * Checks if a shard is reconnecting using the provided JDA instance. * * @param jda The shard to check for reconnecting. * @return If the shard is reconnecting or not. * @see ShardUtils#isReconnecting(int) */ public static boolean isReconnecting(JDA jda) { return isReconnecting(jda.getShardInfo().getShardId()); }
/** * Checks if a shard is dead by comparing the last event time to the provided timeout. * * @param jda The shard to check for being dead. * @param timeout The timeout the compare the last event time * @return Whether the shard is dead or not. */ public static boolean isDead(JDA jda, long timeout) { return isDead(jda.getShardInfo().getShardId(), timeout); }
/** * Checks if a shard is dead by comparing the last event time to the {@link ShardUtils#POSSIBLE_DEAD_SHARD_TIMEOUT}. * * @param jda The shard to check for being dead. * @return Whether the shard is dead or not. */ public static boolean isDead(JDA jda) { return isDead(jda.getShardInfo().getShardId(), POSSIBLE_DEAD_SHARD_TIMEOUT); }
private String formatGame(AvaIre avaire, String game, JDA shard) { game = game.replaceAll("%users%", NumberUtil.formatNicely(avaire.getShardEntityCounter().getUsers())); game = game.replaceAll("%guilds%", NumberUtil.formatNicely(avaire.getShardEntityCounter().getGuilds())); game = game.replaceAll("%shard%", shard.getShardInfo().getShardString()); game = game.replaceAll("%shard-id%", "" + shard.getShardInfo().getShardId()); game = game.replaceAll("%shard-total%", "" + shard.getShardInfo().getShardTotal()); return game; } }
private JSONArray buildShards() { JSONArray shards = new JSONArray(); for (JDA shard : metrics.getAvaire().getShardManager().getShards()) { JSONObject stats = new JSONObject(); stats.put("id", shard.getShardInfo().getShardId()) .put("guilds", shard.getGuilds().size()) .put("users", shard.getUsers().size()) .put("status", shard.getStatus()) .put("channels", (shard.getTextChannels().size() + shard.getVoiceChannels().size())) .put("latency", shard.getPing()); shards.put(stats); } return shards; }
@Override public void run(Context context) { MessageReceivedEvent event = context.getEvent(); JDA jda = event.getJDA(); ShardManager manager = jda.asBot().getShardManager(); String message = manager.getShards().stream() .sorted(Comparator.comparingInt(a -> a.getShardInfo().getShardId())) .map(shard -> shard.getShardInfo().getShardId() + ": " + shard.getStatus()) .collect(Collectors.joining("\n")); context.reply("```\n%s```", message); } }
private String getQueuedRestActions() { String queued = FlareBot.instance().getShardManager().getShards().stream() .filter(shard -> !((JDAImpl) shard).getRequester().getRateLimiter().getQueuedRouteBuckets().isEmpty()) .map(shard -> "`" + shard.getShardInfo().getShardId() + "`: " + ((JDAImpl) shard).getRequester().getRateLimiter().getQueuedRouteBuckets().size()) .collect(Collectors.joining(", ")); if (queued.isEmpty()) queued = "No queued rest actions"; return queued; } }
@Override public void onUserUpdateOnlineStatus(UserUpdateOnlineStatusEvent event) { String shard = Integer.toString(event.getJDA().getShardInfo().getShardId()); int memberCount = event.getJDA().getGuildCache().stream().flatMap(g -> g.getMembers().stream()).collect(Collectors.toList()).size(); members.labels(shard).set(memberCount); }
private final void setGuildCount(JDA jda) { long count = jda.getGuildCache().size(); String shard = Integer.toString(jda.getShardInfo().getShardId()); guilds.labels(shard).set(count); } }
@Override public void onStatusChange(StatusChangeEvent event) { discordBot.getContainer().reportStatus(event.getJDA().getShardInfo() != null ? event.getJDA().getShardInfo().getShardId() : 0, event.getOldStatus(), event.getStatus()); }
@Override public void handle(AvaIre avaire) { if (!avaire.areWeReadyYet()) { return; } for (JDA shard : avaire.getShardManager().getShards()) { Metrics.websocketHeartbeat.labels("Shard " + shard.getShardInfo().getShardId()) .set(shard.getPing()); } } }