Refactor code and packages
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Bea 2022-11-21 15:02:40 +01:00
parent cdc45d62f2
commit e9f475cb59
6 changed files with 61 additions and 48 deletions

View File

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

View File

@ -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);

View File

@ -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",

View File

@ -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);

View File

@ -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);

View File

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