/** * The {@link net.dv8tion.jda.core.entities.SelfUser SelfUser} * * @return The {@link net.dv8tion.jda.core.entities.SelfUser SelfUser} */ public SelfUser getSelfUser() { return api.getSelfUser(); }
/** * This returns the {@link net.dv8tion.jda.core.JDA.Status JDA.Status} of the shard which has the same id as the one provided. * <br>If there is no shard with an id that matches the provided one, then this returns {@code null}. * * @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 * {@code null} if no shard has the given id */ default JDA.Status getStatus(final int shardId) { final JDA jda = this.getShardCache().getElementById(shardId); return jda == null ? null : jda.getStatus(); }
/** * 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); }); }
@Override public void shutdown(final int shardId) { final JDA jda = this.shards.getMap().remove(shardId); if (jda != null) { if (this.useShutdownNow) jda.shutdownNow(); else jda.shutdown(); } }
/** * 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()))); }
/** * 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())) ); }
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; }
public void sendStatsToDiscordbotsOrg() { if (BotConfig.BOT_TOKEN_DISCORDBOTS_ORG.length() < 10) { return; } JSONObject data = new JSONObject(); data.put("server_count", getJda().getGuilds().size()); if (totShards > 1) { data.put("shard_id", shardId); data.put("shard_count", totShards); } Unirest.post("https://discordbots.org/api/bots/" + getJda().getSelfUser().getId() + "/stats") .header("Authorization", BotConfig.BOT_TOKEN_DISCORDBOTS_ORG) .header("Content-Type", "application/json") .body(data.toString()) .asJsonAsync(); }
public void stopStuff(Boolean fake) { if (!fake) { if (jda != null) { DiscordHandler.sendMessageToChannel(Config.CHANNELS.MAIN_CHANNEL, Config.MESSAGES.SERVER_STOPPING); DiscordHandler.close(); if (updater != null) updater.interrupt(); jda.getTextChannelById(Config.CHANNELS.MAIN_CHANNEL).getManager().setTopic(FormatType.OFFLINE_TOPIC_FORMAT.get()).queue(); jda.shutdown(); long time = System.currentTimeMillis(); while ((System.currentTimeMillis() - time < 10000) && (jda != null && jda.getStatus() != JDA.Status.SHUTDOWN)) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } return; } } logger.info("Disconnecting from Discord..."); if (jda != null && jda.getStatus() != JDA.Status.SHUTDOWN && jda.getStatus() != JDA.Status.SHUTTING_DOWN) { jda.shutdownNow(); } // Unregistering listeners Sponge.getEventManager().unregisterPluginListeners(this); Sponge.getEventManager().registerListeners(this, this); Config = null; }
while (matcher.find()) TextChannel channel = jda.getTextChannelById(matcher.group(1)); if (channel != null) while (matcher.find()) for (Guild g : jda.getGuilds()) while (matcher.find()) User user = jda.getUserById(matcher.group(1)); String replacement;
@Nullable public SelfUser getSelfUser() { for (JDA shard : getShardManager().getShards()) { if (shard.getStatus().equals(JDA.Status.CONNECTED)) { return shard.getSelfUser(); } } return null; }
public void shutdown() { if(shuttingDown) return; shuttingDown = true; threadpool.shutdownNow(); if(jda.getStatus()!=JDA.Status.SHUTTING_DOWN) { jda.getGuilds().stream().forEach(g -> { g.getAudioManager().closeAudioConnection(); AudioHandler ah = (AudioHandler)g.getAudioManager().getSendingHandler(); if(ah!=null) { ah.stopAndClear(); ah.getPlayer().destroy(); nowplaying.updateTopic(g.getIdLong(), ah, true); } }); jda.shutdown(); } if(gui!=null) gui.dispose(); System.exit(0); }
private static boolean isValidChannel(String channel) { if (MagiBridge.jda == null) return false; if (MagiBridge.jda.getStatus() != JDA.Status.CONNECTED) return false; if (MagiBridge.jda.getTextChannelById(channel) == null) { MagiBridge.getLogger().error("The channel " + channel + " defined in the config isn't a valid Discord Channel ID!"); MagiBridge.getLogger().error("Replace it with a valid one then reload the plugin!"); return false; } return true; }
@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 buildPayload(SelfUser selfUser) { JSONObject main = new JSONObject(); JSONObject bot = new JSONObject(); bot.put("name", selfUser.getName()); bot.put("avatar", selfUser.getAvatarId()); main.put("bot", bot); JSONArray shards = new JSONArray(); for (JDA shard : avaire.getShardManager().getShards()) { JSONObject shardObj = new JSONObject(); shardObj.put("id", shard.getShardInfo().getShardId()); shardObj.put("latency", shard.getPing()); shardObj.put("users", shard.getUsers().size()); shardObj.put("channels", getTotalChannels(shard)); shardObj.put("guilds", shard.getGuilds().size()); shards.put(shardObj); } main.put("shards", shards); return main.toString(); }
@Override public void onReady(ReadyEvent event) if(event.getJDA().getGuilds().isEmpty()) log.warn(event.getJDA().asBot().getInviteUrl(JMusicBot.RECOMMENDED_PERMS)); event.getJDA().getGuilds().forEach((guild) -> User owner = bot.getJDA().getUserById(bot.getConfig().getOwnerId()); if(owner!=null)
/** * The user associated with the id * * @return Possibly-null user associated with the id */ @Nullable public User getUser() { return getJDA().getUserById(userId); }
public void resetGame() { Game game = config.getGame()==null || config.getGame().getName().equalsIgnoreCase("none") ? null : config.getGame(); if(!Objects.equals(jda.getPresence().getGame(), game)) jda.getPresence().setGame(game); }
void initializeBotOwner() { if (botOwner == 0L) { if (jda.getAccountType() == AccountType.BOT) { botOwner = jda.asBot().getApplicationInfo().complete().getOwner().getIdLong(); } else { botOwner = jda.getSelfUser().getIdLong(); } } }
@Override public synchronized List<TextChannel> getMentionedChannels() { if (channelMentions == null) { channelMentions = new ArrayList<>(); Matcher matcher = MentionType.CHANNEL.getPattern().matcher(content); while (matcher.find()) { try { String id = matcher.group(1); TextChannel channel = getJDA().getTextChannelById(id); if (channel != null && !channelMentions.contains(channel)) channelMentions.add(channel); } catch (NumberFormatException ignored) {} } channelMentions = Collections.unmodifiableList(channelMentions); } return channelMentions; }