106 lines
2.8 KiB
Java
106 lines
2.8 KiB
Java
package linux.general.hackyquizbot;
|
||
|
||
import org.javacord.api.DiscordApi;
|
||
import org.javacord.api.DiscordApiBuilder;
|
||
import java.io.File;
|
||
import java.io.FileNotFoundException;
|
||
import java.util.ArrayList;
|
||
import java.util.Date;
|
||
import java.util.Scanner;
|
||
|
||
public class Main {
|
||
public static final String startupTime = String.format("%1$tY-%1$tm-%1$td %1$tI:%1$tM:%1$tS%1$tp UTC%1$tz", new Date());
|
||
private static String[] commandLineArguments;
|
||
private static long userID;
|
||
|
||
|
||
public static void main(String[] args) {
|
||
// [0] token, [1] db username, [2] db password
|
||
commandLineArguments = args;
|
||
|
||
// checks every 7500 ms if a deployment happened, if so shuts down the bot
|
||
CheckDeploy check = new CheckDeploy(7500, "/var/www/deployment/Hacky-Quizbot/id.txt");
|
||
check.start();
|
||
|
||
//TODO: remove, this is just a way of force loading of the QuizBackend class while it isn’t being loaded automatically
|
||
QuizBackend backend = new QuizBackend();
|
||
|
||
DiscordApi api = new DiscordApiBuilder().setToken(commandLineArguments[0]).login().join();
|
||
userID = api.getYourself().getId();
|
||
System.err.println("Logging in as "+api.getYourself().getDiscriminatedName());
|
||
|
||
//user management
|
||
ArrayList<QuizHandler> QuizHandlers = new ArrayList<QuizHandler>();
|
||
|
||
// commands
|
||
api.addMessageCreateListener(event -> {
|
||
if (event.getMessageContent().equalsIgnoreCase("!ping")) {
|
||
event.getChannel().sendMessage("Pong!\nHacky the quiz bot has been running since "+startupTime);
|
||
}
|
||
if (event.getMessageContent().equalsIgnoreCase("!trivia")) {
|
||
QuizHandlers.add(new QuizHandler(event.getMessageAuthor().asUser().get()));
|
||
}
|
||
});
|
||
|
||
}
|
||
|
||
public static String getCommandLineArgument(int index) {
|
||
return commandLineArguments[index];
|
||
}
|
||
|
||
public static long getUserID() {
|
||
return userID;
|
||
}
|
||
|
||
//Deploy check thread
|
||
public static class CheckDeploy extends Thread {
|
||
private int interval;
|
||
private String id;
|
||
private String filename;
|
||
|
||
public CheckDeploy(int interval, String filename) {
|
||
this.interval = interval;
|
||
this.filename = filename;
|
||
}
|
||
|
||
public String grabId() throws FileNotFoundException {
|
||
File file = new File(this.filename);
|
||
Scanner reader;
|
||
String id = new String();
|
||
|
||
reader = new Scanner(file);
|
||
while (reader.hasNextLine()) {
|
||
id = reader.nextLine();
|
||
}
|
||
reader.close();
|
||
return id;
|
||
}
|
||
|
||
public void run() {
|
||
try {
|
||
this.id = this.grabId();
|
||
} catch (FileNotFoundException e) {
|
||
System.err.println("Failed to grab ID on startup.");
|
||
System.exit(1);
|
||
}
|
||
while (true) {
|
||
String new_id = this.id;
|
||
try {
|
||
new_id = this.grabId();
|
||
} catch (FileNotFoundException e) {
|
||
System.err.println("Failed to check ID.");
|
||
}
|
||
if (!new_id.equals(this.id)) {
|
||
System.exit(0);
|
||
}
|
||
|
||
try {
|
||
sleep(this.interval);
|
||
} catch (Exception e) {
|
||
System.err.println(e.getMessage());
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|