Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Switch to a JSON configuration file #56

Merged
merged 4 commits into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 0 additions & 16 deletions .env.example

This file was deleted.

2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ build
bin
.vscode
.idea
.env
config.json

.classpath
.project
Expand Down
1 change: 0 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ dependencies {
testImplementation 'org.springframework.boot:spring-boot-starter-test'

implementation 'com.konghq:unirest-java:3.14.5'
implementation 'io.github.cdimascio:dotenv-java:3.0.0'
implementation 'org.apache.logging.log4j:log4j-api:2.20.0'
implementation 'org.apache.logging.log4j:log4j-core:2.20.0'
implementation "com.deepl.api:deepl-java:1.3.0"
Expand Down
15 changes: 15 additions & 0 deletions app/config-example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"BOT_TOKEN": "<Insert Discord bot's token here>",

"KEY_DEEPL": "<Insert DeepL Translation token here>",
"KEY_RAPID_API": "<Insert Rapid API key here>",

"ROLE_ID_VOICE_CHAT": "<@Voice Chat role ID>",
"ROLE_ID_TOTD": "<@TOTD role ID>",
"ROLE_ID_WOTD": "<@WOTD role ID>",
"CHANNEL_ID_CHAT": "#chat text channel ID",
"CHANNEL_ID_WOTD": "#wotd text channel ID",

"MONGODB_URI": "mongodb://<host>:<port>",
"MONGODB_DATABASE": "learn_english"
}
34 changes: 14 additions & 20 deletions app/src/main/java/bot/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
import bot.service.UserService;
import bot.task.StreakResetTask;
import com.deepl.api.Translator;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.mongodb.BasicDBObject;
import io.github.cdimascio.dotenv.Dotenv;
import it.sauronsoftware.cron4j.Scheduler;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
Expand Down Expand Up @@ -40,7 +43,7 @@ public class App implements ApplicationRunner {

public static final Scheduler scheduler = new Scheduler();

private static Dotenv dotenv;
private static JsonObject config;
private static Translator translator;

public static List<EventListener> listeners = new ArrayList<>();
Expand Down Expand Up @@ -79,17 +82,7 @@ public void launch() {
scheduler.start();

try {
String envVar = App.getenv("PROD_BUILD");
int isProdBuild;

if (envVar != null) {
isProdBuild = Integer.parseInt(envVar);
} else {
isProdBuild = 0;
}

String botToken = isProdBuild == 1 ? "BOT_TOKEN_PROD" : "BOT_TOKEN_DEV";
JDABuilder jdaBuilder = JDABuilder.createDefault(App.getenv(botToken));
JDABuilder jdaBuilder = JDABuilder.createDefault(App.getenv("BOT_TOKEN"));

jdaBuilder.enableIntents(GatewayIntent.GUILD_VOICE_STATES);
commands = SpringContext.getBeansOfType(BotCommand.class);
Expand Down Expand Up @@ -168,20 +161,21 @@ public static Translator getTranslator() {
}

public static String getenv(String key) {
if (dotenv == null) {
if (config == null) {
try {
dotenv = Dotenv.configure().directory("..").load();
config = JsonParser.parseReader(new FileReader("config.json")).getAsJsonObject();
} catch (Exception e) {
dotenv = null;
config = null;
}
}

// Prioritize .env first
String value = null;
// Prioritize config first
JsonElement value = null;

if (dotenv != null) value = dotenv.get(key);
System.out.println(key);
if (config != null) value = config.get(key);

return value != null ? value : System.getenv(key);
return value != null ? value.getAsString() : System.getenv(key);
}

public static void main(String[] args) {
Expand Down