Use env vars instead of json

This commit is contained in:
Roscoe 2026-02-15 23:31:34 +00:00
commit 89585cae0e
Signed by: RoscoeDaWah
SSH key fingerprint: SHA256:Hqn452XQ1ETzUt/FthJu6+OFkS4NBxCv5VQSEvuk7CE
8 changed files with 28 additions and 38 deletions

2
.env Normal file
View file

@ -0,0 +1,2 @@
DOKI_TOKEN=OTM4NTIyNzgxMDM1OTIxNDQ4.G49Qim.wrCrGvb3YwTQEMMoJ0P3LpSCWcaeL2AAuOOsEI
DOKI_WARNING_CHANNEL=1320191969137393736

View file

@ -1,5 +1,8 @@
<component name="ProjectRunConfigurationManager"> <component name="ProjectRunConfigurationManager">
<configuration default="false" name="Doki" type="Application" factoryName="Application"> <configuration default="false" name="Doki" type="Application" factoryName="Application">
<option name="envFilePaths">
<option value="$PROJECT_DIR$/.env" />
</option>
<option name="MAIN_CLASS_NAME" value="net.hypr.doki.Doki" /> <option name="MAIN_CLASS_NAME" value="net.hypr.doki.Doki" />
<module name="doki" /> <module name="doki" />
<method v="2"> <method v="2">

View file

@ -18,13 +18,12 @@ A multipurpose Discord bot written in Java.
--- ---
## Config file schema ## Enviromnent Variables
```json
{ | Variable | Description |
"token": "your_token", |------------------------|----------------------------|
"warningChannel": 0 | `DOKI_TOKEN` | Discord token for the bot |
} | `DOKI_WARNING_CHANNEL` | Channel to log warnings to |
```
--- ---

View file

@ -1,4 +0,0 @@
{
"token": "CHANGEME",
"warningChannel": 0
}

View file

@ -6,7 +6,7 @@
<groupId>net.hypr</groupId> <groupId>net.hypr</groupId>
<artifactId>doki</artifactId> <artifactId>doki</artifactId>
<version>1.0.9</version> <version>1.1.0</version>
<properties> <properties>
<maven.compiler.source>17</maven.compiler.source> <maven.compiler.source>17</maven.compiler.source>

View file

@ -1,18 +1,20 @@
package net.hypr.doki; package net.hypr.doki;
import com.freya02.botcommands.api.Logging; import com.freya02.botcommands.api.Logging;
import com.google.gson.Gson;
import org.slf4j.Logger; import org.slf4j.Logger;
import java.io.*; import java.io.*;
import java.nio.charset.StandardCharsets;
//You can add more fields in this class, if your input json matches the structure //You can add more fields in this class, if your input json matches the structure
//You will need a valid config.json in the resources folder for this to work //You will need a valid config.json in the resources folder for this to work
public class Config { public class Config {
@SuppressWarnings("unused") private String token; @SuppressWarnings("unused") private final String token;
@SuppressWarnings("unused") private long warningChannel; @SuppressWarnings("unused") private final long warningChannel;
private Config(String token, long warningChannel) {
this.token = token;
this.warningChannel = warningChannel;
}
/** /**
* Returns the configuration object for this bot * Returns the configuration object for this bot
* *
@ -21,14 +23,10 @@ public class Config {
*/ */
public static Config readConfig() throws IOException { public static Config readConfig() throws IOException {
Logger log = Logging.getLogger(); Logger log = Logging.getLogger();
try (InputStream in = new FileInputStream("./config.json")) { return new Config(
Reader reader = new InputStreamReader(in, StandardCharsets.UTF_8); System.getenv("DOKI_TOKEN"),
log.info("Loaded config"); Long.parseLong(System.getenv("DOKI_WARNING_CHANNEL"))
return new Gson().fromJson(reader, Config.class); );
} catch (IOException | NullPointerException e) {
log.error("Failed to load config.json, does the file exist?");
throw new IOException(e);
}
} }
public String getToken() { public String getToken() {

View file

@ -38,8 +38,12 @@ public class Doki {
// Print some information about the bot // Print some information about the bot
log.info("Bot connected as {}", jda.getSelfUser().getAsTag()); log.info("Bot connected as {}", jda.getSelfUser().getAsTag());
log.info("The bot is present in the following guilds:"); log.info("The bot is present in the following guilds:");
for (Guild guild : jda.getGuildCache()) { try {
log.info("\t- {} ({})", guild.getName(), guild.getId()); for (Guild guild : jda.getGuildCache()) {
log.info("\t- {} ({})", guild.getName(), guild.getId());
}
} catch (Exception e) {
throw new RuntimeException(e);
} }
new Doki(jda, config); new Doki(jda, config);

View file

@ -1,12 +0,0 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"token": {
"type": "string"
}
},
"required": [
"token"
]
}