From cb487b5939464a0163c6506ddfd8d4ac0155d730 Mon Sep 17 00:00:00 2001 From: ChomeNS <95471003+ChomeNS@users.noreply.github.com> Date: Mon, 30 Dec 2024 14:31:41 +0700 Subject: [PATCH] refactor: weather command refactoring which includes more features --- build-number.txt | 2 +- .../chomens_bot/commands/WeatherCommand.java | 58 ++++++++++++++----- 2 files changed, 45 insertions(+), 15 deletions(-) diff --git a/build-number.txt b/build-number.txt index 57d72ebe..c197cc7a 100644 --- a/build-number.txt +++ b/build-number.txt @@ -1 +1 @@ -1365 \ No newline at end of file +1369 \ No newline at end of file diff --git a/src/main/java/me/chayapak1/chomens_bot/commands/WeatherCommand.java b/src/main/java/me/chayapak1/chomens_bot/commands/WeatherCommand.java index 94ab52d0..0690cbeb 100644 --- a/src/main/java/me/chayapak1/chomens_bot/commands/WeatherCommand.java +++ b/src/main/java/me/chayapak1/chomens_bot/commands/WeatherCommand.java @@ -12,12 +12,10 @@ import me.chayapak1.chomens_bot.util.HttpUtilities; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; +import java.net.URI; import java.net.URL; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; -import java.time.ZoneId; -import java.time.ZonedDateTime; -import java.time.format.DateTimeFormatter; public class WeatherCommand extends Command { public WeatherCommand () { @@ -39,27 +37,20 @@ public class WeatherCommand extends Command { final Gson gson = new Gson(); try { - final URL url = new URL( + final URL url = new URI( "https://api.weatherapi.com/v1/current.json?key=" + bot.config.weatherApiKey + "&q=" + URLEncoder.encode( location, StandardCharsets.UTF_8 ) - + "&aqi=no" - ); + ).toURL(); final String jsonOutput = HttpUtilities.getRequest(url); final JsonObject jsonObject = gson.fromJson(jsonOutput, JsonObject.class); - final ZoneId zoneId = ZoneId.of(jsonObject.get("location").getAsJsonObject().get("tz_id").getAsString()); - final ZonedDateTime zonedDateTime = ZonedDateTime.now(zoneId); - - final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("hh:mm:ss a"); - final String time = zonedDateTime.format(formatter); - return Component.translatable( - "Weather forecast for %s, %s:\n%s (%s), feels like %s (%s)\nTime: %s", + "Current weather for %s, %s:\n%s (%s), feels like %s (%s)\nCondition: %s\nCloud cover: %s\nHumidity: %s\nTime: %s", Component.text(jsonObject.get("location").getAsJsonObject().get("name").getAsString()).color(ColorUtilities.getColorByString(bot.config.colorPalette.string)), Component.text(jsonObject.get("location").getAsJsonObject().get("country").getAsString()).color(ColorUtilities.getColorByString(bot.config.colorPalette.string)), Component @@ -102,7 +93,46 @@ public class WeatherCommand extends Command { .getAsString() + "°F" ) .color(NamedTextColor.GREEN), - Component.text(time).color(ColorUtilities.getColorByString(bot.config.colorPalette.string)) + Component + .text( + jsonObject + .get("current") + .getAsJsonObject() + .get("condition") + .getAsJsonObject() + .get("text") + .getAsString() + ) + .color(ColorUtilities.getColorByString(bot.config.colorPalette.string)), + Component + .text( + jsonObject + .get("current") + .getAsJsonObject() + .get("cloud") + .getAsInt() + ) + .append(Component.text("%")) + .color(ColorUtilities.getColorByString(bot.config.colorPalette.number)), + Component + .text( + jsonObject + .get("current") + .getAsJsonObject() + .get("humidity") + .getAsInt() + ) + .append(Component.text("%")) + .color(ColorUtilities.getColorByString(bot.config.colorPalette.number)), + Component + .text( + jsonObject + .get("location") + .getAsJsonObject() + .get("localtime") + .getAsString() + ) + .color(ColorUtilities.getColorByString(bot.config.colorPalette.string)) ).color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor)); } catch (Exception e) { throw new CommandException(Component.text("Location \"" + location + "\" not found"));