Search in sources :

Example 1 with TriConsumer

use of com.jockie.bot.core.utility.function.TriConsumer in project Sx4 by sx4-discord-bot.

the class EmoteCommand method getBytes.

private void getBytes(OkHttpClient httpClient, String url, TriConsumer<byte[], Boolean, Integer> bytes) {
    Request request = new Request.Builder().url(RequestUtility.getWorkerUrl(url)).build();
    httpClient.newCall(request).enqueue((HttpCallback) response -> {
        if (response.code() == 200) {
            String contentType = response.header("Content-Type"), extension = null;
            if (contentType != null && contentType.contains("/")) {
                extension = contentType.split("/")[1].toLowerCase();
            }
            bytes.accept(response.body().bytes(), extension == null ? null : extension.equals("gif"), 200);
            return;
        } else if (response.code() == 415) {
            int periodIndex = url.lastIndexOf('.');
            if (periodIndex != -1) {
                String extension = url.substring(periodIndex + 1);
                if (extension.equalsIgnoreCase("gif")) {
                    this.getBytes(httpClient, url.substring(0, periodIndex + 1) + "png", bytes);
                    return;
                }
            }
        }
        bytes.accept(null, null, response.code());
    });
}
Also used : Arrays(java.util.Arrays) Command(com.jockie.bot.core.command.Command) Permission(net.dv8tion.jda.api.Permission) Cooldown(com.jockie.bot.core.command.Command.Cooldown) PagedResult(com.sx4.bot.paged.PagedResult) HashSet(java.util.HashSet) Role(net.dv8tion.jda.api.entities.Role) Sx4CommandEvent(com.sx4.bot.core.Sx4CommandEvent) RequestUtility(com.sx4.bot.utility.RequestUtility) TimeUtility(com.sx4.bot.utility.TimeUtility) Icon(net.dv8tion.jda.api.entities.Icon) RateLimitedException(net.dv8tion.jda.api.exceptions.RateLimitedException) TriConsumer(com.jockie.bot.core.utility.function.TriConsumer) com.sx4.bot.annotations.command(com.sx4.bot.annotations.command) Argument(com.jockie.bot.core.argument.Argument) ErrorResponseException(net.dv8tion.jda.api.exceptions.ErrorResponseException) Request(okhttp3.Request) HttpCallback(com.sx4.bot.http.HttpCallback) Sx4Command(com.sx4.bot.core.Sx4Command) PartialEmote(com.sx4.bot.entities.mod.PartialEmote) Predicate(java.util.function.Predicate) Set(java.util.Set) CompletionException(java.util.concurrent.CompletionException) ModuleCategory(com.sx4.bot.category.ModuleCategory) List(java.util.List) OkHttpClient(okhttp3.OkHttpClient) ExceptionUtility(com.sx4.bot.utility.ExceptionUtility) Emote(net.dv8tion.jda.api.entities.Emote) Request(okhttp3.Request)

Aggregations

Argument (com.jockie.bot.core.argument.Argument)1 Command (com.jockie.bot.core.command.Command)1 Cooldown (com.jockie.bot.core.command.Command.Cooldown)1 TriConsumer (com.jockie.bot.core.utility.function.TriConsumer)1 com.sx4.bot.annotations.command (com.sx4.bot.annotations.command)1 ModuleCategory (com.sx4.bot.category.ModuleCategory)1 Sx4Command (com.sx4.bot.core.Sx4Command)1 Sx4CommandEvent (com.sx4.bot.core.Sx4CommandEvent)1 PartialEmote (com.sx4.bot.entities.mod.PartialEmote)1 HttpCallback (com.sx4.bot.http.HttpCallback)1 PagedResult (com.sx4.bot.paged.PagedResult)1 ExceptionUtility (com.sx4.bot.utility.ExceptionUtility)1 RequestUtility (com.sx4.bot.utility.RequestUtility)1 TimeUtility (com.sx4.bot.utility.TimeUtility)1 Arrays (java.util.Arrays)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Set (java.util.Set)1 CompletionException (java.util.concurrent.CompletionException)1 Predicate (java.util.function.Predicate)1