diff --git a/src/main/java/wtf/beatrice/hidekobot/Cache.java b/src/main/java/wtf/beatrice/hidekobot/Cache.java index 07df5be..d0930bf 100644 --- a/src/main/java/wtf/beatrice/hidekobot/Cache.java +++ b/src/main/java/wtf/beatrice/hidekobot/Cache.java @@ -5,8 +5,10 @@ import org.jetbrains.annotations.Nullable; import wtf.beatrice.hidekobot.database.DatabaseManager; import wtf.beatrice.hidekobot.listeners.MessageLogger; import wtf.beatrice.hidekobot.utils.ConfigurationManager; +import wtf.beatrice.hidekobot.utils.Logger; import java.awt.*; +import java.lang.reflect.Field; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.Comparator; @@ -15,13 +17,13 @@ import java.util.List; public class Cache { + + private static final Logger logger = new Logger(Cache.class); private static ConfigurationManager configManager = null; private static DatabaseManager dbManager = null; private static boolean verbose = false; private static MessageLogger verbosityLogger; private static final long botMaintainerId = 979809420714332260L; - private static long botOwnerId = 0L; - private final static String expiryTimestampFormat = "yy/MM/dd HH:mm:ss"; // note: discord sets interactions' expiry time to 15 minutes by default, so we can't go higher than that. @@ -30,16 +32,10 @@ public class Cache // used to count eg. uptime private static LocalDateTime startupTime; - // todo: allow people to set their own url - private static final String heartbeatLink = "https://status.beatrice.wtf/api/push/%apikey%?status=up&msg=OK&ping="; - private static String heartbeatApiKey = ""; - private final static String execPath = System.getProperty("user.dir"); private static final String botVersion = "0.1.5-slash"; // we should probably find a way to make this consistent with Maven private static final String botName = "HidekoBot"; - private static final Color botColor = Color.PINK; - private static List registeredCommands = new ArrayList<>(); private final static String defaultInviteLink = @@ -97,7 +93,19 @@ public class Cache * * @return a long of the account's id */ - public static long getBotOwnerId() { return botOwnerId; } + public static long getBotOwnerId() { + return configManager == null ? 0L : (Long) configManager.getConfigValue("bot-owner-id"); + } + + + /** + * Get the bot's token. + * + * @return a String of the bot's token. + */ + public static String getBotToken() { + return configManager == null ? null : (String) configManager.getConfigValue("bot-token"); + } /** * Get the bot maintainer's profile id. @@ -179,7 +187,20 @@ public class Cache * * @return the Color object. */ - public static Color getBotColor() { return botColor; } + public static Color getBotColor() { + Color defaultColor = Color.PINK; + if(configManager == null) return defaultColor; + String colorName = (String) configManager.getConfigValue("bot-color"); + + Color color = null; + try { + Field field = Color.class.getField(colorName); + color = (Color)field.get(null); + } catch (Exception e) { + logger.log("Unknown color: " + colorName); + } + return color == null ? defaultColor : color; + } /** * Set the list of registered commands. They will be sorted alphabetically. @@ -225,18 +246,12 @@ public class Cache public static LocalDateTime getStartupTime() { return startupTime; } public static String getFullHeartBeatLink() { - return heartbeatLink.replace("%apikey%", heartbeatApiKey); + return configManager == null ? null : (String) configManager.getConfigValue("heartbeat-link"); } //todo javadocs - public static void setHeartBeatApiKey(String key) { - heartbeatApiKey = key; - } - public static String getHeartBeatApiKey() { - return heartbeatApiKey; - } public static String getExecPath() { return execPath; } - public static ConfigurationManager getConfigurationManager() + private static ConfigurationManager getConfigurationManager() { return configManager; } public static void setConfigManager(ConfigurationManager configurationManager) diff --git a/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java b/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java index 33768c9..771b2fb 100644 --- a/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java +++ b/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java @@ -29,27 +29,29 @@ import java.util.concurrent.TimeUnit; public class HidekoBot { - private static String botToken; - private static JDA jda; - - // create a logger instance for ease of use private static final Logger logger = new Logger(HidekoBot.class); public static void main(String[] args) { - // check if bot token was specified as a startup argument - if(args.length < 1) + // load configuration + logger.log("Loading configuration..."); + String configFilePath = Cache.getExecPath() + File.separator + "config.yml"; + ConfigurationManager configurationManager = new ConfigurationManager(configFilePath); + configurationManager.initConfig(); + Cache.setConfigManager(configurationManager); + logger.log("Configuration loaded!"); + + String botToken = Cache.getBotToken(); + if(botToken == null || botToken.isEmpty()) { - logger.log("Please specify your bot token!"); + logger.log("Invalid bot token!"); + shutdown(); return; } - // load token from args - botToken = args[0]; - try { // try to create the bot object and authenticate it with discord. @@ -79,19 +81,18 @@ public class HidekoBot // if there is more than 1 arg, then iterate through them because we have additional things to do. // we are doing this at the end because we might need the API to be already initialized for some things. if(args.length > 1) { - List argsList = new ArrayList<>(Arrays.asList(args).subList(1, args.length)); + List argsList = new ArrayList<>(Arrays.asList(args)); + + // NOTE: do not replace with enhanced for, since we might need + // to know what position we're at or do further elaboration of the string. + // we were using this for api key parsing in the past. for(int i = 0; i < argsList.size(); i++) { String arg = argsList.get(i); if(arg.equals("verbose")) Cache.setVerbose(true); if(arg.equals("refresh")) forceUpdateCommands = true; - if(arg.startsWith("heartbeat=")) - { - String apiKey = arg.replaceAll(".*=", ""); //remove the "heartbeat=" part - Cache.setHeartBeatApiKey(apiKey); - } } } @@ -111,13 +112,6 @@ public class HidekoBot jda.getPresence().setStatus(OnlineStatus.ONLINE); jda.getPresence().setActivity(Activity.playing("Hatsune Miku: Project DIVA")); - - // load configuration - String configFilePath = Cache.getExecPath() + File.separator + "config.yml"; - ConfigurationManager configurationManager = new ConfigurationManager(configFilePath); - configurationManager.initConfig(); - Cache.setConfigManager(configurationManager); - // connect to database logger.log("Connecting to database..."); String dbFilePath = Cache.getExecPath() + File.separator + "db.sqlite"; // in current directory diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/slash/DieCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/slash/DieCommand.java index b9b94e3..c8d888d 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/slash/DieCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/slash/DieCommand.java @@ -13,7 +13,7 @@ public class DieCommand public void runSlashCommand(@NotNull SlashCommandInteractionEvent event) { - if(Cache.getBotOwnerId() != event.getMember().getIdLong()) + if(Cache.getBotOwnerId() != event.getUser().getIdLong()) { event.reply("Sorry, only the bot owner can run this command!").setEphemeral(true).queue(); } else { diff --git a/src/main/java/wtf/beatrice/hidekobot/runnables/HeartBeatTask.java b/src/main/java/wtf/beatrice/hidekobot/runnables/HeartBeatTask.java index ec077cb..0a455b6 100644 --- a/src/main/java/wtf/beatrice/hidekobot/runnables/HeartBeatTask.java +++ b/src/main/java/wtf/beatrice/hidekobot/runnables/HeartBeatTask.java @@ -21,10 +21,9 @@ public class HeartBeatTask implements Runnable @Override public void run() { - String apiKey = Cache.getHeartBeatApiKey(); - if(apiKey == null || apiKey.isEmpty()) return; - String urlString = Cache.getFullHeartBeatLink(); + if(urlString == null || urlString.isEmpty()) return; + try { URL heartbeatUrl = new URL(urlString); @@ -46,7 +45,7 @@ public class HeartBeatTask implements Runnable } } catch (IOException e) { - logger.log("Error while trying to push heartbeat: " + e.getMessage()); + logger.log("Error while trying to push heartbeat: " + e.getMessage() + ", " + e.getCause().getMessage()); } } diff --git a/src/main/java/wtf/beatrice/hidekobot/utils/ConfigurationManager.java b/src/main/java/wtf/beatrice/hidekobot/utils/ConfigurationManager.java index f6153cc..9b3b17c 100644 --- a/src/main/java/wtf/beatrice/hidekobot/utils/ConfigurationManager.java +++ b/src/main/java/wtf/beatrice/hidekobot/utils/ConfigurationManager.java @@ -127,9 +127,9 @@ public class ConfigurationManager private void loadConfig(LinkedHashMap configurationEntries) { - configurationEntries.putAll(configurationEntries); + this.configurationEntries.putAll(configurationEntries); } - private Object getConfigValue(String key) + public Object getConfigValue(String key) { return configurationEntries.get(key); } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index ed58365..103c28a 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -1,4 +1,4 @@ bot-token: 'MTAxMjUzNzI5MTMwODI4NjAyMw.GWeNuh.00000000000000000000000000000000000000' -bot-owner-id: '000000000000000000' +bot-owner-id: 000000000000000000 bot-color: 'PINK' heartbeat-link: 'https://your-heartbeat-api.com/api/push/apikey?status=up&msg=OK&ping=' \ No newline at end of file