Search in sources :

Example 1 with Colour

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();
    });
}
Also used : 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) Colour(com.sx4.bot.annotations.argument.Colour) ModuleCategory(com.sx4.bot.category.ModuleCategory) ColourUtility(com.sx4.bot.utility.ColourUtility) DefaultNumber(com.sx4.bot.annotations.argument.DefaultNumber) Sx4CommandEvent(com.sx4.bot.core.Sx4CommandEvent) MessageAction(net.dv8tion.jda.api.requests.restaction.MessageAction) MessageEmbed(net.dv8tion.jda.api.entities.MessageEmbed) Argument(com.jockie.bot.core.argument.Argument) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) MessageEmbed(net.dv8tion.jda.api.entities.MessageEmbed) ImageRequest(com.sx4.bot.entities.image.ImageRequest) MessageAction(net.dv8tion.jda.api.requests.restaction.MessageAction) Request(okhttp3.Request) ImageRequest(com.sx4.bot.entities.image.ImageRequest)

Example 2 with Colour

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);
    });
}
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) NumberUtility(com.sx4.bot.utility.NumberUtility) Permission(net.dv8tion.jda.api.Permission) PagedResult(com.sx4.bot.paged.PagedResult) ModuleCategory(com.sx4.bot.category.ModuleCategory) ColourUtility(com.sx4.bot.utility.ColourUtility) List(java.util.List) Sx4CommandEvent(com.sx4.bot.core.Sx4CommandEvent) 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) List(java.util.List) Document(org.bson.Document) PagedResult(com.sx4.bot.paged.PagedResult)

Example 3 with Colour

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());
    });
}
Also used : Document(org.bson.Document) java.util(java.util) Command(com.jockie.bot.core.command.Command) Permission(net.dv8tion.jda.api.Permission) MongoDatabase(com.sx4.bot.database.mongo.MongoDatabase) Cooldown(com.jockie.bot.core.command.Command.Cooldown) CommandId(com.sx4.bot.annotations.command.CommandId) Member(net.dv8tion.jda.api.entities.Member) Async(com.jockie.bot.core.command.Command.Async) User(net.dv8tion.jda.api.entities.User) Bson(org.bson.conversions.Bson) Alternative(com.sx4.bot.entities.argument.Alternative) Sx4CommandEvent(com.sx4.bot.core.Sx4CommandEvent) com.sx4.bot.utility(com.sx4.bot.utility) com.mongodb.client.model(com.mongodb.client.model) ZoneOffset(java.time.ZoneOffset) GameState(com.sx4.bot.entities.games.GameState) Argument(com.jockie.bot.core.argument.Argument) Operators(com.sx4.bot.database.mongo.model.Operators) Request(okhttp3.Request) ImageRequest(com.sx4.bot.entities.image.ImageRequest) HttpCallback(com.sx4.bot.http.HttpCallback) TextStyle(java.time.format.TextStyle) Sx4Command(com.sx4.bot.core.Sx4Command) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) com.sx4.bot.annotations.argument(com.sx4.bot.annotations.argument) ModuleCategory(com.sx4.bot.category.ModuleCategory) Examples(com.sx4.bot.annotations.command.Examples) LocalDate(java.time.LocalDate) Clock(java.time.Clock) ICooldown(com.jockie.bot.core.cooldown.ICooldown) User(net.dv8tion.jda.api.entities.User) ImageRequest(com.sx4.bot.entities.image.ImageRequest) Document(org.bson.Document) LocalDate(java.time.LocalDate) Bson(org.bson.conversions.Bson)

Example 4 with Colour

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();
    });
}
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) Member(net.dv8tion.jda.api.entities.Member) User(net.dv8tion.jda.api.entities.User) ModuleCategory(com.sx4.bot.category.ModuleCategory) Sx4CommandEvent(com.sx4.bot.core.Sx4CommandEvent) Option(com.jockie.bot.core.option.Option) Argument(com.jockie.bot.core.argument.Argument) 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) Member(net.dv8tion.jda.api.entities.Member)

Example 5 with Colour

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());
}
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) Colour(com.sx4.bot.annotations.argument.Colour) Argument(com.jockie.bot.core.argument.Argument) ImageRequest(com.sx4.bot.entities.image.ImageRequest) Request(okhttp3.Request) ImageRequest(com.sx4.bot.entities.image.ImageRequest)

Aggregations

Sx4Command (com.sx4.bot.core.Sx4Command)10 ModuleCategory (com.sx4.bot.category.ModuleCategory)9 Sx4CommandEvent (com.sx4.bot.core.Sx4CommandEvent)9 ImageRequest (com.sx4.bot.entities.image.ImageRequest)9 HttpCallback (com.sx4.bot.http.HttpCallback)9 Permission (net.dv8tion.jda.api.Permission)9 Request (okhttp3.Request)9 Document (org.bson.Document)9 Argument (com.jockie.bot.core.argument.Argument)8 ImageUtility (com.sx4.bot.utility.ImageUtility)8 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)5 ColourUtility (com.sx4.bot.utility.ColourUtility)4 ImageUrl (com.sx4.bot.annotations.argument.ImageUrl)3 Member (net.dv8tion.jda.api.entities.Member)3 User (net.dv8tion.jda.api.entities.User)3 Command (com.jockie.bot.core.command.Command)2 Option (com.jockie.bot.core.option.Option)2 Colour (com.sx4.bot.annotations.argument.Colour)2 MongoDatabase (com.sx4.bot.database.mongo.MongoDatabase)2 Operators (com.sx4.bot.database.mongo.model.Operators)2