Search in sources :

Example 6 with JDA

use of net.dv8tion.jda.core.JDA in project FlareBot by FlareBot.

the class BotMetrics method count.

public boolean count() {
    if (FlareBot.instance().getShardManager() == null)
        return false;
    for (JDA jda : Getters.getShards()) {
        if (jda.getStatus() != JDA.Status.CONNECTED)
            return false;
    }
    this.guildCount = Getters.getGuildCache().size();
    this.userCount = Getters.getUserCache().size();
    this.textChannelCount = Getters.getTextChannelCache().size();
    this.voiceChannelCount = Getters.getVoiceChannelCache().size();
    return true;
}
Also used : JDA(net.dv8tion.jda.core.JDA)

Example 7 with JDA

use of net.dv8tion.jda.core.JDA in project FlareBot by FlareBot.

the class FlareBot method postToBotList.

// TODO: Spread this out a little so we don't just burst.
private void postToBotList(String auth, String url) {
    for (JDA client : shardManager.getShards()) {
        if (shardManager.getShardsTotal() == 1) {
            Request.Builder request = new Request.Builder().url(url).addHeader("Authorization", auth).addHeader("User-Agent", "Mozilla/5.0 FlareBot");
            RequestBody body = RequestBody.create(WebUtils.APPLICATION_JSON, new JSONObject().put("server_count", client.getGuilds().size()).toString());
            WebUtils.postAsync(request.post(body));
            return;
        }
        try {
            Request.Builder request = new Request.Builder().url(url).addHeader("Authorization", auth).addHeader("User-Agent", "Mozilla/5.0 FlareBot");
            RequestBody body = RequestBody.create(WebUtils.APPLICATION_JSON, (new JSONObject().put("server_count", client.getGuilds().size()).put("shard_id", client.getShardInfo().getShardId()).put("shard_count", client.getShardInfo().getShardTotal()).toString()));
            WebUtils.postAsync(request.post(body));
            // Gonna spread these out just a bit so we don't burst (insert shard number here) requests all at once
            Thread.sleep(20_000);
        } catch (Exception e1) {
            FlareBot.LOGGER.error("Could not POST data to a botlist", e1);
        }
    }
    LOGGER.debug("Sent " + shardManager.getShardsTotal() + " requests to " + url);
}
Also used : JSONObject(org.json.JSONObject) JDA(net.dv8tion.jda.core.JDA) Request(okhttp3.Request) ErrorResponseException(net.dv8tion.jda.core.exceptions.ErrorResponseException) IOException(java.io.IOException) UnknownBindingException(com.arsenarsen.lavaplayerbridge.libraries.UnknownBindingException) RequestBody(okhttp3.RequestBody)

Example 8 with JDA

use of net.dv8tion.jda.core.JDA in project Rubicon by Rubicon-Bot.

the class CommandPortal method closePortal.

private void closePortal(CommandManager.ParsedCommandInvocation parsedCommandInvocation) {
    JDA jda = parsedCommandInvocation.getMessage().getJDA();
    Guild messageGuild = parsedCommandInvocation.getMessage().getGuild();
    TextChannel messageChannel = parsedCommandInvocation.getMessage().getTextChannel();
    // Check if portal exists
    String oldGuildPortalEntry = RubiconBot.getMySQL().getGuildValue(messageGuild, "portal");
    if (oldGuildPortalEntry.equals("closed")) {
        messageChannel.sendMessage(EmbedUtil.error("Portal error!", "Portal is already closed").build()).queue();
        return;
    }
    if (oldGuildPortalEntry.equals("waiting")) {
        RubiconBot.getMySQL().updateGuildValue(messageGuild, "portal", "closed");
        messageChannel.sendMessage(EmbedUtil.success("Portal", "Successful closed portal request.").build()).queue();
        return;
    }
    Guild partnerGuild = jda.getGuildById(RubiconBot.getMySQL().getPortalValue(messageGuild, "partnerid"));
    // Close Channels
    TextChannel channelOne = null;
    TextChannel channelTwo = null;
    try {
        channelOne = jda.getTextChannelById(RubiconBot.getMySQL().getPortalValue(messageGuild, "channelid"));
        channelTwo = jda.getTextChannelById(RubiconBot.getMySQL().getPortalValue(partnerGuild, "channelid"));
    } catch (NullPointerException ignored) {
    // Channels doesn't exist
    }
    if (channelOne != null)
        channelOne.getManager().setName(closedChannelName).queue();
    if (channelTwo != null)
        channelTwo.getManager().setName(closedChannelName).queue();
    // Close and delete DB Portal
    RubiconBot.getMySQL().updateGuildValue(messageGuild, "portal", "closed");
    RubiconBot.getMySQL().deletePortal(messageGuild);
    RubiconBot.getMySQL().updateGuildValue(partnerGuild, "portal", "closed");
    RubiconBot.getMySQL().deletePortal(partnerGuild);
    EmbedBuilder portalClosedMessage = new EmbedBuilder();
    portalClosedMessage.setAuthor("Portal closed!", null, jda.getSelfUser().getEffectiveAvatarUrl());
    portalClosedMessage.setDescription("Portal was closed. Create a new one with `" + parsedCommandInvocation.getPrefix() + "portal create`");
    portalClosedMessage.setColor(Colors.COLOR_ERROR);
    channelOne.sendMessage(portalClosedMessage.build()).queue();
    portalClosedMessage.setDescription("Portal was closed. Create a new one with `" + parsedCommandInvocation.getPrefix() + "portal create`");
    channelTwo.sendMessage(portalClosedMessage.build()).queue();
    channelOne.getManager().setTopic("Portal closed").queue();
    channelTwo.getManager().setTopic("Portal closed").queue();
}
Also used : TextChannel(net.dv8tion.jda.core.entities.TextChannel) EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) JDA(net.dv8tion.jda.core.JDA) Guild(net.dv8tion.jda.core.entities.Guild)

Example 9 with JDA

use of net.dv8tion.jda.core.JDA in project SkyBot by duncte123.

the class ShardInfoCommand method executeCommand.

@Override
public void executeCommand(@NotNull String invoke, @NotNull String[] args, @NotNull GuildMessageReceivedEvent event) {
    List<String> headers = new ArrayList<>();
    headers.add("Shard ID");
    headers.add("Status");
    headers.add("Ping");
    headers.add("Guild Count");
    headers.add("Connected VCs");
    List<List<String>> table = new ArrayList<>();
    List<JDA> shards = new ArrayList<>(event.getJDA().asBot().getShardManager().getShards());
    Collections.reverse(shards);
    for (JDA jda : shards) {
        List<String> row = new ArrayList<>();
        row.add((jda.getShardInfo().getShardId() + 1) + (event.getJDA().getShardInfo().getShardId() == jda.getShardInfo().getShardId() ? " (current)" : ""));
        row.add(WordUtils.capitalizeFully(jda.getStatus().toString().replace("_", " ")));
        row.add(String.valueOf(jda.getPing()));
        row.add(String.valueOf(jda.getGuilds().size()));
        row.add(String.valueOf(jda.getVoiceChannels().stream().filter(vc -> vc.getMembers().contains(vc.getGuild().getSelfMember())).count()));
        table.add(row);
        if (table.size() == 20) {
            MessageUtils.sendMsg(event, makeAsciiTable(headers, table));
            table = new ArrayList<>();
        }
    }
    if (table.size() > 0) {
        MessageUtils.sendMsg(event, makeAsciiTable(headers, table));
    }
}
Also used : GuildMessageReceivedEvent(net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent) List(java.util.List) WordUtils(org.apache.commons.lang3.text.WordUtils) Command(ml.duncte123.skybot.objects.command.Command) MessageUtils(ml.duncte123.skybot.utils.MessageUtils) CommandCategory(ml.duncte123.skybot.objects.command.CommandCategory) JDA(net.dv8tion.jda.core.JDA) NotNull(org.jetbrains.annotations.NotNull) StringUtils(org.apache.commons.lang3.StringUtils) Collections(java.util.Collections) ArrayList(java.util.ArrayList) JDA(net.dv8tion.jda.core.JDA) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList)

Example 10 with JDA

use of net.dv8tion.jda.core.JDA in project SkyBot by duncte123.

the class FakeTest method medium.

/**
 * Test medium
 * Mock Lists, don't share objects
 */
@Test
public void medium() {
    Member member = new FakeInterface<>(Member.class).create();
    JDA jda = member.getJDA();
    assertNotEquals(jda, member.getJDA());
    assertNotEquals(jda.getGuildById(member.getGuild().getId()), member.getGuild());
    assertEquals(jda.getGuilds(), new ArrayList<>());
}
Also used : JDA(net.dv8tion.jda.core.JDA) Member(net.dv8tion.jda.core.entities.Member) Test(org.junit.Test)

Aggregations

JDA (net.dv8tion.jda.core.JDA)42 Guild (net.dv8tion.jda.core.entities.Guild)10 TextChannel (net.dv8tion.jda.core.entities.TextChannel)9 Document (org.bson.Document)9 Permission (net.dv8tion.jda.core.Permission)7 Main (ws.nmathe.saber.Main)7 Consumer (java.util.function.Consumer)6 Message (net.dv8tion.jda.core.entities.Message)6 JSONObject (org.json.JSONObject)5 Logging (ws.nmathe.saber.utils.Logging)5 ArrayList (java.util.ArrayList)4 Collections (java.util.Collections)4 Date (java.util.Date)4 List (java.util.List)4 Collectors (java.util.stream.Collectors)4 Member (net.dv8tion.jda.core.entities.Member)4 Bson (org.bson.conversions.Bson)4 EmbedBuilder (net.dv8tion.jda.core.EmbedBuilder)3 User (net.dv8tion.jda.core.entities.User)3 PermissionException (net.dv8tion.jda.core.exceptions.PermissionException)3