Search in sources :

Example 6 with HttpCallback

use of com.sx4.bot.http.HttpCallback in project Sx4 by sx4-discord-bot.

the class WeatherCommand method onCommand.

public void onCommand(Sx4CommandEvent event, @Argument(value = "query", endless = true) String query) {
    Request request = new Request.Builder().url(event.getConfig().getSearchWebserverUrl("weather") + "?q=" + URLEncoder.encode(query, StandardCharsets.UTF_8)).build();
    event.getHttpClient().newCall(request).enqueue((HttpCallback) response -> {
        if (response.code() == 404) {
            event.replyFailure("I could not find that location").queue();
            return;
        }
        Document document = Document.parse(response.body().string());
        if (!response.isSuccessful()) {
            StringBuilder builder = new StringBuilder("Command failed with status " + response.code());
            if (document.containsKey("message")) {
                builder.append(" with message `").append(document.getString("message")).append("`");
            }
            event.replyFailure(builder.toString()).queue();
            return;
        }
        Document now = document.get("now", Document.class);
        Document today = document.get("today", Document.class);
        int windDegrees = now.getInteger("wdir");
        Integer maxTemp = today.getInteger("max_temp");
        EmbedBuilder embed = new EmbedBuilder();
        embed.setAuthor(document.getString("location"), document.getString("url"), null);
        embed.setTitle(now.getString("phrase_32char"));
        embed.addField("Temperature", String.format("Maximum: %s°C\nCurrent: %d°C\nMinimum: %d°C", maxTemp == null ? "--" : maxTemp, now.getInteger("temp"), today.getInteger("min_temp")), true);
        embed.addBlankField(true);
        embed.addField("Wind", String.format("Direction: %d° (%s)\nSpeed: %d mph", windDegrees, Direction.getDirection(windDegrees).getName(), now.getInteger("wspd")), true);
        embed.addField("Humidity", now.getInteger("rh") + "%", true);
        embed.addBlankField(true);
        embed.addField("Precipitation", now.getInteger("pop") + "%", true);
        embed.setThumbnail(now.getString("icon"));
        embed.setFooter(now.getString("dow") + " " + OffsetDateTime.parse(now.getString("fcst_valid_local"), this.parseFormatter).format(this.formatter));
        event.reply(embed.build()).queue();
    });
}
Also used : Document(org.bson.Document) Request(okhttp3.Request) Arrays(java.util.Arrays) HttpCallback(com.sx4.bot.http.HttpCallback) Sx4Command(com.sx4.bot.core.Sx4Command) Permission(net.dv8tion.jda.api.Permission) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) StandardCharsets(java.nio.charset.StandardCharsets) ModuleCategory(com.sx4.bot.category.ModuleCategory) URLEncoder(java.net.URLEncoder) OffsetDateTime(java.time.OffsetDateTime) Sx4CommandEvent(com.sx4.bot.core.Sx4CommandEvent) DateTimeFormatter(java.time.format.DateTimeFormatter) Comparator(java.util.Comparator) Argument(com.jockie.bot.core.argument.Argument) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) Request(okhttp3.Request) Document(org.bson.Document)

Example 7 with HttpCallback

use of com.sx4.bot.http.HttpCallback in project Sx4 by sx4-discord-bot.

the class ShortenCommand method onCommand.

public void onCommand(Sx4CommandEvent event, @Argument(value = "url") String url) {
    Request request = new Request.Builder().url("https://api-ssl.bitly.com/v4/shorten").post(RequestBody.create(MediaType.parse("application/json; charset=utf-8"), new Document("long_url", url).toJson())).addHeader("Authorization", "Bearer " + event.getConfig().getBitly()).addHeader("Content-Type", "application/json").build();
    event.getHttpClient().newCall(request).enqueue((HttpCallback) response -> {
        Document json = Document.parse(response.body().string());
        event.replyFormat("<" + json.getString("link") + ">").queue();
    });
}
Also used : Document(org.bson.Document) ModuleCategory(com.sx4.bot.category.ModuleCategory) Request(okhttp3.Request) HttpCallback(com.sx4.bot.http.HttpCallback) Sx4Command(com.sx4.bot.core.Sx4Command) Sx4CommandEvent(com.sx4.bot.core.Sx4CommandEvent) MediaType(okhttp3.MediaType) Argument(com.jockie.bot.core.argument.Argument) RequestBody(okhttp3.RequestBody) Request(okhttp3.Request) Document(org.bson.Document)

Example 8 with HttpCallback

use of com.sx4.bot.http.HttpCallback in project Sx4 by sx4-discord-bot.

the class DuckCommand method onCommand.

public void onCommand(Sx4CommandEvent event) {
    Request request = new Request.Builder().url("https://random-d.uk/api/v1/random").build();
    event.getHttpClient().newCall(request).enqueue((HttpCallback) response -> {
        Document data = Document.parse(response.body().string());
        EmbedBuilder embed = new EmbedBuilder().setDescription(":duck:").setImage(data.getString("url"));
        event.reply(embed.build()).queue();
    });
}
Also used : Document(org.bson.Document) ModuleCategory(com.sx4.bot.category.ModuleCategory) Request(okhttp3.Request) HttpCallback(com.sx4.bot.http.HttpCallback) 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) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) Request(okhttp3.Request) Document(org.bson.Document)

Example 9 with HttpCallback

use of com.sx4.bot.http.HttpCallback in project Sx4 by sx4-discord-bot.

the class TTSCommand method onCommand.

public void onCommand(Sx4CommandEvent event, @Argument(value = "query", endless = true) @Limit(max = 200) String query) {
    Request request = new Request.Builder().url("https://translate.google.com/translate_tts?ie=UTF-8&client=tw-ob&tl=en-gb&q=" + URLEncoder.encode(query, StandardCharsets.UTF_8)).build();
    event.getHttpClient().newCall(request).enqueue((HttpCallback) response -> {
        event.replyFile(response.body().bytes(), query + ".mp3").queue();
    });
}
Also used : ModuleCategory(com.sx4.bot.category.ModuleCategory) Request(okhttp3.Request) URLEncoder(java.net.URLEncoder) HttpCallback(com.sx4.bot.http.HttpCallback) Sx4Command(com.sx4.bot.core.Sx4Command) Sx4CommandEvent(com.sx4.bot.core.Sx4CommandEvent) Permission(net.dv8tion.jda.api.Permission) StandardCharsets(java.nio.charset.StandardCharsets) Argument(com.jockie.bot.core.argument.Argument) Limit(com.sx4.bot.annotations.argument.Limit) Request(okhttp3.Request)

Example 10 with HttpCallback

use of com.sx4.bot.http.HttpCallback 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)

Aggregations

HttpCallback (com.sx4.bot.http.HttpCallback)73 Request (okhttp3.Request)71 ModuleCategory (com.sx4.bot.category.ModuleCategory)62 Sx4Command (com.sx4.bot.core.Sx4Command)62 Sx4CommandEvent (com.sx4.bot.core.Sx4CommandEvent)62 Argument (com.jockie.bot.core.argument.Argument)57 Permission (net.dv8tion.jda.api.Permission)57 Document (org.bson.Document)40 ImageRequest (com.sx4.bot.entities.image.ImageRequest)35 ImageUtility (com.sx4.bot.utility.ImageUtility)33 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)27 ImageUrl (com.sx4.bot.annotations.argument.ImageUrl)24 PagedResult (com.sx4.bot.paged.PagedResult)18 StandardCharsets (java.nio.charset.StandardCharsets)17 URLEncoder (java.net.URLEncoder)16 List (java.util.List)16 Option (com.jockie.bot.core.option.Option)13 OffsetDateTime (java.time.OffsetDateTime)11 ZoneOffset (java.time.ZoneOffset)11 Limit (com.sx4.bot.annotations.argument.Limit)10