Search in sources :

Example 66 with DataArray

use of net.dv8tion.jda.api.utils.data.DataArray in project Bean by Xirado.

the class CommandsRoute method handle.

@Override
public Object handle(Request request, Response response) throws Exception {
    List<SlashCommand> commands = Bean.getInstance().isDebug() ? Bean.getInstance().getInteractionCommandHandler().getRegisteredGuildCommands().get(DEV_GUILD_ID).stream().filter(cmd -> cmd instanceof SlashCommand).map(cmd -> (SlashCommand) cmd).toList() : Bean.getInstance().getInteractionCommandHandler().getRegisteredSlashCommands();
    DataArray commandArray = DataArray.empty();
    for (SlashCommand command : commands) {
        DataObject commandObject = DataObject.empty();
        SlashCommandData slashCommandData = command.getData();
        DataArray options = DataArray.empty();
        DataArray subCommands = DataArray.empty();
        for (OptionData option : slashCommandData.getOptions()) {
            DataObject optionObject = DataObject.empty().put("name", option.getName()).put("type", option.getType().toString()).put("description", option.getDescription()).put("required", option.isRequired());
            options.add(optionObject);
        }
        for (SubcommandData subcommandData : slashCommandData.getSubcommands()) {
            DataObject subCommandObject = DataObject.empty();
            DataArray subCommandOptions = DataArray.empty();
            for (OptionData option : subcommandData.getOptions()) {
                DataObject optionObject = DataObject.empty().put("name", option.getName()).put("type", option.getType().toString()).put("description", option.getDescription()).put("required", option.isRequired());
                subCommandOptions.add(optionObject);
            }
            subCommandObject.put("name", subcommandData.getName()).put("description", subcommandData.getDescription());
            subCommandObject.put("options", subCommandOptions);
            subCommands.add(subCommandObject);
        }
        commandObject.put("name", slashCommandData.getName()).put("description", slashCommandData.getDescription()).put("options", options).put("sub_commands", subCommands);
        commandArray.add(commandObject);
    }
    return commandArray.toString();
}
Also used : SlashCommand(at.xirado.bean.command.SlashCommand) DataArray(net.dv8tion.jda.api.utils.data.DataArray) List(java.util.List) SubcommandData(net.dv8tion.jda.api.interactions.commands.build.SubcommandData) Request(spark.Request) OptionData(net.dv8tion.jda.api.interactions.commands.build.OptionData) Response(spark.Response) SlashCommand(at.xirado.bean.command.SlashCommand) DataObject(net.dv8tion.jda.api.utils.data.DataObject) Route(spark.Route) Bean(at.xirado.bean.Bean) SlashCommandData(net.dv8tion.jda.api.interactions.commands.build.SlashCommandData) DataObject(net.dv8tion.jda.api.utils.data.DataObject) OptionData(net.dv8tion.jda.api.interactions.commands.build.OptionData) SlashCommandData(net.dv8tion.jda.api.interactions.commands.build.SlashCommandData) DataArray(net.dv8tion.jda.api.utils.data.DataArray) SubcommandData(net.dv8tion.jda.api.interactions.commands.build.SubcommandData)

Example 67 with DataArray

use of net.dv8tion.jda.api.utils.data.DataArray in project Bean by Xirado.

the class MEE6Queue method makeCall.

private void makeCall(MEE6Request request) {
    currentRequestGuildId = request.getGuildId();
    try {
        URI uri = new URIBuilder().setScheme("https").setHost("mee6.xyz").setPath("/api/plugins/levels/leaderboard/" + request.getGuildId()).addParameter("page", String.valueOf(request.getPage())).build();
        Request httpRequest = new Request.Builder().url(uri.toURL()).get().build();
        Call call = client.newCall(httpRequest);
        Response response = call.execute();
        if (response.code() == 429) {
            String retryAfter = response.header("retry-after");
            if (retryAfter != null)
                LOGGER.warn("Encountered MEE6 Cloudflare Rate-limit! Retrying after {} seconds", retryAfter);
            else
                LOGGER.warn("Encountered MEE6 Cloudflare Rate-limit without \"retry-after\" header! Retrying after 1 hour");
            isRateLimit = true;
            int retryAfterMillis = retryAfter == null ? 60 * 60 * 1000 : Integer.parseInt(retryAfter) * 1000;
            this.rateLimitExpiry = System.currentTimeMillis() + retryAfterMillis;
            queue.offer(request);
            response.close();
            return;
        }
        if (response.code() == 404) {
            Guild guild = Bean.getInstance().getShardManager().getGuildById(request.getGuildId());
            if (guild != null)
                Util.sendDM(request.getAuthorId(), EmbedUtil.defaultEmbed("Hey! We tried to migrate MEE6 experience for your guild **" + guild.getName() + "**, but sadly we could not find anything!\n\nAdding MEE6 to your server again often fixes this issue!"));
            response.close();
            currentRequestGuildId = 0L;
            return;
        }
        if (response.code() == 401) {
            Guild guild = Bean.getInstance().getShardManager().getGuildById(request.getGuildId());
            if (guild != null)
                Util.sendDM(request.getAuthorId(), EmbedUtil.defaultEmbed("Hey! We tried to migrate MEE6 experience for your guild **" + guild.getName() + "**, but your servers MEE6 leaderboard is [set to private](https://mee6.xyz/dashboard/" + guild.getIdLong() + "/leaderboard)!\n\nPlease set it to public and try again!"));
            response.close();
            currentRequestGuildId = 0L;
            return;
        }
        if (String.valueOf(response.code()).charAt(0) == '5') {
            Guild guild = Bean.getInstance().getShardManager().getGuildById(request.getGuildId());
            if (guild != null)
                Util.sendDM(request.getAuthorId(), EmbedUtil.defaultEmbed("Hey! We tried to migrate MEE6 experience for your guild **" + guild.getName() + "**, but the MEE6 server appears to be having issues!\n\nPlease try again later."));
            response.close();
            currentRequestGuildId = 0L;
            return;
        }
        if (!response.isSuccessful()) {
            LOGGER.error("MEE6 returned unhandled error {}\n{}", response.code(), response.body().string());
            response.close();
            currentRequestGuildId = 0L;
            return;
        }
        DataObject object = DataObject.fromJson(response.body().string());
        response.close();
        DataArray playersArray = object.getArray("players");
        int entries = playersArray.length();
        try (Connection connection = Database.getConnectionFromPool()) {
            playersArray.stream(DataArray::getObject).map(MEE6Player::fromData).forEach(player -> updateXP(connection, player, request.getGuildId()));
        } catch (SQLException e) {
            LOGGER.error("SQLException occurred!", e);
        }
        if (entries == 100) {
            addRequest(new MEE6Request(request.getGuildId(), request.getAuthorId()).setPage(request.getPage() + 1));
            currentRequestGuildId = 0L;
            return;
        }
        currentRequestGuildId = 0L;
        int entriesTotal = ((request.getPage()) * 100) + entries;
        LOGGER.debug("Finished transferring xp for guild {}! Migrated {}users.", request.getGuildId(), entriesTotal);
        Guild guild = Bean.getInstance().getShardManager().getGuildById(request.getGuildId());
        if (guild != null && entriesTotal > 1)
            Util.sendDM(request.getAuthorId(), EmbedUtil.defaultEmbed("Hey! We'd like you to know that we have finished migrating MEE6 xp for all users on your guild **" + guild.getName() + "**! (" + entriesTotal + " Users)"));
        else if (guild != null && entriesTotal == 0)
            Util.sendDM(request.getAuthorId(), EmbedUtil.defaultEmbed("Hey! We tried to migrate MEE6 xp for all users on your guild **" + guild.getName() + "**, but we couldn't find any!"));
        currentRequestGuildId = 0L;
    } catch (URISyntaxException | IOException exception) {
        LOGGER.error("Error occurred in MEE6Queue!", exception);
    }
}
Also used : Call(okhttp3.Call) SQLException(java.sql.SQLException) Request(okhttp3.Request) Connection(java.sql.Connection) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) Guild(net.dv8tion.jda.api.entities.Guild) URI(java.net.URI) DataArray(net.dv8tion.jda.api.utils.data.DataArray) URIBuilder(org.apache.http.client.utils.URIBuilder) Response(okhttp3.Response) DataObject(net.dv8tion.jda.api.utils.data.DataObject)

Aggregations

DataArray (net.dv8tion.jda.api.utils.data.DataArray)67 DataObject (net.dv8tion.jda.api.utils.data.DataObject)40 Nonnull (javax.annotation.Nonnull)21 EntityBuilder (net.dv8tion.jda.internal.entities.EntityBuilder)14 InsufficientPermissionException (net.dv8tion.jda.api.exceptions.InsufficientPermissionException)10 JDAImpl (net.dv8tion.jda.internal.JDAImpl)9 TLongObjectHashMap (gnu.trove.map.hash.TLongObjectHashMap)8 ArrayList (java.util.ArrayList)8 Route (net.dv8tion.jda.internal.requests.Route)8 RestActionImpl (net.dv8tion.jda.internal.requests.RestActionImpl)7 Collectors (java.util.stream.Collectors)6 CheckReturnValue (javax.annotation.CheckReturnValue)6 TemplateRole (net.dv8tion.jda.api.entities.templates.TemplateRole)5 ParsingException (net.dv8tion.jda.api.exceptions.ParsingException)5 GuildImpl (net.dv8tion.jda.internal.entities.GuildImpl)5 Bean (at.xirado.bean.Bean)4 java.util (java.util)4 OnlineStatus (net.dv8tion.jda.api.OnlineStatus)4 Template (net.dv8tion.jda.api.entities.templates.Template)4 CacheView (net.dv8tion.jda.api.utils.cache.CacheView)4