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