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());
}
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();
});
}
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();
});
});
}
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();
});
}
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();
}
});
}
}
Aggregations