diff --git a/README.md b/README.md
index 41c062b..9aff4d0 100644
--- a/README.md
+++ b/README.md
@@ -1,8 +1,8 @@
# Net Neutrality Discord bot
-This is the source code for my custom discord bot. This is a personal project, so I offer no support on how to build it.
+This is the source code for my custom discord bot.
+This is a personal project, so I offer no support on how to build it.
-The configuration is contained in the file "bot.properties". There is an example file for all the variables, they can also be overwritten by environment variables.
The preferred method for running the bot is by using Docker Compose like this:
@@ -10,3 +10,6 @@ The preferred method for running the bot is by using Docker Compose like this:
docker compose build
docker compose up -d
```
+
+The environment variables needed for the bot to run are available in the file **bot.properties.example**
+If running locally without Docker, you need to copy the example file to **bot.properties** and fill the values.
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
index 85df176..bc81439 100644
--- a/pom.xml
+++ b/pom.xml
@@ -56,12 +56,6 @@
https://repo.maven.apache.org/maven2/
-
- dv8tion
- m2-dv8tion
- https://m2.dv8tion.net/releases
-
-
jitpack
https://jitpack.io
diff --git a/src/main/java/netneutrality/NetNeutrality.java b/src/main/java/netneutrality/NetNeutrality.java
index aaa9b7c..39c7e0d 100644
--- a/src/main/java/netneutrality/NetNeutrality.java
+++ b/src/main/java/netneutrality/NetNeutrality.java
@@ -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();
diff --git a/src/main/java/netneutrality/core/SessionManager.java b/src/main/java/netneutrality/core/SessionManager.java
index fd4743a..bb38ce1 100644
--- a/src/main/java/netneutrality/core/SessionManager.java
+++ b/src/main/java/netneutrality/core/SessionManager.java
@@ -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);
}
}
diff --git a/src/main/java/netneutrality/core/context/MessageCommandContext.java b/src/main/java/netneutrality/core/context/MessageCommandContext.java
index 2906077..3491a66 100644
--- a/src/main/java/netneutrality/core/context/MessageCommandContext.java
+++ b/src/main/java/netneutrality/core/context/MessageCommandContext.java
@@ -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());
}
diff --git a/src/main/java/netneutrality/utils/Settings.java b/src/main/java/netneutrality/utils/Settings.java
index 10f9bf9..c35ba2b 100644
--- a/src/main/java/netneutrality/utils/Settings.java
+++ b/src/main/java/netneutrality/utils/Settings.java
@@ -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 envs = System.getenv();
for (Map.Entry 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");
- }
-
}
diff --git a/src/test/java/netneutrality/utils/SettingsTest.java b/src/test/java/netneutrality/utils/SettingsTest.java
deleted file mode 100644
index 38fd259..0000000
--- a/src/test/java/netneutrality/utils/SettingsTest.java
+++ /dev/null
@@ -1,33 +0,0 @@
-package netneutrality.utils;
-
-/*
- * Copyright (c) anthorob
- *
- * https://discord.gg/qUa6zNFZsd
- */
-
-import org.junit.Before;
-import org.junit.Test;
-
-import static org.junit.Assert.assertNotNull;
-
-public class SettingsTest {
-
- private Settings instance;
-
- @Before
- public void setUp() {
- instance = Settings.getInstance();
- }
-
- @Test
- public void get() {
- assertNotNull(instance.get("BOT_TOKEN"));
- }
-
- @Test
- public void getCreator() {
- String creator = instance.getCreatorId();
- assertNotNull(creator);
- }
-}