Search in sources :

Example 6 with DefaultNumber

use of com.sx4.bot.annotations.argument.DefaultNumber in project Sx4 by sx4-discord-bot.

the class BotListCommand method onCommand.

public void onCommand(Sx4CommandEvent event, @Argument(value = "page") @DefaultNumber(1) @Limit(min = 1, max = 50) int page) {
    Request request = new Request.Builder().url("https://top.gg/api/bots?sort=server_count&limit=500&fields=username,server_count,id").addHeader("Authorization", event.getConfig().getTopGG()).build();
    event.getHttpClient().newCall(request).enqueue((HttpCallback) response -> {
        Document data = Document.parse(response.body().string());
        List<Document> results = data.getList("results", Document.class);
        PagedResult<Document> paged = new PagedResult<>(event.getBot(), results).setPage(page).setAuthor("Bot List", null, "https://imgur.com/HlfRQ3g.png").setIncreasedIndex(true).setSelect().setDisplayFunction(bot -> String.format("[%s](https://top.gg/bot/%s) - **%,d** servers", bot.getString("username"), bot.getString("id"), bot.getInteger("server_count")));
        paged.execute(event);
    });
}
Also used : Document(org.bson.Document) ModuleCategory(com.sx4.bot.category.ModuleCategory) Request(okhttp3.Request) List(java.util.List) DefaultNumber(com.sx4.bot.annotations.argument.DefaultNumber) HttpCallback(com.sx4.bot.http.HttpCallback) Sx4Command(com.sx4.bot.core.Sx4Command) Sx4CommandEvent(com.sx4.bot.core.Sx4CommandEvent) Permission(net.dv8tion.jda.api.Permission) PagedResult(com.sx4.bot.paged.PagedResult) Argument(com.jockie.bot.core.argument.Argument) Limit(com.sx4.bot.annotations.argument.Limit) Request(okhttp3.Request) List(java.util.List) Document(org.bson.Document) PagedResult(com.sx4.bot.paged.PagedResult)

Example 7 with DefaultNumber

use of com.sx4.bot.annotations.argument.DefaultNumber in project Sx4 by sx4-discord-bot.

the class ConvertCommand method onCommand.

public void onCommand(Sx4CommandEvent event, @Argument(value = "amount") @DefaultNumber(1) double amount, @Argument(value = "currency from") @Uppercase String from, @Argument(value = "currency to") @Uppercase String to) {
    if (to.equals(from)) {
        event.replyFormat("**%,.2f** %s \\➡ **%,.2f** %s", amount, from, amount, to).queue();
        return;
    }
    Request request = new Request.Builder().url(String.format("https://free.currconv.com/api/v7/convert?q=%s_%s&apiKey=%s&compact=y", from, to, event.getConfig().getCurrencyConvertor())).build();
    event.getHttpClient().newCall(request).enqueue((HttpCallback) response -> {
        if (!response.isSuccessful()) {
            event.replyFailure("Failed to convert, try again if this repeats it's likely due to the API being down").queue();
            return;
        }
        Document json = Document.parse(response.body().string());
        Document result = json.get(from + "_" + to, Document.class);
        if (result == null) {
            event.replyFailure("I could not find one or both of those currencies").queue();
            return;
        }
        event.replyFormat("**%,.2f** %s \\➡ **%,.2f** %s", amount, from, amount * result.getDouble("val"), to).queue();
    });
}
Also used : Document(org.bson.Document) ModuleCategory(com.sx4.bot.category.ModuleCategory) Request(okhttp3.Request) DefaultNumber(com.sx4.bot.annotations.argument.DefaultNumber) Uppercase(com.sx4.bot.annotations.argument.Uppercase) HttpCallback(com.sx4.bot.http.HttpCallback) Sx4Command(com.sx4.bot.core.Sx4Command) Sx4CommandEvent(com.sx4.bot.core.Sx4CommandEvent) Argument(com.jockie.bot.core.argument.Argument) Request(okhttp3.Request) Document(org.bson.Document)

Example 8 with DefaultNumber

use of com.sx4.bot.annotations.argument.DefaultNumber 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 9 with DefaultNumber

use of com.sx4.bot.annotations.argument.DefaultNumber in project Sx4 by sx4-discord-bot.

the class CropCommand method onCommand.

public void onCommand(Sx4CommandEvent event, @Argument(value = "image url") @ImageUrl String imageUrl, @Argument(value = "width") @Limit(min = 0) double width, @Argument(value = "height") @Limit(min = 0) @DefaultNumber(1) double height) {
    Request request = new ImageRequest(event.getConfig().getImageWebserverUrl("crop")).addQuery("w", width).addQuery("h", height).addQuery("image", imageUrl).build(event.getConfig().getImageWebserver());
    event.getHttpClient().newCall(request).enqueue((HttpCallback) response -> {
        ImageUtility.getImageMessage(event, response, (body, error) -> {
            if (error == ImageError.INVALID_QUERY_VALUE) {
                return event.replyFailure(body.getString("message"));
            }
            return null;
        }).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) ImageError(com.sx4.bot.entities.image.ImageError) Permission(net.dv8tion.jda.api.Permission) ModuleCategory(com.sx4.bot.category.ModuleCategory) DefaultNumber(com.sx4.bot.annotations.argument.DefaultNumber) Sx4CommandEvent(com.sx4.bot.core.Sx4CommandEvent) ImageUrl(com.sx4.bot.annotations.argument.ImageUrl) Argument(com.jockie.bot.core.argument.Argument) Limit(com.sx4.bot.annotations.argument.Limit) ImageRequest(com.sx4.bot.entities.image.ImageRequest) Request(okhttp3.Request) ImageRequest(com.sx4.bot.entities.image.ImageRequest)

Example 10 with DefaultNumber

use of com.sx4.bot.annotations.argument.DefaultNumber in project Sx4 by sx4-discord-bot.

the class ResizeCommand method onCommand.

public void onCommand(Sx4CommandEvent event, @Argument(value = "image url") @ImageUrl String imageUrl, @Argument(value = "width") @Limit(min = 0, max = 5000) double width, @Argument(value = "height") @Limit(min = 0, max = 5000) @DefaultNumber(1) double height) {
    Request request = new ImageRequest(event.getConfig().getImageWebserverUrl("resize")).addQuery("w", width).addQuery("h", height).addQuery("image", imageUrl).build(event.getConfig().getImageWebserver());
    event.getHttpClient().newCall(request).enqueue((HttpCallback) response -> {
        ImageUtility.getImageMessage(event, response, (body, error) -> {
            if (error == ImageError.INVALID_QUERY_VALUE) {
                return event.replyFailure(body.getString("message"));
            }
            return null;
        }).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) ImageError(com.sx4.bot.entities.image.ImageError) Permission(net.dv8tion.jda.api.Permission) ModuleCategory(com.sx4.bot.category.ModuleCategory) DefaultNumber(com.sx4.bot.annotations.argument.DefaultNumber) Sx4CommandEvent(com.sx4.bot.core.Sx4CommandEvent) ImageUrl(com.sx4.bot.annotations.argument.ImageUrl) Argument(com.jockie.bot.core.argument.Argument) Limit(com.sx4.bot.annotations.argument.Limit) ImageRequest(com.sx4.bot.entities.image.ImageRequest) Request(okhttp3.Request) ImageRequest(com.sx4.bot.entities.image.ImageRequest)

Aggregations

Argument (com.jockie.bot.core.argument.Argument)12 ModuleCategory (com.sx4.bot.category.ModuleCategory)12 Sx4Command (com.sx4.bot.core.Sx4Command)12 Sx4CommandEvent (com.sx4.bot.core.Sx4CommandEvent)12 DefaultNumber (com.sx4.bot.annotations.argument.DefaultNumber)11 Document (org.bson.Document)10 Limit (com.sx4.bot.annotations.argument.Limit)9 Permission (net.dv8tion.jda.api.Permission)9 List (java.util.List)8 PagedResult (com.sx4.bot.paged.PagedResult)7 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)7 Waiter (com.sx4.bot.waiter.Waiter)6 CancelException (com.sx4.bot.waiter.exception.CancelException)6 TimeoutException (com.sx4.bot.waiter.exception.TimeoutException)6 CompletableFuture (java.util.concurrent.CompletableFuture)6 CompletionException (java.util.concurrent.CompletionException)6 User (net.dv8tion.jda.api.entities.User)6 GenericEvent (net.dv8tion.jda.api.events.GenericEvent)6 ButtonClickEvent (net.dv8tion.jda.api.events.interaction.ButtonClickEvent)6 Button (net.dv8tion.jda.api.interactions.components.Button)6