change how properties file is loaded and rely on environment variables if it doesn't exist

This commit is contained in:
anthorob committed 2024-07-10 20:28:28 -04:00
1 parent 0b1c326f40
commit f51ae465bd
7 files changed
+46 -89

No files matched your search

+20 -9
View File
@@ -39,15 +39,20 @@ public class NetNeutrality {
public static final String VERSION = "2.0.0";
public static void main(String[] args) {
String sentryDsn = Settings.getInstance().get("SENTRY_DSN");
String env = Settings.getInstance().get("BOT_ENV", "dev");
Sentry.init(options -> {
options.setDsn(sentryDsn);
options.setEnvironment(env);
options.setTracesSampleRate(1.0);
options.setDebug(false);
options.setRelease(String.format("netneutrality@%s", VERSION));
});
String sentryDsn = Settings.getInstance().get("SENTRY_DSN");
if (sentryDsn != null) {
logger.info("Found Sentry DSN in config, setting up the SDK..");
Sentry.init(options -> {
options.setDsn(sentryDsn);
options.setEnvironment(env);
options.setTracesSampleRate(1.0);
options.setDebug(false);
options.setRelease(String.format("netneutrality@%s", VERSION));
});
}
if ("dev".equals(env)) {
Configurator.setLevel("net.dv8tion.jda", Level.DEBUG);
@@ -67,9 +72,15 @@ public class NetNeutrality {
}
public static void initJDA() {
String token = Settings.getInstance().get("BOT_TOKEN");
if (token == null) {
DBController.shutdown();
throw new RuntimeException("No bot token provided, exiting.");
}
try {
jda = JDABuilder
.createDefault(Settings.getInstance().get("BOT_TOKEN"))
.createDefault(token)
.enableIntents(GatewayIntent.GUILD_MEMBERS, GatewayIntent.GUILD_MESSAGE_REACTIONS, GatewayIntent.GUILD_VOICE_STATES, GatewayIntent.MESSAGE_CONTENT)
.setActivity(Activity.watching("me load..."))
.build();
@@ -52,7 +52,7 @@ public class SessionManager {
}
shutdown();
logger.error("Error connecting to database. Shutting down..", e);
logger.error("Error connecting to database. Shutting down..");
System.exit(1);
}
}
@@ -184,7 +184,7 @@ public class MessageCommandContext implements CommandContext {
}
public boolean isMessageFromCreator() {
String creatorId = Settings.getInstance().getCreatorId();
String creatorId = Settings.getInstance().get("BOT_OWNER");
return creatorId.equals(member.getId());
}
+19 -37
View File
@@ -6,7 +6,7 @@ package netneutrality.utils;
* https://discord.gg/qUa6zNFZsd
*/
import org.apache.commons.io.FileUtils;
import org.apache.logging.log4j.LogManager;
import java.io.File;
import java.io.FileInputStream;
@@ -18,46 +18,28 @@ public class Settings {
private static Settings INSTANCE;
private final Properties prop = new Properties();
public final String DATETIMEFORMAT = "yyyy-MM-dd HH:mm:ss";
public Settings() {
private Settings() {
checkAndLoadPropertiesFile();
loadEnvironmentVariables();
}
private void checkAndLoadPropertiesFile() {
File propFile = new File("bot.properties");
File exampleFile = new File("bot.properties.example");
FileInputStream fis = null;
try {
if (!propFile.exists() && !exampleFile.exists()) {
System.err.println("Missing example config file in root directory.");
System.exit(1);
}
if (!propFile.exists()) {
FileUtils.copyFile(exampleFile, propFile);
System.err.println("Config file was created, please edit bot.properties before launching bot");
}
fis = new FileInputStream(propFile);
try (FileInputStream fis = new FileInputStream(propFile)) {
prop.load(fis);
loadEnvironmentVariables();
} catch (IOException e) {
System.err.println("Can't find config file or config file is empty: bot.properties");
System.exit(1);
} finally {
try {
if (fis != null)
fis.close();
} catch (IOException ignored) { }
LogManager.getLogger(Settings.class).info("Can't load settings from bot.properties, bot is most likely running in docker. Using environment variables instead.");
}
}
private void loadEnvironmentVariables() {
Map<String, String> envs = System.getenv();
for (Map.Entry<String, String> env : envs.entrySet()) {
if (prop.containsKey(env.getKey())) {
prop.setProperty(env.getKey(), env.getValue());
}
prop.setProperty(env.getKey(), env.getValue());
}
}
@@ -70,11 +52,17 @@ public class Settings {
}
public String get(String key) {
return prop.getProperty(key);
String property = prop.getProperty(key);
if (property == null) {
LogManager.getLogger(Settings.class).error("Can't find property with key {}", key);
}
return property;
}
public String get(String key, String def) {
String property = prop.getProperty(key);
String property = get(key);
if (property == null || property.isEmpty())
return def;
@@ -82,10 +70,4 @@ public class Settings {
return property;
}
public final String DATETIMEFORMAT = "yyyy-MM-dd HH:mm:ss";
public String getCreatorId() {
return get("BOT_OWNER");
}
}