Compare commits

...

5 Commits

Author SHA1 Message Date
Shwoomple 92f66779c1 started work on Member class 2022-02-20 23:11:38 +05:30
BodgeMaster 22e0139466 fixed a minor bug where the bot would exit immediately when deploying instead of waiting for the build to finish 2022-02-20 08:53:04 +01:00
BodgeMaster 4b075d63ed poll slower 2022-02-20 08:45:55 +01:00
BodgeMaster ce026ac1d3 hard-code file path for deployment check 2022-02-20 08:41:52 +01:00
BodgeMaster 7ed5d26a75 fix a potential hang before it occurs 2022-02-20 08:40:57 +01:00
3 changed files with 96 additions and 67 deletions

View File

@ -6,7 +6,7 @@ $(date "+%Y-%m-%d %I:%M:%S%p UTC%:z")
# build the thing into a big jar with all the dependencies # build the thing into a big jar with all the dependencies
mvn clean compile assembly:single mvn clean compile assembly:single
cat /dev/urandom | head -c 10 | base64 > id.txt dd if=/dev/urandom bs=1 count=20 | base64 > id.txt
# TODO: Somehow tell a service that manages the bot to shut it down and replace it with the updated version? # TODO: Somehow tell a service that manages the bot to shut it down and replace it with the updated version?
echo "================================================================================ echo "================================================================================

View File

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

View File

@ -0,0 +1,23 @@
package linux.general.hackyquizbot;
import org.javacord.api.DiscordApi;
import org.javacord.api.entity.user.User;
public class Member {
private DiscordApi api;
private User user;
public Member(DiscordApi api) {
this.api = api;
this.api.addMessageCreateListener(event ->{
if(event.getMessageContent().equalsIgnoreCase("!trivia")) {
if(event.getMessageAuthor().asUser().isPresent()) {
this.user = event.getMessageAuthor().asUser().get();
this.user.sendMessage("Welcome to hell");
}
}
});
}
}