From cdc45d62f2db14662a596d8dc111bc309801198a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beatrice=20Dellac=C3=A0?= Date: Mon, 21 Nov 2022 15:00:37 +0100 Subject: [PATCH] Move uptime string generator to config class --- .../wtf/beatrice/hidekobot/Configuration.java | 41 +++++++++++++++++++ .../commands/slash/BotInfoCommand.java | 30 +------------- 2 files changed, 42 insertions(+), 29 deletions(-) diff --git a/src/main/java/wtf/beatrice/hidekobot/Configuration.java b/src/main/java/wtf/beatrice/hidekobot/Configuration.java index 56d02a9..2d27116 100644 --- a/src/main/java/wtf/beatrice/hidekobot/Configuration.java +++ b/src/main/java/wtf/beatrice/hidekobot/Configuration.java @@ -6,7 +6,9 @@ import wtf.beatrice.hidekobot.database.DatabaseManager; import wtf.beatrice.hidekobot.listeners.MessageLogger; import java.awt.*; +import java.time.Duration; import java.time.LocalDateTime; +import java.time.temporal.ChronoUnit; import java.util.ArrayList; import java.util.Comparator; import java.util.List; @@ -211,4 +213,43 @@ public class Configuration */ public static LocalDateTime getStartupTime() { return startupTime; } + /** + * Generate a nicely formatted uptime String that omits unnecessary data + * (eg. 0 days, 0 hours, 4 minutes, 32 seconds -> 4m 32s) + * + * @return the formatter String + */ + public static String getNiceUptime() + { + LocalDateTime now = LocalDateTime.now(); + long uptimeSeconds = ChronoUnit.SECONDS.between(Configuration.getStartupTime(), now); + Duration uptime = Duration.ofSeconds(uptimeSeconds); + long seconds = uptime.toSecondsPart(); + long minutes = uptime.toMinutesPart(); + long hours = uptime.toHoursPart(); + long days = uptime.toDays(); + + StringBuilder uptimeStringBuilder = new StringBuilder(); + if(days == 0) + { + if(hours == 0) + { + if(minutes == 0) + {} else { + uptimeStringBuilder.append(minutes).append("m "); + } + } else { + uptimeStringBuilder.append(hours).append("h "); + uptimeStringBuilder.append(minutes).append("m "); + } + } else { + uptimeStringBuilder.append(days).append("d "); + uptimeStringBuilder.append(hours).append("h "); + uptimeStringBuilder.append(minutes).append("m "); + } + uptimeStringBuilder.append(seconds).append("s "); + + return uptimeStringBuilder.toString(); + } + } diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/slash/BotInfoCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/slash/BotInfoCommand.java index a692305..d12ab01 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/slash/BotInfoCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/slash/BotInfoCommand.java @@ -74,35 +74,7 @@ public class BotInfoCommand embedBuilder.addField("Author", authorMention, true); // uptime field - LocalDateTime now = LocalDateTime.now(); - long uptimeSeconds = ChronoUnit.SECONDS.between(Configuration.getStartupTime(), now); - Duration uptime = Duration.ofSeconds(uptimeSeconds); - long seconds = uptime.toSecondsPart(); - long minutes = uptime.toMinutesPart(); - long hours = uptime.toHoursPart(); - long days = uptime.toDays(); - - StringBuilder uptimeStringBuilder = new StringBuilder(); - if(days == 0) - { - if(hours == 0) - { - if(minutes == 0) - {} else { - uptimeStringBuilder.append(minutes).append("m "); - } - } else { - uptimeStringBuilder.append(hours).append("h "); - uptimeStringBuilder.append(minutes).append("m "); - } - } else { - uptimeStringBuilder.append(days).append("d "); - uptimeStringBuilder.append(hours).append("h "); - uptimeStringBuilder.append(minutes).append("m "); - } - uptimeStringBuilder.append(seconds).append("s "); - - embedBuilder.addField("Uptime", uptimeStringBuilder.toString(), true); + embedBuilder.addField("Uptime", Configuration.getNiceUptime(), true); // issue tracker field embedBuilder.addField("Support",