Hacky-Quizbot/src/main/java/linux/general/hackyquizbot/Main.java

89 lines
2.2 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.Date;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
String startupTime = String.format("%1$tY-%1$tm-%1$td %1$tI:%1$tM:%1$tS%1$tp UTC%1$tz", new Date());
// Insert your bot's token here
String token = args[0];
CheckDeploy check = new CheckDeploy(7500, "/var/www/deployment/Hacky-Quizbot/id.txt");
check.start();
DiscordApi api = new DiscordApiBuilder().setToken(token).login().join();
//Trivia management
Member member = new Member(api);
// Add a listener which answers with "Pong!" if someone writes "!ping"
api.addMessageCreateListener(event -> {
if (event.getMessageContent().equalsIgnoreCase("!ping")) {
event.getChannel().sendMessage("Pong!\nHacky the quiz bot has been running since "+startupTime);
}
});
// Print the invite url of your bot
System.out.println("You can invite the bot by using the following url: " + api.createBotInvite());
}
//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());
}
}
}
}
}