Search in sources :

Example 31 with ImageRequest

use of com.sx4.bot.entities.image.ImageRequest in project Sx4 by sx4-discord-bot.

the class InvertCommand method onCommand.

public void onCommand(Sx4CommandEvent event, @Argument(value = "image url", endless = true, acceptEmpty = true) @ImageUrl String imageUrl) {
    Request request = new ImageRequest(event.getConfig().getImageWebserverUrl("invert")).addQuery("image", imageUrl).build(event.getConfig().getImageWebserver());
    event.getHttpClient().newCall(request).enqueue((HttpCallback) response -> ImageUtility.getImageMessage(event, response).queue());
}
Also used : ModuleCategory(com.sx4.bot.category.ModuleCategory) Request(okhttp3.Request) ImageRequest(com.sx4.bot.entities.image.ImageRequest) HttpCallback(com.sx4.bot.http.HttpCallback) ImageUtility(com.sx4.bot.utility.ImageUtility) Sx4Command(com.sx4.bot.core.Sx4Command) Sx4CommandEvent(com.sx4.bot.core.Sx4CommandEvent) Permission(net.dv8tion.jda.api.Permission) ImageUrl(com.sx4.bot.annotations.argument.ImageUrl) Argument(com.jockie.bot.core.argument.Argument) ImageRequest(com.sx4.bot.entities.image.ImageRequest) Request(okhttp3.Request) ImageRequest(com.sx4.bot.entities.image.ImageRequest)

Example 32 with ImageRequest

use of com.sx4.bot.entities.image.ImageRequest in project Sx4 by sx4-discord-bot.

the class MostCommonColourCommand method onCommand.

public void onCommand(Sx4CommandEvent event, @Argument(value = "image url", acceptEmpty = true, endless = true) @ImageUrl String imageUrl) {
    Request request = new ImageRequest(event.getConfig().getImageWebserverUrl("common-colour")).addQuery("image", imageUrl).build(event.getConfig().getImageWebserver());
    event.getHttpClient().newCall(request).enqueue((HttpCallback) response -> {
        if (!response.isSuccessful()) {
            ImageUtility.getErrorMessage(event.getTextChannel(), response.code(), response.body().string()).queue();
            return;
        }
        Document data = Document.parse(response.body().string());
        Document common = data.getList("colours", Document.class).get(0);
        int colour = common.getInteger("colour");
        EmbedBuilder embed = new EmbedBuilder().setTitle("Most Common Colour").setThumbnail(imageUrl).setColor(ImageUtility.getEmbedColour(colour)).addField("Colour", String.format("Hex: #%s\nRGB: %s", ColourUtility.toHexString(colour), ColourUtility.toRGBString(colour)), true).addField("Pixels", String.format("Amount: %,d", common.getInteger("pixels")), true);
        event.reply(embed.build()).queue();
    });
}
Also used : Document(org.bson.Document) Request(okhttp3.Request) ImageRequest(com.sx4.bot.entities.image.ImageRequest) HttpCallback(com.sx4.bot.http.HttpCallback) ImageUtility(com.sx4.bot.utility.ImageUtility) Sx4Command(com.sx4.bot.core.Sx4Command) Permission(net.dv8tion.jda.api.Permission) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) ModuleCategory(com.sx4.bot.category.ModuleCategory) ColourUtility(com.sx4.bot.utility.ColourUtility) Sx4CommandEvent(com.sx4.bot.core.Sx4CommandEvent) ImageUrl(com.sx4.bot.annotations.argument.ImageUrl) Argument(com.jockie.bot.core.argument.Argument) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) ImageRequest(com.sx4.bot.entities.image.ImageRequest) Request(okhttp3.Request) ImageRequest(com.sx4.bot.entities.image.ImageRequest) Document(org.bson.Document)

Example 33 with ImageRequest

use of com.sx4.bot.entities.image.ImageRequest in project Sx4 by sx4-discord-bot.

the class UserBannerCommand method onCommand.

public void onCommand(Sx4CommandEvent event, @Argument(value = "user", endless = true, nullDefault = true) Member member) {
    User user = member == null ? event.getAuthor() : member.getUser();
    user.retrieveProfile().queue(profile -> {
        String banner = profile.getBannerUrl();
        if (banner == null) {
            int accent = profile.getAccentColorRaw();
            if (accent == User.DEFAULT_ACCENT_COLOR_RAW) {
                event.replyFailure("That user does not have a banner").queue();
                return;
            }
            String accentBanner = event.getConfig().getImageWebserverUrl("colour") + "?colour=" + accent + "&w=1024&h=205";
            EmbedBuilder embed = new EmbedBuilder().setImage(accentBanner).setColor(accent).setAuthor(user.getAsTag(), accentBanner, user.getEffectiveAvatarUrl());
            event.reply(embed.build()).queue();
            return;
        }
        Request request = new ImageRequest(event.getConfig().getImageWebserverUrl("median-colour")).addQuery("image", banner).build(event.getConfig().getImageWebserver());
        event.getHttpClient().newCall(request).enqueue((HttpCallback) response -> {
            if (!response.isSuccessful()) {
                ImageUtility.getErrorMessage(event.getTextChannel(), response.code(), response.body().string()).queue();
                return;
            }
            Document data = Document.parse(response.body().string());
            String sizedAvatar = banner + "?size=1024";
            EmbedBuilder embed = new EmbedBuilder().setImage(sizedAvatar).setColor(data.getInteger("colour")).setAuthor(user.getAsTag(), sizedAvatar, user.getEffectiveAvatarUrl());
            event.reply(embed.build()).queue();
        });
    });
}
Also used : Document(org.bson.Document) ModuleCategory(com.sx4.bot.category.ModuleCategory) Request(okhttp3.Request) ImageRequest(com.sx4.bot.entities.image.ImageRequest) HttpCallback(com.sx4.bot.http.HttpCallback) ImageUtility(com.sx4.bot.utility.ImageUtility) Sx4Command(com.sx4.bot.core.Sx4Command) Sx4CommandEvent(com.sx4.bot.core.Sx4CommandEvent) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) Member(net.dv8tion.jda.api.entities.Member) Argument(com.jockie.bot.core.argument.Argument) User(net.dv8tion.jda.api.entities.User) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) User(net.dv8tion.jda.api.entities.User) ImageRequest(com.sx4.bot.entities.image.ImageRequest) Request(okhttp3.Request) ImageRequest(com.sx4.bot.entities.image.ImageRequest) Document(org.bson.Document)

Example 34 with ImageRequest

use of com.sx4.bot.entities.image.ImageRequest in project Sx4 by sx4-discord-bot.

the class ServerAvatarCommand method onCommand.

public void onCommand(Sx4CommandEvent event) {
    String icon = event.getGuild().getIconUrl();
    if (icon == null) {
        event.replyFailure("This server does not have an icon").queue();
        return;
    }
    Request request = new ImageRequest(event.getConfig().getImageWebserverUrl("median-colour")).addQuery("image", icon).build(event.getConfig().getImageWebserver());
    event.getHttpClient().newCall(request).enqueue((HttpCallback) response -> {
        if (!response.isSuccessful()) {
            ImageUtility.getErrorMessage(event.getTextChannel(), response.code(), response.body().string()).queue();
            return;
        }
        Document data = Document.parse(response.body().string());
        String sizedIcon = icon + "?size=1024";
        EmbedBuilder embed = new EmbedBuilder().setImage(sizedIcon).setColor(data.getInteger("colour")).setAuthor(event.getGuild().getName(), sizedIcon, sizedIcon);
        event.reply(embed.build()).queue();
    });
}
Also used : Document(org.bson.Document) ModuleCategory(com.sx4.bot.category.ModuleCategory) Request(okhttp3.Request) ImageRequest(com.sx4.bot.entities.image.ImageRequest) HttpCallback(com.sx4.bot.http.HttpCallback) ImageUtility(com.sx4.bot.utility.ImageUtility) Sx4Command(com.sx4.bot.core.Sx4Command) Sx4CommandEvent(com.sx4.bot.core.Sx4CommandEvent) Permission(net.dv8tion.jda.api.Permission) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) ImageRequest(com.sx4.bot.entities.image.ImageRequest) Request(okhttp3.Request) ImageRequest(com.sx4.bot.entities.image.ImageRequest) Document(org.bson.Document)

Example 35 with ImageRequest

use of com.sx4.bot.entities.image.ImageRequest in project Sx4 by sx4-discord-bot.

the class WelcomerUtility method getWelcomerMessage.

public static void getWelcomerMessage(OkHttpClient httpClient, Document messageData, String bannerId, Member member, boolean canary, boolean image, boolean gif, BiConsumer<WebhookMessageBuilder, Throwable> consumer) {
    Guild guild = member.getGuild();
    User user = member.getUser();
    OffsetDateTime now = OffsetDateTime.now();
    Formatter<Document> formatter = new JsonFormatter(messageData).member(member).user(user).guild(guild).addVariable(User.class, "age", TimeUtility.LONG_TIME_FORMATTER.parse(Duration.between(user.getTimeCreated(), now).toSeconds())).addVariable("now", now);
    if (!image) {
        WebhookMessageBuilder builder;
        if (messageData != null) {
            try {
                builder = MessageUtility.fromJson(formatter.parse());
            } catch (IllegalArgumentException e) {
                consumer.accept(null, e);
                return;
            }
        } else {
            builder = new WebhookMessageBuilder();
        }
        consumer.accept(builder, null);
    } else {
        ImageRequest request = new ImageRequest(Config.get().getImageWebserverUrl("welcomer")).addQuery("avatar", user.getEffectiveAvatarUrl()).addQuery("name", user.getAsTag()).addQuery("gif", gif).addQuery("directory", canary ? "sx4-canary" : "sx4-main");
        if (bannerId != null) {
            request.addQuery("banner_id", bannerId);
        }
        httpClient.newCall(request.build(Config.get().getImageWebserver())).enqueue((HttpCallback) response -> {
            if (response.isSuccessful()) {
                String fileName = "welcomer." + response.header("Content-Type").split("/")[1];
                formatter.addVariable("file.name", fileName).addVariable("file.url", "attachment://" + fileName);
                WebhookMessageBuilder builder;
                if (messageData == null) {
                    builder = new WebhookMessageBuilder();
                } else {
                    try {
                        builder = MessageUtility.fromJson(formatter.parse());
                    } catch (IllegalArgumentException e) {
                        consumer.accept(null, e);
                        return;
                    }
                }
                builder.addFile(fileName, response.body().bytes());
                consumer.accept(builder, null);
            } else {
                response.close();
            }
        });
    }
}
Also used : JsonFormatter(com.sx4.bot.formatter.JsonFormatter) WebhookMessageBuilder(club.minnced.discord.webhook.send.WebhookMessageBuilder) Document(org.bson.Document) Config(com.sx4.bot.config.Config) ImageRequest(com.sx4.bot.entities.image.ImageRequest) HttpCallback(com.sx4.bot.http.HttpCallback) JsonFormatter(com.sx4.bot.formatter.JsonFormatter) Member(net.dv8tion.jda.api.entities.Member) User(net.dv8tion.jda.api.entities.User) Guild(net.dv8tion.jda.api.entities.Guild) OkHttpClient(okhttp3.OkHttpClient) OffsetDateTime(java.time.OffsetDateTime) Formatter(com.sx4.bot.formatter.Formatter) Duration(java.time.Duration) BiConsumer(java.util.function.BiConsumer) User(net.dv8tion.jda.api.entities.User) OffsetDateTime(java.time.OffsetDateTime) WebhookMessageBuilder(club.minnced.discord.webhook.send.WebhookMessageBuilder) ImageRequest(com.sx4.bot.entities.image.ImageRequest) Guild(net.dv8tion.jda.api.entities.Guild) Document(org.bson.Document)

Aggregations

ImageRequest (com.sx4.bot.entities.image.ImageRequest)35 HttpCallback (com.sx4.bot.http.HttpCallback)35 ModuleCategory (com.sx4.bot.category.ModuleCategory)34 Sx4Command (com.sx4.bot.core.Sx4Command)34 Sx4CommandEvent (com.sx4.bot.core.Sx4CommandEvent)34 Argument (com.jockie.bot.core.argument.Argument)33 ImageUtility (com.sx4.bot.utility.ImageUtility)33 Request (okhttp3.Request)33 Permission (net.dv8tion.jda.api.Permission)31 ImageUrl (com.sx4.bot.annotations.argument.ImageUrl)21 Limit (com.sx4.bot.annotations.argument.Limit)8 Member (net.dv8tion.jda.api.entities.Member)8 Document (org.bson.Document)8 User (net.dv8tion.jda.api.entities.User)6 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)5 DefaultNumber (com.sx4.bot.annotations.argument.DefaultNumber)3 ImageError (com.sx4.bot.entities.image.ImageError)3 ColourUtility (com.sx4.bot.utility.ColourUtility)3 Option (com.jockie.bot.core.option.Option)2 Colour (com.sx4.bot.annotations.argument.Colour)2