fix shutdown db saving (fr this time) and fixed embed color parsing

This commit is contained in:
Lee 2024-07-04 02:47:36 +01:00
parent 96486bb3a1
commit 040846ef0a
4 changed files with 61 additions and 8 deletions

View File

@ -12,6 +12,7 @@ import lombok.SneakyThrows;
import lombok.extern.log4j.Log4j2;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.scheduling.annotation.EnableScheduling;
import java.io.File;
@ -39,15 +40,20 @@ public class BatApplication {
}
log.info("Found configuration at '{}'", config.getAbsolutePath()); // Log the found config
// Start the application
SpringApplication app = new SpringApplication(BatApplication.class);
app.setRegisterShutdownHook(false); // Disable the default shutdown hook
ConfigurableApplicationContext context = app.run(args);
// Register the shutdown hook
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
log.info("Shutting down...");
for (EventListener listener : EventService.LISTENERS) {
listener.onShutdown();
}
context.close();
}));
// Start the app
SpringApplication.run(BatApplication.class, args);
log.info("APP IS RUNNING IN %s MODE!!!!!!!!!".formatted(Config.isProduction() ? "PRODUCTION" : "DEVELOPMENT"));
}
}

View File

@ -0,0 +1,45 @@
package cc.fascinated.bat.common;
import java.awt.*;
/**
* @author Fascinated (fascinated7)
*/
public class HexColorUtils {
/**
* Checks if the given string is a hex color
*
* @param hexColor the hex color to check
* @return if the given string is a hex color
*/
public static boolean isHexColor(String hexColor) {
return hexColor.matches("^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$");
}
/**
* Converts a hex color to a Color object
*
* @param hex the hex color to convert
* @return the Color object
*/
public static Color hexToColor(String hex) {
if (hex == null || (!hex.matches("^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$"))) {
throw new IllegalArgumentException("Invalid hex color code");
}
// Remove the '#' character
hex = hex.substring(1);
// If the hex code is 3 characters long, expand it to 6 characters
if (hex.length() == 3) {
char r = hex.charAt(0);
char g = hex.charAt(1);
char b = hex.charAt(2);
hex = "" + r + r + g + g + b + b;
}
// Convert the hex string to an integer and create a Color object
int rgb = Integer.parseInt(hex, 16);
return new Color(rgb);
}
}

View File

@ -1,5 +1,6 @@
package cc.fascinated.bat.features.welcomer;
import cc.fascinated.bat.common.HexColorUtils;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NonNull;
@ -44,7 +45,7 @@ public class WelcomerEmbed {
embedBuilder.setTitle(WelcomerPlaceholders.replaceAllPlaceholders(title, replacements));
}
embedBuilder.setDescription(WelcomerPlaceholders.replaceAllPlaceholders(description, replacements));
embedBuilder.setColor(Integer.parseInt(color, 16));
embedBuilder.setColor(HexColorUtils.hexToColor(color));
return embedBuilder;
}
}

View File

@ -5,6 +5,7 @@ import cc.fascinated.bat.command.BatSubCommand;
import cc.fascinated.bat.command.CommandInfo;
import cc.fascinated.bat.common.EmbedDescriptionBuilder;
import cc.fascinated.bat.common.EmbedUtils;
import cc.fascinated.bat.common.HexColorUtils;
import cc.fascinated.bat.features.welcomer.WelcomerProfile;
import cc.fascinated.bat.model.BatGuild;
import cc.fascinated.bat.model.BatUser;
@ -46,13 +47,13 @@ public class EmbedSubCommand extends BatSubCommand {
String color = colorOption.getAsString();
boolean pingBeforeSend = pingBeforeSendOption.getAsBoolean();
// Remove # if the user added it
color = color.replace("#", "");
// Add the # if it's not there
color = !color.startsWith("#") ? "#" + color : color;
// Validate the input
if (color.length() != 6 || Color.decode("#" + color).getRGB() == -1){
if (!HexColorUtils.isHexColor(color)){
event.replyEmbeds(EmbedUtils.errorEmbed()
.setDescription("The color must be a valid hex color code\n" +
.setDescription("The color must be a valid hex color code Example: `#3498DB`\n" +
"You can use this website to get a hex color code: https://htmlcolorcodes.com")
.build()).queue();
return;
@ -84,7 +85,7 @@ public class EmbedSubCommand extends BatSubCommand {
successDescription.appendLine("Title: `%s`".formatted(title), true);
}
successDescription.appendLine("Description: `%s`".formatted(description), true);
successDescription.appendLine("Color: `#%s`".formatted(color), true);
successDescription.appendLine("Color: `%s`".formatted(color), true);
successDescription.appendLine("Ping Before Send: %s".formatted(pingBeforeSend ? Emojis.CHECK_MARK_EMOJI.getFormatted() + " *(Preview won't ping you)*" : Emojis.CROSS_MARK_EMOJI), true);
successDescription.emptyLine();
successDescription.appendLine("**Preview Below:**", false);