From 702aead53a2bc60f9efaa8c5be6ac8fad6114d72 Mon Sep 17 00:00:00 2001 From: Liam Date: Sun, 30 Jun 2024 04:13:54 +0100 Subject: [PATCH] cleanup dev mode --- .../cc/fascinated/bat/BatApplication.java | 3 +++ .../fascinated/bat/common/ProfileHolder.java | 2 +- .../java/cc/fascinated/bat/config/Config.java | 20 +++++++++++++++++++ .../bat/service/CommandService.java | 16 +++++++++++++-- src/main/resources/application.yml | 3 +++ 5 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 src/main/java/cc/fascinated/bat/config/Config.java diff --git a/src/main/java/cc/fascinated/bat/BatApplication.java b/src/main/java/cc/fascinated/bat/BatApplication.java index a5e1e32..6a3716d 100644 --- a/src/main/java/cc/fascinated/bat/BatApplication.java +++ b/src/main/java/cc/fascinated/bat/BatApplication.java @@ -1,5 +1,6 @@ package cc.fascinated.bat; +import cc.fascinated.bat.config.Config; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import io.mongock.runner.springboot.EnableMongock; @@ -37,5 +38,7 @@ public class BatApplication { // Start the app SpringApplication.run(BatApplication.class, args); + + log.info("APP IS RUNNING IN %s MODE!!!!!!!!!".formatted(Config.isProduction() ? "PRODUCTION" : "DEVELOPMENT")); } } \ No newline at end of file diff --git a/src/main/java/cc/fascinated/bat/common/ProfileHolder.java b/src/main/java/cc/fascinated/bat/common/ProfileHolder.java index 75e490c..42a9435 100644 --- a/src/main/java/cc/fascinated/bat/common/ProfileHolder.java +++ b/src/main/java/cc/fascinated/bat/common/ProfileHolder.java @@ -27,7 +27,7 @@ public class ProfileHolder { profiles = new HashMap<>(); } - Profile profile = profiles.values().stream().filter(p -> p.getClass().equals(clazz)).findFirst().orElse(null); + Profile profile = profiles.values().stream().filter(clazz::isInstance).findFirst().orElse(null); if (profile == null) { try { profile = clazz.newInstance(); diff --git a/src/main/java/cc/fascinated/bat/config/Config.java b/src/main/java/cc/fascinated/bat/config/Config.java new file mode 100644 index 0000000..123e935 --- /dev/null +++ b/src/main/java/cc/fascinated/bat/config/Config.java @@ -0,0 +1,20 @@ +package cc.fascinated.bat.config; + +import lombok.Getter; + +/** + * @author Fascinated (fascinated7) + */ +public class Config { + /** + * Is the app running in a production environment? + */ + @Getter + private static final boolean production; + + static { + // Are we running on production? + String appEnv = System.getenv("APP_ENV"); + production = appEnv != null && (appEnv.equals("production")); + } +} diff --git a/src/main/java/cc/fascinated/bat/service/CommandService.java b/src/main/java/cc/fascinated/bat/service/CommandService.java index 1a4c030..d00e92d 100644 --- a/src/main/java/cc/fascinated/bat/service/CommandService.java +++ b/src/main/java/cc/fascinated/bat/service/CommandService.java @@ -3,6 +3,7 @@ package cc.fascinated.bat.service; import cc.fascinated.bat.Consts; import cc.fascinated.bat.command.*; import cc.fascinated.bat.common.EmbedUtils; +import cc.fascinated.bat.config.Config; import cc.fascinated.bat.model.BatGuild; import cc.fascinated.bat.model.BatUser; import lombok.Getter; @@ -73,6 +74,19 @@ public class CommandService extends ListenerAdapter { JDA jda = DiscordService.JDA; long before = System.currentTimeMillis(); + Guild adminGuild = jda.getGuildById(Consts.ADMIN_GUILD); + if (!Config.isProduction()) { + if (adminGuild == null) { + log.error("Unable to find the admin guild to register commands"); + return; + } + jda.retrieveCommands().complete().forEach(command -> jda.deleteCommandById(command.getId()).complete()); + adminGuild.updateCommands().addCommands(commands.values().stream() + .map(BatCommand::getCommandData).toList()).complete(); + log.info("Registered {} slash commands in {}ms (DEV MODE)", commands.size(), System.currentTimeMillis() - before); + return; + } + // Unregister all commands that Discord has but we don't jda.retrieveCommands().complete().forEach(command -> { if (commands.containsKey(command.getName()) @@ -102,8 +116,6 @@ public class CommandService extends ListenerAdapter { } } } - - Guild adminGuild = jda.getGuildById(Consts.ADMIN_GUILD); if (adminGuild != null) { adminGuild.updateCommands().addCommands(commands.values().stream() .filter(command -> command.getCategory().isHidden() || command.isBotOwnerOnly()) diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index a420698..4f64dc8 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -2,6 +2,9 @@ discord: token: "oh my goodnesssssssssss" +# Bat Configuration +environment: "development" + # Sentry Configuration sentry: dsn: "CHANGE_ME"