Improve bot service

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

@ -153,28 +153,34 @@ public final class DiscordService {
return; return;
} }
// Initialize the bot // Initialize the bot
long before = System.currentTimeMillis(); new Thread(() -> {
log.info("Logging in..."); // Log that we're logging in long before = System.currentTimeMillis();
jda = JDABuilder.createDefault(token) log.info("Logging in..."); // Log that we're logging in
.enableIntents( jda = JDABuilder.createDefault(token)
GatewayIntent.GUILD_MEMBERS .enableIntents(
).setStatus(OnlineStatus.DO_NOT_DISTURB) GatewayIntent.GUILD_MEMBERS
.setActivity(Activity.watching("your licenses")) ).setStatus(OnlineStatus.DO_NOT_DISTURB)
.addEventListeners(new EventHandler()) .setActivity(Activity.watching("your licenses"))
.build(); .addEventListeners(new EventHandler())
jda.awaitReady(); // Await JDA to be ready .build();
try {
// Log that we're logged in jda.awaitReady(); // Await JDA to be ready
log.info("Logged into {} in {}ms",
jda.getSelfUser().getEffectiveName(), System.currentTimeMillis() - before // Log that we're logged in
); log.info("Logged into {} in {}ms",
jda.getSelfUser().getEffectiveName(), System.currentTimeMillis() - before
// Registering slash commands );
jda.updateCommands().addCommands(
Commands.slash("license", "Manage one of your licenses") // Registering slash commands
.addOption(OptionType.STRING, "key", "The license key", true) jda.updateCommands().addCommands(
.addOption(OptionType.STRING, "product", "The product the license is for", true) Commands.slash("license", "Manage one of your licenses")
).queue(); .addOption(OptionType.STRING, "key", "The license key", true)
.addOption(OptionType.STRING, "product", "The product the license is for", true)
).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