Use env vars instead of json
This commit is contained in:
parent
57ae8a5e94
commit
89585cae0e
8 changed files with 28 additions and 38 deletions
2
.env
Normal file
2
.env
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
DOKI_TOKEN=OTM4NTIyNzgxMDM1OTIxNDQ4.G49Qim.wrCrGvb3YwTQEMMoJ0P3LpSCWcaeL2AAuOOsEI
|
||||
DOKI_WARNING_CHANNEL=1320191969137393736
|
||||
3
.idea/runConfigurations/Doki.xml
generated
3
.idea/runConfigurations/Doki.xml
generated
|
|
@ -1,5 +1,8 @@
|
|||
<component name="ProjectRunConfigurationManager">
|
||||
<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" />
|
||||
<module name="doki" />
|
||||
<method v="2">
|
||||
|
|
|
|||
13
README.md
13
README.md
|
|
@ -18,13 +18,12 @@ A multipurpose Discord bot written in Java.
|
|||
|
||||
---
|
||||
|
||||
## Config file schema
|
||||
```json
|
||||
{
|
||||
"token": "your_token",
|
||||
"warningChannel": 0
|
||||
}
|
||||
```
|
||||
## Enviromnent Variables
|
||||
|
||||
| Variable | Description |
|
||||
|------------------------|----------------------------|
|
||||
| `DOKI_TOKEN` | Discord token for the bot |
|
||||
| `DOKI_WARNING_CHANNEL` | Channel to log warnings to |
|
||||
|
||||
---
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +0,0 @@
|
|||
{
|
||||
"token": "CHANGEME",
|
||||
"warningChannel": 0
|
||||
}
|
||||
2
pom.xml
2
pom.xml
|
|
@ -6,7 +6,7 @@
|
|||
|
||||
<groupId>net.hypr</groupId>
|
||||
<artifactId>doki</artifactId>
|
||||
<version>1.0.9</version>
|
||||
<version>1.1.0</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
|
|
|
|||
|
|
@ -1,18 +1,20 @@
|
|||
package net.hypr.doki;
|
||||
|
||||
import com.freya02.botcommands.api.Logging;
|
||||
import com.google.gson.Gson;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
//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
|
||||
public class Config {
|
||||
@SuppressWarnings("unused") private String token;
|
||||
@SuppressWarnings("unused") private long warningChannel;
|
||||
@SuppressWarnings("unused") private final String token;
|
||||
@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
|
||||
*
|
||||
|
|
@ -21,14 +23,10 @@ public class Config {
|
|||
*/
|
||||
public static Config readConfig() throws IOException {
|
||||
Logger log = Logging.getLogger();
|
||||
try (InputStream in = new FileInputStream("./config.json")) {
|
||||
Reader reader = new InputStreamReader(in, StandardCharsets.UTF_8);
|
||||
log.info("Loaded config");
|
||||
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);
|
||||
}
|
||||
return new Config(
|
||||
System.getenv("DOKI_TOKEN"),
|
||||
Long.parseLong(System.getenv("DOKI_WARNING_CHANNEL"))
|
||||
);
|
||||
}
|
||||
|
||||
public String getToken() {
|
||||
|
|
|
|||
|
|
@ -38,8 +38,12 @@ public class Doki {
|
|||
// Print some information about the bot
|
||||
log.info("Bot connected as {}", jda.getSelfUser().getAsTag());
|
||||
log.info("The bot is present in the following guilds:");
|
||||
for (Guild guild : jda.getGuildCache()) {
|
||||
log.info("\t- {} ({})", guild.getName(), guild.getId());
|
||||
try {
|
||||
for (Guild guild : jda.getGuildCache()) {
|
||||
log.info("\t- {} ({})", guild.getName(), guild.getId());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
new Doki(jda, config);
|
||||
|
|
|
|||
|
|
@ -1,12 +0,0 @@
|
|||
{
|
||||
"$schema": "http://json-schema.org/draft-04/schema#",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"token": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"token"
|
||||
]
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue