From e9f475cb590370a5882e1ce4af919ae8dce10f57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beatrice=20Dellac=C3=A0?= Date: Mon, 21 Nov 2022 15:02:40 +0100 Subject: [PATCH] Refactor code and packages --- .../wtf/beatrice/hidekobot/Configuration.java | 39 --------------- .../wtf/beatrice/hidekobot/HidekoBot.java | 8 +-- .../commands/slash/BotInfoCommand.java | 3 +- .../ExpiredMessageTask.java} | 7 +-- ...ommandsUtil.java => SlashCommandUtil.java} | 2 +- .../beatrice/hidekobot/utils/TimeUtil.java | 50 +++++++++++++++++++ 6 files changed, 61 insertions(+), 48 deletions(-) rename src/main/java/wtf/beatrice/hidekobot/{utils/ExpiredMessageRunner.java => runnables/ExpiredMessageTask.java} (95%) rename src/main/java/wtf/beatrice/hidekobot/utils/{SlashCommandsUtil.java => SlashCommandUtil.java} (99%) create mode 100644 src/main/java/wtf/beatrice/hidekobot/utils/TimeUtil.java diff --git a/src/main/java/wtf/beatrice/hidekobot/Configuration.java b/src/main/java/wtf/beatrice/hidekobot/Configuration.java index 2d27116..51a47c6 100644 --- a/src/main/java/wtf/beatrice/hidekobot/Configuration.java +++ b/src/main/java/wtf/beatrice/hidekobot/Configuration.java @@ -213,43 +213,4 @@ 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/HidekoBot.java b/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java index 8cc1451..486cbfb 100644 --- a/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java +++ b/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java @@ -11,9 +11,9 @@ import wtf.beatrice.hidekobot.listeners.ButtonInteractionListener; import wtf.beatrice.hidekobot.listeners.MessageListener; import wtf.beatrice.hidekobot.listeners.SlashCommandCompleter; import wtf.beatrice.hidekobot.listeners.SlashCommandListener; -import wtf.beatrice.hidekobot.utils.ExpiredMessageRunner; +import wtf.beatrice.hidekobot.runnables.ExpiredMessageTask; import wtf.beatrice.hidekobot.utils.Logger; -import wtf.beatrice.hidekobot.utils.SlashCommandsUtil; +import wtf.beatrice.hidekobot.utils.SlashCommandUtil; import java.io.File; import java.time.LocalDateTime; @@ -91,7 +91,7 @@ public class HidekoBot // update slash commands (delayed) final boolean finalForceUpdateCommands = forceUpdateCommands; Executors.newSingleThreadScheduledExecutor().schedule(() -> - SlashCommandsUtil.updateSlashCommands(finalForceUpdateCommands), 1, TimeUnit.SECONDS); + SlashCommandUtil.updateSlashCommands(finalForceUpdateCommands), 1, TimeUnit.SECONDS); // set the bot's status jda.getPresence().setStatus(OnlineStatus.ONLINE); @@ -115,7 +115,7 @@ public class HidekoBot // start scheduled runnables ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(); - ExpiredMessageRunner task = new ExpiredMessageRunner(); + ExpiredMessageTask task = new ExpiredMessageTask(); int initDelay = 5; int periodicDelay = 5; scheduler.scheduleAtFixedRate(task, initDelay, periodicDelay, TimeUnit.SECONDS); 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 d12ab01..cba289e 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/slash/BotInfoCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/slash/BotInfoCommand.java @@ -6,6 +6,7 @@ import net.dv8tion.jda.api.interactions.commands.Command; import org.jetbrains.annotations.NotNull; import wtf.beatrice.hidekobot.Configuration; import wtf.beatrice.hidekobot.HidekoBot; +import wtf.beatrice.hidekobot.utils.TimeUtil; import java.lang.management.ManagementFactory; import java.text.DecimalFormat; @@ -74,7 +75,7 @@ public class BotInfoCommand embedBuilder.addField("Author", authorMention, true); // uptime field - embedBuilder.addField("Uptime", Configuration.getNiceUptime(), true); + embedBuilder.addField("Uptime", TimeUtil.getNiceUptime(), true); // issue tracker field embedBuilder.addField("Support", diff --git a/src/main/java/wtf/beatrice/hidekobot/utils/ExpiredMessageRunner.java b/src/main/java/wtf/beatrice/hidekobot/runnables/ExpiredMessageTask.java similarity index 95% rename from src/main/java/wtf/beatrice/hidekobot/utils/ExpiredMessageRunner.java rename to src/main/java/wtf/beatrice/hidekobot/runnables/ExpiredMessageTask.java index a0b62f6..9d0cb79 100644 --- a/src/main/java/wtf/beatrice/hidekobot/utils/ExpiredMessageRunner.java +++ b/src/main/java/wtf/beatrice/hidekobot/runnables/ExpiredMessageTask.java @@ -1,4 +1,4 @@ -package wtf.beatrice.hidekobot.utils; +package wtf.beatrice.hidekobot.runnables; import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.Message; @@ -8,20 +8,21 @@ import net.dv8tion.jda.api.requests.RestAction; import wtf.beatrice.hidekobot.Configuration; import wtf.beatrice.hidekobot.HidekoBot; import wtf.beatrice.hidekobot.database.DatabaseManager; +import wtf.beatrice.hidekobot.utils.Logger; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.List; -public class ExpiredMessageRunner implements Runnable { +public class ExpiredMessageTask implements Runnable { private final DateTimeFormatter formatter; private final Logger logger; private DatabaseManager databaseManager; - public ExpiredMessageRunner() + public ExpiredMessageTask() { String format = Configuration.getExpiryTimestampFormat(); formatter = DateTimeFormatter.ofPattern(format); diff --git a/src/main/java/wtf/beatrice/hidekobot/utils/SlashCommandsUtil.java b/src/main/java/wtf/beatrice/hidekobot/utils/SlashCommandUtil.java similarity index 99% rename from src/main/java/wtf/beatrice/hidekobot/utils/SlashCommandsUtil.java rename to src/main/java/wtf/beatrice/hidekobot/utils/SlashCommandUtil.java index 086a891..abd99ae 100644 --- a/src/main/java/wtf/beatrice/hidekobot/utils/SlashCommandsUtil.java +++ b/src/main/java/wtf/beatrice/hidekobot/utils/SlashCommandUtil.java @@ -14,7 +14,7 @@ import wtf.beatrice.hidekobot.listeners.MessageListener; import java.util.ArrayList; import java.util.List; -public class SlashCommandsUtil +public class SlashCommandUtil { private static final Logger logger = new Logger(MessageListener.class); diff --git a/src/main/java/wtf/beatrice/hidekobot/utils/TimeUtil.java b/src/main/java/wtf/beatrice/hidekobot/utils/TimeUtil.java new file mode 100644 index 0000000..a7512d2 --- /dev/null +++ b/src/main/java/wtf/beatrice/hidekobot/utils/TimeUtil.java @@ -0,0 +1,50 @@ +package wtf.beatrice.hidekobot.utils; + +import wtf.beatrice.hidekobot.Configuration; + +import java.time.Duration; +import java.time.LocalDateTime; +import java.time.temporal.ChronoUnit; + +public class TimeUtil +{ + + /** + * 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(); + } +}