Improve bot service

This commit is contained in:
Braydon 2023-06-14 20:10:04 -04:00
parent 2e1e2f5127
commit 57b5c4e05d

@ -153,6 +153,7 @@ public final class DiscordService {
return; return;
} }
// Initialize the bot // Initialize the bot
new Thread(() -> {
long before = System.currentTimeMillis(); long before = System.currentTimeMillis();
log.info("Logging in..."); // Log that we're logging in log.info("Logging in..."); // Log that we're logging in
jda = JDABuilder.createDefault(token) jda = JDABuilder.createDefault(token)
@ -162,6 +163,7 @@ public final class DiscordService {
.setActivity(Activity.watching("your licenses")) .setActivity(Activity.watching("your licenses"))
.addEventListeners(new EventHandler()) .addEventListeners(new EventHandler())
.build(); .build();
try {
jda.awaitReady(); // Await JDA to be ready jda.awaitReady(); // Await JDA to be ready
// Log that we're logged in // Log that we're logged in
@ -175,6 +177,10 @@ public final class DiscordService {
.addOption(OptionType.STRING, "key", "The license key", true) .addOption(OptionType.STRING, "key", "The license key", true)
.addOption(OptionType.STRING, "product", "The product the license is for", true) .addOption(OptionType.STRING, "product", "The product the license is for", true)
).queue(); ).queue();
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}, "Discord Bot Thread").start();
} }
/** /**
@ -269,6 +275,10 @@ public final class DiscordService {
public class EventHandler extends ListenerAdapter { public class EventHandler extends ListenerAdapter {
@Override @Override
public void onSlashCommandInteraction(@NonNull SlashCommandInteractionEvent event) { public void onSlashCommandInteraction(@NonNull SlashCommandInteractionEvent event) {
// Bot isn't ready, don't handle events
if (!isReady()) {
return;
}
User user = event.getUser(); // The command executor User user = event.getUser(); // The command executor
// Handle the license command // Handle the license command
@ -339,6 +349,10 @@ public final class DiscordService {
@Override @Override
public void onButtonInteraction(@NonNull ButtonInteractionEvent event) { public void onButtonInteraction(@NonNull ButtonInteractionEvent event) {
// Bot isn't ready, don't handle events
if (!isReady()) {
return;
}
User user = event.getUser(); // The user who clicked the button User user = event.getUser(); // The user who clicked the button
String componentId = event.getComponentId(); // The button id String componentId = event.getComponentId(); // The button id