Skip to content

Commit ef028a4

Browse files
committed
Allow token to be read from given file
This enables the proper usage of Docker secrets for the bot.
1 parent b4a96de commit ef028a4

File tree

1 file changed

+32
-9
lines changed

1 file changed

+32
-9
lines changed

src/main/java/org/javacord/bot/Main.java

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22

33
import de.btobastian.sdcf4j.CommandHandler;
44
import de.btobastian.sdcf4j.handler.JavacordHandler;
5-
import org.apache.logging.log4j.LogManager;
6-
import org.apache.logging.log4j.Logger;
75
import org.javacord.api.DiscordApi;
86
import org.javacord.api.DiscordApiBuilder;
7+
import org.javacord.api.util.logging.ExceptionLogger;
98
import org.javacord.bot.commands.DocsCommand;
109
import org.javacord.bot.commands.ExampleCommand;
1110
import org.javacord.bot.commands.GitHubCommand;
@@ -17,19 +16,43 @@
1716
import org.javacord.bot.commands.SetupCommand;
1817
import org.javacord.bot.commands.WikiCommand;
1918

20-
public class Main {
19+
import java.io.IOException;
20+
import java.nio.file.Files;
21+
import java.nio.file.Path;
22+
import java.nio.file.Paths;
2123

22-
private static Logger logger = LogManager.getLogger(Main.class);
24+
public class Main {
2325

2426
/**
25-
* The entrance point of the bot.
27+
* The entry point of the bot.
2628
*
27-
* @param args The first argument should be the bot's token. Every other argument gets ignored.
29+
* @param args The bot requires exactly one argument, either a file with the token as content or the token directly.
30+
* If the argument is a relative file path, it is relative to the working directory.
31+
* @throws IOException If there is an error when reading the token file.
2832
*/
29-
public static void main(String[] args) {
33+
public static void main(String[] args) throws IOException {
34+
Thread.setDefaultUncaughtExceptionHandler(ExceptionLogger.getUncaughtExceptionHandler());
35+
36+
if (args.length != 1) {
37+
System.err.println("This bot requires exactly one argument, "
38+
+ "either a file with the token as content or the token directly.\n"
39+
+ "If the argument is a relative file path, it is relative to the working directory");
40+
System.exit(1);
41+
}
42+
43+
DiscordApiBuilder apiBuilder = new DiscordApiBuilder();
44+
45+
// Token
46+
Path tokenFile = Paths.get(args[0]);
47+
if (Files.isRegularFile(tokenFile)) {
48+
apiBuilder.setToken(Files.newBufferedReader(tokenFile).readLine());
49+
} else {
50+
apiBuilder.setToken(args[0]);
51+
}
52+
3053
// Login
31-
DiscordApi api = new DiscordApiBuilder()
32-
.setToken(args[0])
54+
DiscordApi api = apiBuilder
55+
.setWaitForServersOnStartup(false)
3356
.login().join();
3457

3558
// Register commands

0 commit comments

Comments
 (0)