From 8faa9c467777884b22dbda3293581466e38a1b92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beatrice=20Dellac=C3=A0?= Date: Tue, 22 Nov 2022 00:28:33 +0100 Subject: [PATCH] Refactor datasource classes --- .../java/wtf/beatrice/hidekobot/Cache.java | 40 +++++++++---------- .../wtf/beatrice/hidekobot/HidekoBot.java | 16 ++++---- .../commands/slash/ClearChatCommand.java | 6 +-- .../commands/slash/CoinFlipCommand.java | 6 +-- .../ConfigurationSource.java} | 7 ++-- .../DatabaseSource.java} | 6 +-- .../runnables/ExpiredMessageTask.java | 38 +++++++++--------- 7 files changed, 60 insertions(+), 59 deletions(-) rename src/main/java/wtf/beatrice/hidekobot/{utils/ConfigurationManager.java => datasource/ConfigurationSource.java} (96%) rename src/main/java/wtf/beatrice/hidekobot/{database/DatabaseManager.java => datasource/DatabaseSource.java} (99%) diff --git a/src/main/java/wtf/beatrice/hidekobot/Cache.java b/src/main/java/wtf/beatrice/hidekobot/Cache.java index a87641a..8930c1e 100644 --- a/src/main/java/wtf/beatrice/hidekobot/Cache.java +++ b/src/main/java/wtf/beatrice/hidekobot/Cache.java @@ -2,9 +2,9 @@ package wtf.beatrice.hidekobot; import net.dv8tion.jda.api.interactions.commands.Command; import org.jetbrains.annotations.Nullable; -import wtf.beatrice.hidekobot.database.DatabaseManager; +import wtf.beatrice.hidekobot.datasource.DatabaseSource; import wtf.beatrice.hidekobot.listeners.MessageLogger; -import wtf.beatrice.hidekobot.utils.ConfigurationManager; +import wtf.beatrice.hidekobot.datasource.ConfigurationSource; import wtf.beatrice.hidekobot.utils.Logger; import java.awt.*; @@ -19,8 +19,8 @@ public class Cache private static final Logger logger = new Logger(Cache.class); - private static ConfigurationManager configManager = null; - private static DatabaseManager dbManager = null; + private static ConfigurationSource configurationSource = null; + private static DatabaseSource databaseSource = null; private static boolean verbose = false; private static MessageLogger verbosityLogger; private static final long botMaintainerId = 979809420714332260L; @@ -94,7 +94,7 @@ public class Cache * @return a long of the account's id */ public static long getBotOwnerId() { - return configManager == null ? 0L : (Long) configManager.getConfigValue("bot-owner-id"); + return configurationSource == null ? 0L : (Long) configurationSource.getConfigValue("bot-owner-id"); } @@ -104,7 +104,7 @@ public class Cache * @return a String of the bot's token. */ public static String getBotToken() { - return configManager == null ? null : (String) configManager.getConfigValue("bot-token"); + return configurationSource == null ? null : (String) configurationSource.getConfigValue("bot-token"); } /** @@ -142,21 +142,21 @@ public class Cache } /** - * Set the already fully-initialized DatabaseManager instance, ready to be accessed and used. + * Set the already fully-initialized DatabaseSource instance, ready to be accessed and used. * - * @param databaseManagerInstance the fully-initialized DatabaseManager instance. + * @param databaseSourceInstance the fully-initialized DatabaseSource instance. */ - public static void setDatabaseManagerInstance(DatabaseManager databaseManagerInstance) + public static void setDatabaseSourceInstance(DatabaseSource databaseSourceInstance) { - dbManager = databaseManagerInstance; + databaseSource = databaseSourceInstance; } /** - * Get the fully-initialized DatabaseManager instance, ready to be used. + * Get the fully-initialized DatabaseSource instance, ready to be used. * - * @return the DatabaseManager instance. + * @return the DatabaseSource instance. */ - public static @Nullable DatabaseManager getDatabaseManager() { return dbManager; } + public static @Nullable DatabaseSource getDatabaseSource() { return databaseSource; } /** * Get the DateTimeFormatter string for parsing the expired messages timestamp. @@ -189,8 +189,8 @@ public class Cache */ public static Color getBotColor() { Color defaultColor = Color.PINK; - if(configManager == null) return defaultColor; - String colorName = (String) configManager.getConfigValue("bot-color"); + if(configurationSource == null) return defaultColor; + String colorName = (String) configurationSource.getConfigValue("bot-color"); Color color = null; try { @@ -246,15 +246,15 @@ public class Cache public static LocalDateTime getStartupTime() { return startupTime; } public static String getFullHeartBeatLink() { - return configManager == null ? null : (String) configManager.getConfigValue("heartbeat-link"); + return configurationSource == null ? null : (String) configurationSource.getConfigValue("heartbeat-link"); } //todo javadocs public static String getExecPath() { return execPath; } - private static ConfigurationManager getConfigurationManager() - { return configManager; } + /*private static ConfigurationSource getConfigurationSource() + { return configurationSource; }*/ - public static void setConfigManager(ConfigurationManager configurationManager) - { configManager = configurationManager; } + public static void setConfigurationSource(ConfigurationSource configurationSource) + { Cache.configurationSource = configurationSource; } } diff --git a/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java b/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java index 771b2fb..45eb304 100644 --- a/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java +++ b/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java @@ -6,7 +6,7 @@ import net.dv8tion.jda.api.OnlineStatus; import net.dv8tion.jda.api.entities.Activity; import net.dv8tion.jda.api.requests.GatewayIntent; import sun.misc.Signal; -import wtf.beatrice.hidekobot.database.DatabaseManager; +import wtf.beatrice.hidekobot.datasource.DatabaseSource; import wtf.beatrice.hidekobot.listeners.ButtonInteractionListener; import wtf.beatrice.hidekobot.listeners.MessageListener; import wtf.beatrice.hidekobot.listeners.SlashCommandCompleter; @@ -14,7 +14,7 @@ import wtf.beatrice.hidekobot.listeners.SlashCommandListener; import wtf.beatrice.hidekobot.runnables.CommandsUpdateTask; import wtf.beatrice.hidekobot.runnables.ExpiredMessageTask; import wtf.beatrice.hidekobot.runnables.HeartBeatTask; -import wtf.beatrice.hidekobot.utils.ConfigurationManager; +import wtf.beatrice.hidekobot.datasource.ConfigurationSource; import wtf.beatrice.hidekobot.utils.Logger; import wtf.beatrice.hidekobot.utils.SlashCommandUtil; @@ -39,9 +39,9 @@ public class HidekoBot // load configuration logger.log("Loading configuration..."); String configFilePath = Cache.getExecPath() + File.separator + "config.yml"; - ConfigurationManager configurationManager = new ConfigurationManager(configFilePath); - configurationManager.initConfig(); - Cache.setConfigManager(configurationManager); + ConfigurationSource configurationSource = new ConfigurationSource(configFilePath); + configurationSource.initConfig(); + Cache.setConfigurationSource(configurationSource); logger.log("Configuration loaded!"); String botToken = Cache.getBotToken(); @@ -115,11 +115,11 @@ public class HidekoBot // connect to database logger.log("Connecting to database..."); String dbFilePath = Cache.getExecPath() + File.separator + "db.sqlite"; // in current directory - DatabaseManager dbManager = new DatabaseManager(dbFilePath); - if(dbManager.connect() && dbManager.initDb()) + DatabaseSource databaseSource = new DatabaseSource(dbFilePath); + if(databaseSource.connect() && databaseSource.initDb()) { logger.log("Database connection initialized!"); - Cache.setDatabaseManagerInstance(dbManager); + Cache.setDatabaseSourceInstance(databaseSource); // load data here... diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/slash/ClearChatCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/slash/ClearChatCommand.java index e64acbf..f063e38 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/slash/ClearChatCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/slash/ClearChatCommand.java @@ -165,8 +165,8 @@ public class ClearChatCommand .setActionRow(dismissButton) .complete(); - Cache.getDatabaseManager().queueDisabling(message); - Cache.getDatabaseManager().trackRanCommandReply(message, event.getUser()); + Cache.getDatabaseSource().queueDisabling(message); + Cache.getDatabaseSource().trackRanCommandReply(message, event.getUser()); } }).start(); @@ -176,7 +176,7 @@ public class ClearChatCommand public void dismissMessage(ButtonInteractionEvent event) { - if(!(Cache.getDatabaseManager().isUserTrackedFor(event.getUser().getId(), event.getMessageId()))) + if(!(Cache.getDatabaseSource().isUserTrackedFor(event.getUser().getId(), event.getMessageId()))) { event.reply("❌ You did not run this command!").setEphemeral(true).queue(); } else diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/slash/CoinFlipCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/slash/CoinFlipCommand.java index e4d53af..c4dc345 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/slash/CoinFlipCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/slash/CoinFlipCommand.java @@ -38,7 +38,7 @@ public class CoinFlipCommand public void buttonReFlip(ButtonInteractionEvent event) { // check if the user interacting is the same one who ran the command - if(!(Cache.getDatabaseManager().isUserTrackedFor(event.getUser().getId(), event.getMessageId()))) + if(!(Cache.getDatabaseSource().isUserTrackedFor(event.getUser().getId(), event.getMessageId()))) { event.reply("❌ You did not run this command!").setEphemeral(true).queue(); return; @@ -63,8 +63,8 @@ public class CoinFlipCommand { String replyMessageId = replyMessage.getId(); - Cache.getDatabaseManager().queueDisabling(replyMessage); - Cache.getDatabaseManager().trackRanCommandReply(replyMessage, user); + Cache.getDatabaseSource().queueDisabling(replyMessage); + Cache.getDatabaseSource().trackRanCommandReply(replyMessage, user); } private String genRandom() diff --git a/src/main/java/wtf/beatrice/hidekobot/utils/ConfigurationManager.java b/src/main/java/wtf/beatrice/hidekobot/datasource/ConfigurationSource.java similarity index 96% rename from src/main/java/wtf/beatrice/hidekobot/utils/ConfigurationManager.java rename to src/main/java/wtf/beatrice/hidekobot/datasource/ConfigurationSource.java index 9b3b17c..a8aee90 100644 --- a/src/main/java/wtf/beatrice/hidekobot/utils/ConfigurationManager.java +++ b/src/main/java/wtf/beatrice/hidekobot/datasource/ConfigurationSource.java @@ -1,13 +1,14 @@ -package wtf.beatrice.hidekobot.utils; +package wtf.beatrice.hidekobot.datasource; import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.Yaml; import wtf.beatrice.hidekobot.HidekoBot; +import wtf.beatrice.hidekobot.utils.Logger; import java.io.*; import java.util.LinkedHashMap; -public class ConfigurationManager +public class ConfigurationSource { @@ -15,7 +16,7 @@ public class ConfigurationManager private final Logger logger; private final String configFilePath; - public ConfigurationManager(String configFilePath) + public ConfigurationSource(String configFilePath) { this.configFilePath = configFilePath; logger = new Logger(getClass()); diff --git a/src/main/java/wtf/beatrice/hidekobot/database/DatabaseManager.java b/src/main/java/wtf/beatrice/hidekobot/datasource/DatabaseSource.java similarity index 99% rename from src/main/java/wtf/beatrice/hidekobot/database/DatabaseManager.java rename to src/main/java/wtf/beatrice/hidekobot/datasource/DatabaseSource.java index 7b4140b..19d024f 100644 --- a/src/main/java/wtf/beatrice/hidekobot/database/DatabaseManager.java +++ b/src/main/java/wtf/beatrice/hidekobot/datasource/DatabaseSource.java @@ -1,4 +1,4 @@ -package wtf.beatrice.hidekobot.database; +package wtf.beatrice.hidekobot.datasource; import net.dv8tion.jda.api.entities.Message; import net.dv8tion.jda.api.entities.User; @@ -12,7 +12,7 @@ import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.List; -public class DatabaseManager +public class DatabaseSource { private final static String sqliteURL = "jdbc:sqlite:%path%"; @@ -20,7 +20,7 @@ public class DatabaseManager private final String dbPath; private final Logger logger; - public DatabaseManager(String dbPath) + public DatabaseSource(String dbPath) { this.dbPath = dbPath; this.logger = new Logger(getClass()); diff --git a/src/main/java/wtf/beatrice/hidekobot/runnables/ExpiredMessageTask.java b/src/main/java/wtf/beatrice/hidekobot/runnables/ExpiredMessageTask.java index b046c31..93f0b6e 100644 --- a/src/main/java/wtf/beatrice/hidekobot/runnables/ExpiredMessageTask.java +++ b/src/main/java/wtf/beatrice/hidekobot/runnables/ExpiredMessageTask.java @@ -9,7 +9,7 @@ import net.dv8tion.jda.api.interactions.components.LayoutComponent; import net.dv8tion.jda.api.requests.RestAction; import wtf.beatrice.hidekobot.Cache; import wtf.beatrice.hidekobot.HidekoBot; -import wtf.beatrice.hidekobot.database.DatabaseManager; +import wtf.beatrice.hidekobot.datasource.DatabaseSource; import wtf.beatrice.hidekobot.utils.Logger; import java.time.LocalDateTime; @@ -21,14 +21,14 @@ public class ExpiredMessageTask implements Runnable { private final DateTimeFormatter formatter; private final Logger logger; - private DatabaseManager databaseManager; + private DatabaseSource databaseSource; public ExpiredMessageTask() { String format = Cache.getExpiryTimestampFormat(); formatter = DateTimeFormatter.ofPattern(format); - databaseManager = Cache.getDatabaseManager(); + databaseSource = Cache.getDatabaseSource(); logger = new Logger(getClass()); } @@ -36,10 +36,10 @@ public class ExpiredMessageTask implements Runnable { @Override public void run() { - databaseManager = Cache.getDatabaseManager(); - if(databaseManager == null) return; + databaseSource = Cache.getDatabaseSource(); + if(databaseSource == null) return; - List expiringMessages = Cache.getDatabaseManager().getQueuedExpiringMessages(); + List expiringMessages = Cache.getDatabaseSource().getQueuedExpiringMessages(); if(expiringMessages == null || expiringMessages.isEmpty()) return; LocalDateTime now = LocalDateTime.now(); @@ -49,11 +49,11 @@ public class ExpiredMessageTask implements Runnable { if(Cache.isVerbose()) logger.log("expired check: " + messageId); - String expiryTimestamp = databaseManager.getQueuedExpiringMessageExpiryDate(messageId); + String expiryTimestamp = databaseSource.getQueuedExpiringMessageExpiryDate(messageId); if(expiryTimestamp == null || expiryTimestamp.equals("")) // if missing timestamp { // count it as already expired - databaseManager.untrackExpiredMessage(messageId); + databaseSource.untrackExpiredMessage(messageId); // move on to next message continue; } @@ -73,9 +73,9 @@ public class ExpiredMessageTask implements Runnable { private void disableExpired(String messageId) { - String channelId = databaseManager.getQueuedExpiringMessageChannel(messageId); + String channelId = databaseSource.getQueuedExpiringMessageChannel(messageId); - ChannelType msgChannelType = databaseManager.getTrackedMessageChannelType(messageId); + ChannelType msgChannelType = databaseSource.getTrackedMessageChannelType(messageId); MessageChannel textChannel = null; @@ -83,20 +83,20 @@ public class ExpiredMessageTask implements Runnable { // this should never happen, but only message channels are supported. if(!msgChannelType.isMessage()) { - databaseManager.untrackExpiredMessage(messageId); + databaseSource.untrackExpiredMessage(messageId); return; } // if this is a DM if(msgChannelType == ChannelType.PRIVATE) { - String userId = databaseManager.getTrackedReplyUserId(messageId); + String userId = databaseSource.getTrackedReplyUserId(messageId); User user = HidekoBot.getAPI().retrieveUserById(userId).complete(); if(user == null) { // if user is not found, consider it expired // (deleted profile, or blocked the bot) - databaseManager.untrackExpiredMessage(messageId); + databaseSource.untrackExpiredMessage(messageId); return; } @@ -104,13 +104,13 @@ public class ExpiredMessageTask implements Runnable { } else { - String guildId = databaseManager.getQueuedExpiringMessageGuild(messageId); + String guildId = databaseSource.getQueuedExpiringMessageGuild(messageId); Guild guild = HidekoBot.getAPI().getGuildById(guildId); if(guild == null) { // if guild is not found, consider it expired // (server was deleted or bot was kicked) - databaseManager.untrackExpiredMessage(messageId); + databaseSource.untrackExpiredMessage(messageId); return; } textChannel = guild.getTextChannelById(channelId); @@ -120,7 +120,7 @@ public class ExpiredMessageTask implements Runnable { { // if channel is not found, count it as expired // (channel was deleted or bot permissions restricted) - databaseManager.untrackExpiredMessage(messageId); + databaseSource.untrackExpiredMessage(messageId); return; } @@ -134,7 +134,7 @@ public class ExpiredMessageTask implements Runnable { message -> { if(message == null) { - databaseManager.untrackExpiredMessage(messageId); + databaseSource.untrackExpiredMessage(messageId); return; } @@ -147,11 +147,11 @@ public class ExpiredMessageTask implements Runnable { } message.editMessageComponents(newComponents).queue(); - databaseManager.untrackExpiredMessage(messageId); + databaseSource.untrackExpiredMessage(messageId); }, (error) -> { - databaseManager.untrackExpiredMessage(messageId); + databaseSource.untrackExpiredMessage(messageId); }); } }