Search in sources :

Example 1 with ShardCacheView

use of net.dv8tion.jda.api.utils.cache.ShardCacheView in project SkyBot by duncte123.

the class ShardInfoCommand method embedTable.

private void embedTable(CommandContext ctx) {
    final EmbedBuilder embedBuilder = new EmbedBuilder();
    final int currentShard = ctx.getJDA().getShardInfo().getShardId();
    final ShardManager shardManager = ctx.getJDA().getShardManager();
    final ShardCacheView shardCache = shardManager.getShardCache();
    final List<JDA> shards = new ArrayList<>(shardCache.asList());
    Collections.reverse(shards);
    for (final JDA shard : shards) {
        final StringBuilder valueBuilder = new StringBuilder();
        final LongLongPair channelStats = getConnectedVoiceChannels(shard);
        valueBuilder.append("**Status:** ").append(getShardStatus(shard)).append("\n**Ping:** ").append(shard.getGatewayPing()).append("\n**Guilds:** ").append(shard.getGuildCache().size()).append("\n**VCs:** ").append(channelStats.getFirst()).append(" / ").append(channelStats.getSecond());
        final int shardId = shard.getShardInfo().getShardId();
        embedBuilder.addField(String.format("Shard #%s%s", shardId, shardId == currentShard ? " (current)" : ""), valueBuilder.toString(), true);
    }
    final long connectedShards = shardCache.applyStream((s) -> s.filter((shard) -> shard.getStatus() == JDA.Status.CONNECTED).count());
    final String avgPing = new DecimalFormat("###").format(shardManager.getAverageGatewayPing());
    final long guilds = shardManager.getGuildCache().size();
    final LongLongPair channelStats = getConnectedVoiceChannels(shardManager);
    embedBuilder.addField("Total/Average", String.format("**Connected:** %s\n**Ping:** %s\n**Guilds:** %s\n**VCs:** %s / %s", connectedShards, avgPing, guilds, channelStats.getFirst(), channelStats.getSecond()), false);
    sendEmbed(ctx, embedBuilder);
}
Also used : EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) LongLongPair(ml.duncte123.skybot.objects.pairs.LongLongPair) JDA(net.dv8tion.jda.api.JDA) ShardCacheView(net.dv8tion.jda.api.utils.cache.ShardCacheView) DecimalFormat(java.text.DecimalFormat) ArrayList(java.util.ArrayList) ShardManager(net.dv8tion.jda.api.sharding.ShardManager)

Example 2 with ShardCacheView

use of net.dv8tion.jda.api.utils.cache.ShardCacheView in project SkyBot by DuncteBot.

the class ShardInfoCommand method makeAsciiTable.

/*
     * These 2 functions have been inspired from FlareBot
     * https://github.com/FlareBot/FlareBot/blob/master/src/main/java/stream/flarebot/flarebot/util/ShardUtils.java
     */
private String makeAsciiTable(List<String> headers, List<List<String>> table, ShardManager shardManager) {
    final StringBuilder builder = new StringBuilder();
    final int padding = 1;
    final int[] widths = new int[headers.size()];
    for (int i = 0; i < headers.size(); i++) {
        if (headers.get(i).length() > widths[i]) {
            widths[i] = headers.get(i).length();
        }
    }
    for (final List<String> row : table) {
        for (int i = 0; i < row.size(); i++) {
            final String cell = row.get(i);
            if (cell.length() > widths[i]) {
                widths[i] = cell.length();
            }
        }
    }
    final LongLongPair channelStats = getConnectedVoiceChannels(shardManager);
    final String statsString = channelStats.getFirst() + " / " + channelStats.getSecond();
    if (statsString.length() > widths[widths.length - 1]) {
        widths[widths.length - 1] = statsString.length();
    }
    if (widths[0] < 7) {
        widths[0] = 7;
    }
    builder.append("```prolog\n");
    final StringBuilder formatLine = new StringBuilder("║");
    for (final int width : widths) {
        formatLine.append(" %-").append(width).append("s ║");
    }
    formatLine.append('\n');
    builder.append(appendSeparatorLine("╔", "╦", "╗", padding, widths)).append(String.format(formatLine.toString(), headers.toArray())).append(appendSeparatorLine("╠", "╬", "╣", padding, widths));
    for (final List<String> row : table) {
        builder.append(String.format(formatLine.toString(), row.toArray()));
    }
    builder.append(appendSeparatorLine("╠", "╬", "╣", padding, widths));
    final ShardCacheView shardCache = shardManager.getShardCache();
    final long shards = shardCache.applyStream((s) -> s.filter(shard -> shard.getStatus() == JDA.Status.CONNECTED).count());
    final String connectedShards = String.valueOf(shards);
    final String avgPing = new DecimalFormat("###").format(shardManager.getAverageGatewayPing());
    final String guilds = String.valueOf(shardManager.getGuildCache().size());
    builder.append(String.format(formatLine.toString(), "Sum/Avg", connectedShards, avgPing, guilds, statsString)).append(appendSeparatorLine("╚", "╩", "╝", padding, widths)).append("```");
    return builder.toString();
}
Also used : LongLongPair(ml.duncte123.skybot.objects.pairs.LongLongPair) ShardCacheView(net.dv8tion.jda.api.utils.cache.ShardCacheView) DecimalFormat(java.text.DecimalFormat)

Example 3 with ShardCacheView

use of net.dv8tion.jda.api.utils.cache.ShardCacheView in project Robertify-Bot by bombies.

the class ShardInfoCommand method onSlashCommand.

@Override
public void onSlashCommand(@NotNull SlashCommandEvent event) {
    if (!checks(event))
        return;
    final EmbedBuilder embedBuilder = RobertifyEmbedUtils.getEmbedBuilder(event.getGuild());
    final int currentShard = event.getJDA().getShardInfo().getShardId();
    final ShardManager shardManager = event.getJDA().getShardManager();
    final ShardCacheView shardCache = shardManager.getShardCache();
    final List<JDA> shards = new ArrayList<>(shardCache.asList());
    Collections.reverse(shards);
    for (final JDA shard : shards) {
        final StringBuilder valueBuilder = new StringBuilder();
        valueBuilder.append("**Status:** ").append(getShardStatus(shard)).append("\n**Ping:** ").append(shard.getGatewayPing()).append("\n**Guilds:** ").append(shard.getGuildCache().size());
        final int shardId = shard.getShardInfo().getShardId();
        embedBuilder.addField(String.format("Shard #%s%s", shardId, shardId == currentShard ? " (current)" : ""), valueBuilder.toString(), true);
    }
    final long connectedShards = shardCache.applyStream((s) -> s.filter((shard) -> shard.getStatus() == JDA.Status.CONNECTED).count());
    final String avgPing = new DecimalFormat("###").format(shardManager.getAverageGatewayPing());
    final long guilds = shardManager.getGuildCache().size();
    embedBuilder.addField("Total/Average", String.format("**Connected:** %s\n**Ping:** %s\n**Guilds:** %s", connectedShards, avgPing, guilds), false);
    event.replyEmbeds(embedBuilder.build()).setEphemeral(true).queue();
}
Also used : EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) JDA(net.dv8tion.jda.api.JDA) ShardCacheView(net.dv8tion.jda.api.utils.cache.ShardCacheView) DecimalFormat(java.text.DecimalFormat) ArrayList(java.util.ArrayList) ShardManager(net.dv8tion.jda.api.sharding.ShardManager)

Example 4 with ShardCacheView

use of net.dv8tion.jda.api.utils.cache.ShardCacheView in project SkyBot by duncte123.

the class ShardInfoCommand method makeAsciiTable.

/*
     * These 2 functions have been inspired from FlareBot
     * https://github.com/FlareBot/FlareBot/blob/master/src/main/java/stream/flarebot/flarebot/util/ShardUtils.java
     */
private String makeAsciiTable(List<String> headers, List<List<String>> table, ShardManager shardManager) {
    final StringBuilder builder = new StringBuilder();
    final int padding = 1;
    final int[] widths = new int[headers.size()];
    for (int i = 0; i < headers.size(); i++) {
        if (headers.get(i).length() > widths[i]) {
            widths[i] = headers.get(i).length();
        }
    }
    for (final List<String> row : table) {
        for (int i = 0; i < row.size(); i++) {
            final String cell = row.get(i);
            if (cell.length() > widths[i]) {
                widths[i] = cell.length();
            }
        }
    }
    final LongLongPair channelStats = getConnectedVoiceChannels(shardManager);
    final String statsString = channelStats.getFirst() + " / " + channelStats.getSecond();
    if (statsString.length() > widths[widths.length - 1]) {
        widths[widths.length - 1] = statsString.length();
    }
    if (widths[0] < 7) {
        widths[0] = 7;
    }
    builder.append("```prolog\n");
    final StringBuilder formatLine = new StringBuilder("║");
    for (final int width : widths) {
        formatLine.append(" %-").append(width).append("s ║");
    }
    formatLine.append('\n');
    builder.append(appendSeparatorLine("╔", "╦", "╗", padding, widths)).append(String.format(formatLine.toString(), headers.toArray())).append(appendSeparatorLine("╠", "╬", "╣", padding, widths));
    for (final List<String> row : table) {
        builder.append(String.format(formatLine.toString(), row.toArray()));
    }
    builder.append(appendSeparatorLine("╠", "╬", "╣", padding, widths));
    final ShardCacheView shardCache = shardManager.getShardCache();
    final long shards = shardCache.applyStream((s) -> s.filter(shard -> shard.getStatus() == JDA.Status.CONNECTED).count());
    final String connectedShards = String.valueOf(shards);
    final String avgPing = new DecimalFormat("###").format(shardManager.getAverageGatewayPing());
    final String guilds = String.valueOf(shardManager.getGuildCache().size());
    builder.append(String.format(formatLine.toString(), "Sum/Avg", connectedShards, avgPing, guilds, statsString)).append(appendSeparatorLine("╚", "╩", "╝", padding, widths)).append("```");
    return builder.toString();
}
Also used : LongLongPair(ml.duncte123.skybot.objects.pairs.LongLongPair) ShardCacheView(net.dv8tion.jda.api.utils.cache.ShardCacheView) DecimalFormat(java.text.DecimalFormat)

Example 5 with ShardCacheView

use of net.dv8tion.jda.api.utils.cache.ShardCacheView in project SkyBot by DuncteBot.

the class ShardInfoCommand method embedTable.

private void embedTable(CommandContext ctx) {
    final EmbedBuilder embedBuilder = new EmbedBuilder();
    final int currentShard = ctx.getJDA().getShardInfo().getShardId();
    final ShardManager shardManager = ctx.getJDA().getShardManager();
    final ShardCacheView shardCache = shardManager.getShardCache();
    final List<JDA> shards = new ArrayList<>(shardCache.asList());
    Collections.reverse(shards);
    for (final JDA shard : shards) {
        final StringBuilder valueBuilder = new StringBuilder();
        final LongLongPair channelStats = getConnectedVoiceChannels(shard);
        valueBuilder.append("**Status:** ").append(getShardStatus(shard)).append("\n**Ping:** ").append(shard.getGatewayPing()).append("\n**Guilds:** ").append(shard.getGuildCache().size()).append("\n**VCs:** ").append(channelStats.getFirst()).append(" / ").append(channelStats.getSecond());
        final int shardId = shard.getShardInfo().getShardId();
        embedBuilder.addField(String.format("Shard #%s%s", shardId, shardId == currentShard ? " (current)" : ""), valueBuilder.toString(), true);
    }
    final long connectedShards = shardCache.applyStream((s) -> s.filter((shard) -> shard.getStatus() == JDA.Status.CONNECTED).count());
    final String avgPing = new DecimalFormat("###").format(shardManager.getAverageGatewayPing());
    final long guilds = shardManager.getGuildCache().size();
    final LongLongPair channelStats = getConnectedVoiceChannels(shardManager);
    embedBuilder.addField("Total/Average", String.format("**Connected:** %s\n**Ping:** %s\n**Guilds:** %s\n**VCs:** %s / %s", connectedShards, avgPing, guilds, channelStats.getFirst(), channelStats.getSecond()), false);
    sendEmbed(ctx, embedBuilder);
}
Also used : EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) LongLongPair(ml.duncte123.skybot.objects.pairs.LongLongPair) JDA(net.dv8tion.jda.api.JDA) ShardCacheView(net.dv8tion.jda.api.utils.cache.ShardCacheView) DecimalFormat(java.text.DecimalFormat) ArrayList(java.util.ArrayList) ShardManager(net.dv8tion.jda.api.sharding.ShardManager)

Aggregations

DecimalFormat (java.text.DecimalFormat)5 ShardCacheView (net.dv8tion.jda.api.utils.cache.ShardCacheView)5 LongLongPair (ml.duncte123.skybot.objects.pairs.LongLongPair)4 ArrayList (java.util.ArrayList)3 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)3 JDA (net.dv8tion.jda.api.JDA)3 ShardManager (net.dv8tion.jda.api.sharding.ShardManager)3