change how properties file is loaded and rely on environment variables if it doesn't exist
This commit is contained in:
1 parent
0b1c326f40
commit
f51ae465bd
7 files changed
+40
-83
No files matched your search
@@ -1,8 +1,8 @@
|
|||||||
# Net Neutrality Discord bot
|
# 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:
|
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 build
|
||||||
docker compose up -d
|
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.
|
||||||
@@ -56,12 +56,6 @@
|
|||||||
<url>https://repo.maven.apache.org/maven2/</url>
|
<url>https://repo.maven.apache.org/maven2/</url>
|
||||||
</repository>
|
</repository>
|
||||||
|
|
||||||
<repository>
|
|
||||||
<id>dv8tion</id>
|
|
||||||
<name>m2-dv8tion</name>
|
|
||||||
<url>https://m2.dv8tion.net/releases</url>
|
|
||||||
</repository>
|
|
||||||
|
|
||||||
<repository>
|
<repository>
|
||||||
<id>jitpack</id>
|
<id>jitpack</id>
|
||||||
<url>https://jitpack.io</url>
|
<url>https://jitpack.io</url>
|
||||||
|
|||||||
@@ -39,8 +39,12 @@ public class NetNeutrality {
|
|||||||
public static final String VERSION = "2.0.0";
|
public static final String VERSION = "2.0.0";
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
String sentryDsn = Settings.getInstance().get("SENTRY_DSN");
|
|
||||||
String env = Settings.getInstance().get("BOT_ENV", "dev");
|
String env = Settings.getInstance().get("BOT_ENV", "dev");
|
||||||
|
|
||||||
|
String sentryDsn = Settings.getInstance().get("SENTRY_DSN");
|
||||||
|
if (sentryDsn != null) {
|
||||||
|
logger.info("Found Sentry DSN in config, setting up the SDK..");
|
||||||
|
|
||||||
Sentry.init(options -> {
|
Sentry.init(options -> {
|
||||||
options.setDsn(sentryDsn);
|
options.setDsn(sentryDsn);
|
||||||
options.setEnvironment(env);
|
options.setEnvironment(env);
|
||||||
@@ -48,6 +52,7 @@ public class NetNeutrality {
|
|||||||
options.setDebug(false);
|
options.setDebug(false);
|
||||||
options.setRelease(String.format("netneutrality@%s", VERSION));
|
options.setRelease(String.format("netneutrality@%s", VERSION));
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
if ("dev".equals(env)) {
|
if ("dev".equals(env)) {
|
||||||
Configurator.setLevel("net.dv8tion.jda", Level.DEBUG);
|
Configurator.setLevel("net.dv8tion.jda", Level.DEBUG);
|
||||||
@@ -67,9 +72,15 @@ public class NetNeutrality {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void initJDA() {
|
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 {
|
try {
|
||||||
jda = JDABuilder
|
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)
|
.enableIntents(GatewayIntent.GUILD_MEMBERS, GatewayIntent.GUILD_MESSAGE_REACTIONS, GatewayIntent.GUILD_VOICE_STATES, GatewayIntent.MESSAGE_CONTENT)
|
||||||
.setActivity(Activity.watching("me load..."))
|
.setActivity(Activity.watching("me load..."))
|
||||||
.build();
|
.build();
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ public class SessionManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
shutdown();
|
shutdown();
|
||||||
logger.error("Error connecting to database. Shutting down..", e);
|
logger.error("Error connecting to database. Shutting down..");
|
||||||
System.exit(1);
|
System.exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -184,7 +184,7 @@ public class MessageCommandContext implements CommandContext {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean isMessageFromCreator() {
|
public boolean isMessageFromCreator() {
|
||||||
String creatorId = Settings.getInstance().getCreatorId();
|
String creatorId = Settings.getInstance().get("BOT_OWNER");
|
||||||
|
|
||||||
return creatorId.equals(member.getId());
|
return creatorId.equals(member.getId());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ package netneutrality.utils;
|
|||||||
* https://discord.gg/qUa6zNFZsd
|
* https://discord.gg/qUa6zNFZsd
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import org.apache.commons.io.FileUtils;
|
import org.apache.logging.log4j.LogManager;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
@@ -18,48 +18,30 @@ public class Settings {
|
|||||||
|
|
||||||
private static Settings INSTANCE;
|
private static Settings INSTANCE;
|
||||||
private final Properties prop = new Properties();
|
private final Properties prop = new Properties();
|
||||||
|
public final String DATETIMEFORMAT = "yyyy-MM-dd HH:mm:ss";
|
||||||
|
|
||||||
public Settings() {
|
private Settings() {
|
||||||
File propFile = new File("bot.properties");
|
checkAndLoadPropertiesFile();
|
||||||
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);
|
|
||||||
prop.load(fis);
|
|
||||||
|
|
||||||
loadEnvironmentVariables();
|
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) { }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void checkAndLoadPropertiesFile() {
|
||||||
|
File propFile = new File("bot.properties");
|
||||||
|
|
||||||
|
try (FileInputStream fis = new FileInputStream(propFile)) {
|
||||||
|
prop.load(fis);
|
||||||
|
} catch (IOException e) {
|
||||||
|
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() {
|
private void loadEnvironmentVariables() {
|
||||||
Map<String, String> envs = System.getenv();
|
Map<String, String> envs = System.getenv();
|
||||||
|
|
||||||
for (Map.Entry<String, String> env : envs.entrySet()) {
|
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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public static Settings getInstance() {
|
public static Settings getInstance() {
|
||||||
if (INSTANCE == null) {
|
if (INSTANCE == null) {
|
||||||
@@ -70,11 +52,17 @@ public class Settings {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public String get(String key) {
|
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) {
|
public String get(String key, String def) {
|
||||||
String property = prop.getProperty(key);
|
String property = get(key);
|
||||||
|
|
||||||
if (property == null || property.isEmpty())
|
if (property == null || property.isEmpty())
|
||||||
return def;
|
return def;
|
||||||
@@ -82,10 +70,4 @@ public class Settings {
|
|||||||
return property;
|
return property;
|
||||||
}
|
}
|
||||||
|
|
||||||
public final String DATETIMEFORMAT = "yyyy-MM-dd HH:mm:ss";
|
|
||||||
|
|
||||||
public String getCreatorId() {
|
|
||||||
return get("BOT_OWNER");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in new issue
Block a user