fixed a minor bug where the bot would exit immediately when deploying instead of waiting for the build to finish

milan
BodgeMaster 2022-02-20 08:53:04 +01:00
parent 4b075d63ed
commit 22e0139466
1 changed files with 69 additions and 66 deletions

View File

@ -6,73 +6,76 @@ import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.util.Scanner; import java.util.Scanner;
public class Main{ public class Main {
public static void main(String[] args) { public static void main(String[] args) {
// Insert your bot's token here // Insert your bot's token here
String token = args[0]; String token = args[0];
CheckDeploy check = new CheckDeploy(7500, "/var/www/deployment/Hacky-Quizbot/id.txt"); CheckDeploy check = new CheckDeploy(7500, "/var/www/deployment/Hacky-Quizbot/id.txt");
check.start(); check.start();
DiscordApi api = new DiscordApiBuilder().setToken(token).login().join();
DiscordApi api = new DiscordApiBuilder().setToken(token).login().join(); // Add a listener which answers with "Pong!" if someone writes "!ping"
api.addMessageCreateListener(event -> {
if (event.getMessageContent().equalsIgnoreCase("!ping")) {
event.getChannel().sendMessage("Pong!");
}
});
// Add a listener which answers with "Pong!" if someone writes "!ping" // Print the invite url of your bot
api.addMessageCreateListener(event -> { System.out.println("You can invite the bot by using the following url: " + api.createBotInvite());
if (event.getMessageContent().equalsIgnoreCase("!ping")) {
event.getChannel().sendMessage("Pong!");
}
});
// Print the invite url of your bot }
System.out.println("You can invite the bot by using the following url: " + api.createBotInvite());
} public static class CheckDeploy extends Thread {
private int interval;
private String id;
private String filename;
public static class CheckDeploy extends Thread{ public CheckDeploy(int interval, String filename) {
private int interval; this.interval = interval;
private String id; this.filename = filename;
private String filename; }
public CheckDeploy(int interval, String filename) { public String grabId() throws FileNotFoundException {
this.interval = interval; File file = new File(this.filename);
this.filename = filename; Scanner reader;
} String id = new String();
public String grabId() { reader = new Scanner(file);
File file = new File(this.filename); while (reader.hasNextLine()) {
Scanner reader; id = reader.nextLine();
String id = new String(); }
reader.close();
return id;
}
try { public void run() {
reader = new Scanner(file); try {
while(reader.hasNextLine()) { this.id = this.grabId();
id = reader.nextLine(); } catch (FileNotFoundException e) {
} System.err.println("Failed to grab ID on startup.");
reader.close(); System.exit(1);
}catch (FileNotFoundException e) { }
System.err.println("File not found"); 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);
}
return id; try {
} sleep(this.interval);
} catch (Exception e) {
public void run() { System.err.println(e.getMessage());
this.id = this.grabId(); //replace with correct filename }
while(true) { }
String new_id = this.grabId(); }
}
if(!new_id.equals(this.id)) {
System.exit(0);
}
try {
sleep(this.interval);
}catch (Exception e) {
System.err.println(e.getMessage());
}
}
}
}
} }