use of com.sx4.bot.annotations.argument.Colour in project Sx4 by sx4-discord-bot.
the class ColourCommand method onCommand.
public void onCommand(Sx4CommandEvent event, @Argument(value = "colour", endless = true) @DefaultNumber(-1) @Colour int colour) {
if (colour == -1) {
colour = event.getRandom().nextInt(0xFFFFFF + 1);
}
String hex = "#" + ColourUtility.toHexString(colour);
MessageEmbed embed = new EmbedBuilder().setColor(ImageUtility.getEmbedColour(colour)).setAuthor(hex, null, "attachment://image.png").setDescription(String.format("Hex: %s\nRGB: %s", hex, ColourUtility.toRGBString(colour))).setImage("attachment://image.png").build();
Request request = new ImageRequest(event.getConfig().getImageWebserverUrl("colour")).addQuery("colour", colour).build(event.getConfig().getImageWebserver());
event.getHttpClient().newCall(request).enqueue((HttpCallback) response -> {
MessageAction action = ImageUtility.getImageMessage(event, response);
if (response.isSuccessful()) {
action.setEmbeds(embed);
}
action.queue();
});
}
use of com.sx4.bot.annotations.argument.Colour in project Sx4 by sx4-discord-bot.
the class ColourCoverageCommand 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("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());
List<Document> colours = data.getList("colours", Document.class);
long totalPixels = colours.stream().mapToInt(colour -> colour.getInteger("pixels")).sum();
PagedResult<Document> paged = new PagedResult<>(event.getBot(), colours).setAuthor("Colours", null, imageUrl).setIncreasedIndex(true).setSelect().setPerPage(15).setDisplayFunction(colour -> {
int pixels = colour.getInteger("pixels");
return "`#" + ColourUtility.toHexString(colour.getInteger("colour")) + "` - " + pixels + " pixel" + (pixels == 1 ? "" : "s") + " (" + NumberUtility.DEFAULT_DECIMAL_FORMAT.format((pixels / (double) totalPixels) * 100) + "%)";
});
paged.execute(event);
});
}
use of com.sx4.bot.annotations.argument.Colour in project Sx4 by sx4-discord-bot.
the class ProfileCommand method onCommand.
public void onCommand(Sx4CommandEvent event, @Argument(value = "user", endless = true, nullDefault = true) Member member) {
User user = member == null ? event.getAuthor() : member.getUser();
long expiry = event.getMongoMain().getUserById(Filters.eq("_id", user.getIdLong()), Projections.include("premium.endAt")).getEmbedded(List.of("premium", "endAt"), 0L);
List<Bson> gamePipeline = List.of(Aggregates.match(Filters.eq("userId", user.getIdLong())), Aggregates.group(null, Accumulators.sum("gamesPlayed", 1L), Accumulators.sum("gamesWon", Operators.cond(Operators.eq("$state", GameState.WIN.getId()), 1L, 0L))));
List<Bson> commandPipeline = List.of(Aggregates.match(Filters.eq("authorId", user.getIdLong())), Aggregates.count("commands"));
List<Bson> marriagePipeline = List.of(Aggregates.project(Projections.include("proposerId", "partnerId")), Aggregates.match(Filters.or(Filters.eq("proposerId", user.getIdLong()), Filters.eq("partnerId", user.getIdLong()))), Aggregates.group(null, Accumulators.push("marriages", Operators.ROOT)));
List<Bson> pipeline = List.of(Aggregates.project(Projections.fields(Projections.computed("balance", "$economy.balance"), Projections.include("profile"), Projections.computed("reputation", "$reputation.amount"), Projections.computed("premium", Operators.lt(Operators.nowEpochSecond(), Operators.ifNull("$premium.endAt", 0L))))), Aggregates.match(Filters.eq("_id", user.getIdLong())), Aggregates.unionWith("marriages", marriagePipeline), Aggregates.unionWith("commands", commandPipeline), Aggregates.unionWith("games", gamePipeline), Aggregates.group(null, Accumulators.max("balance", "$balance"), Accumulators.max("reputation", "$reputation"), Accumulators.max("marriages", "$marriages"), Accumulators.max("profile", "$profile"), Accumulators.max("gamesPlayed", "$gamesPlayed"), Accumulators.max("gamesWon", "$gamesWon"), Accumulators.max("commands", "$commands"), Accumulators.max("premium", Operators.ifNull("$premium", false))));
event.getMongo().aggregateUsers(pipeline).thenApply(documents -> {
Document data = documents.isEmpty() ? MongoDatabase.EMPTY_DOCUMENT : documents.get(0);
List<Document> marriages = data.getList("marriages", Document.class, Collections.emptyList());
List<String> partners = new ArrayList<>();
for (Document marriage : marriages) {
long partnerId = marriage.getLong("partnerId");
long otherId = partnerId == user.getIdLong() ? marriage.getLong("proposerId") : partnerId;
User other = event.getShardManager().getUserById(otherId);
if (other != null) {
partners.add(other.getName());
}
}
Document profileData = data.get("profile", MongoDatabase.EMPTY_DOCUMENT);
Document birthdayData = profileData.get("birthday", Document.class);
String birthday = null;
boolean isBirthday = false;
if (birthdayData != null) {
LocalDate date = LocalDate.now(ZoneOffset.UTC);
int day = birthdayData.getInteger("day"), month = birthdayData.getInteger("month");
isBirthday = date.getDayOfMonth() == day && date.getMonthValue() == month;
birthday = NumberUtility.getZeroPrefixedNumber(day) + "/" + NumberUtility.getZeroPrefixedNumber(month) + (birthdayData.containsKey("year") ? "/" + birthdayData.getInteger("year") : "");
}
return new ImageRequest(event.getConfig().getImageWebserverUrl("profile")).addField("birthday", birthday == null ? "Not set" : birthday).addField("is_birthday", isBirthday).addField("description", profileData.get("description", "Nothing to see here")).addField("height", profileData.get("height", 0)).addField("balance", NumberUtility.getNumberReadable(data.get("balance", 0L))).addField("reputation", data.get("reputation", 0)).addField("married_users", partners).addField("commands", data.get("commands", 0L)).addField("games_played", data.get("gamesPlayed", 0L)).addField("games_won", data.get("gamesWon", 0L)).addField("banner_id", profileData.getString("bannerId")).addField("directory", event.getConfig().isCanary() ? "sx4-canary" : "sx4-main").addField("name", user.getAsTag()).addField("gif", Clock.systemUTC().instant().getEpochSecond() < expiry).addField("avatar", user.getEffectiveAvatarUrl()).addField("colour", profileData.getInteger("colour")).build(event.getConfig().getImageWebserver());
}).whenComplete((request, exception) -> {
if (ExceptionUtility.sendExceptionally(event, exception)) {
return;
}
event.getHttpClient().newCall(request).enqueue((HttpCallback) response -> ImageUtility.getImageMessage(event, response).queue());
});
}
use of com.sx4.bot.annotations.argument.Colour in project Sx4 by sx4-discord-bot.
the class AvatarCommand method onCommand.
public void onCommand(Sx4CommandEvent event, @Argument(value = "user", endless = true, nullDefault = true) Member member, @Option(value = "global", description = "Forced to show their global avatar") boolean global) {
Member effectiveMember = member == null ? event.getMember() : member;
User user = effectiveMember.getUser();
String avatar = global ? user.getEffectiveAvatarUrl() : effectiveMember.getEffectiveAvatarUrl();
Request request = new ImageRequest(event.getConfig().getImageWebserverUrl("median-colour")).addQuery("image", avatar).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 = avatar + "?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.annotations.argument.Colour in project Sx4 by sx4-discord-bot.
the class ChangeColourCommand method onCommand.
public void onCommand(Sx4CommandEvent event, @Argument(value = "image", acceptEmpty = true) @ImageUrl String imageUrl, @Argument(value = "colour", endless = true) @Colour int colour) {
Request request = new ImageRequest(event.getConfig().getImageWebserverUrl("manipulate-colour")).addQuery("image", imageUrl).addQuery("colour", colour).build(event.getConfig().getImageWebserver());
event.getHttpClient().newCall(request).enqueue((HttpCallback) response -> ImageUtility.getImageMessage(event, response).queue());
}
Aggregations