From 23fec44dc8a62c1e27838e6e579b6db8301ee0e5 Mon Sep 17 00:00:00 2001 From: HypherionMC Date: Wed, 22 Nov 2023 20:43:01 +0200 Subject: [PATCH] [CHANGE] Use deferred replies for slash commands --- .../commands/slash/general/HelpSlashCommand.java | 3 ++- .../slash/general/PlayerListSlashCommand.java | 8 +++++--- .../slash/general/ServerStatusSlashCommand.java | 3 ++- .../commands/slash/setup/SetChannelCommand.java | 6 ++++-- .../slash/verification/StaffUnverifyCommand.java | 8 +++++--- .../slash/verification/StaffVerifyAccountCommand.java | 6 ++++-- .../verification/UnverifyAccountSlashCommand.java | 8 +++++--- .../slash/verification/VerifyAccountCommand.java | 11 +++++++---- .../slash/verification/ViewVerifiedAccounts.java | 6 ++++-- .../core/discord/events/DiscordEventHandler.java | 3 +-- 10 files changed, 39 insertions(+), 23 deletions(-) diff --git a/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/general/HelpSlashCommand.java b/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/general/HelpSlashCommand.java index e3a93ad..9a5cfeb 100644 --- a/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/general/HelpSlashCommand.java +++ b/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/general/HelpSlashCommand.java @@ -27,6 +27,7 @@ public class HelpSlashCommand extends SDLinkSlashCommand { @Override protected void execute(SlashCommandEvent event) { + event.deferReply(true).queue(); Set commands = CommandManager.INSTANCE.getCommands(); EmbedBuilder builder = new EmbedBuilder(); @@ -34,6 +35,6 @@ public class HelpSlashCommand extends SDLinkSlashCommand { builder.setColor(Color.BLUE); commands.forEach(cmd -> builder.addField(cmd.getName(), cmd.getHelp(), false)); - event.replyEmbeds(builder.build()).setEphemeral(true).queue(); + event.getHook().sendMessageEmbeds(builder.build()).setEphemeral(true).queue(); } } diff --git a/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/general/PlayerListSlashCommand.java b/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/general/PlayerListSlashCommand.java index 5391c77..dba04a8 100644 --- a/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/general/PlayerListSlashCommand.java +++ b/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/general/PlayerListSlashCommand.java @@ -35,6 +35,8 @@ public class PlayerListSlashCommand extends SDLinkSlashCommand { @Override protected void execute(SlashCommandEvent event) { + event.deferReply(true).queue(); + try { List players = SDLinkPlatform.minecraftHelper.getOnlinePlayers(); @@ -46,7 +48,7 @@ public class PlayerListSlashCommand extends SDLinkSlashCommand { builder.setTitle("Online Players"); builder.setColor(Color.RED); builder.setDescription("There are currently no players online"); - event.replyEmbeds(builder.build()).setEphemeral(true).queue(); + event.getHook().sendMessageEmbeds(builder.build()).setEphemeral(true).queue(); return; } @@ -78,9 +80,9 @@ public class PlayerListSlashCommand extends SDLinkSlashCommand { paginator.setItems(pages); ButtonEmbedPaginator embedPaginator = paginator.build(); - event.replyEmbeds(pages.get(0)).setEphemeral(false).queue(success -> - success.retrieveOriginal().queue(msg -> embedPaginator.paginate(msg, 1))); + event.getHook().sendMessageEmbeds(pages.get(0)).setEphemeral(false).queue(success -> embedPaginator.paginate(success, 1)); } catch (Exception e) { + event.getHook().sendMessage("Failed to execute command. Please see your server log").setEphemeral(true).queue(); if (SDLinkConfig.INSTANCE.generalConfig.debugging) e.printStackTrace(); } diff --git a/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/general/ServerStatusSlashCommand.java b/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/general/ServerStatusSlashCommand.java index 6530772..7328f90 100644 --- a/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/general/ServerStatusSlashCommand.java +++ b/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/general/ServerStatusSlashCommand.java @@ -32,8 +32,9 @@ public class ServerStatusSlashCommand extends SDLinkSlashCommand { @Override protected void execute(SlashCommandEvent event) { + event.deferReply(true).queue(); Button refreshBtn = Button.danger("sdrefreshbtn", "Refresh"); - event.replyEmbeds(runStatusCommand()).addActionRow(refreshBtn).queue(); + event.getHook().sendMessageEmbeds(runStatusCommand()).addActionRow(refreshBtn).queue(); } public static MessageEmbed runStatusCommand() { diff --git a/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/setup/SetChannelCommand.java b/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/setup/SetChannelCommand.java index 47ba7fb..9b4fd66 100644 --- a/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/setup/SetChannelCommand.java +++ b/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/setup/SetChannelCommand.java @@ -41,12 +41,14 @@ public class SetChannelCommand extends SDLinkSlashCommand { @Override protected void execute(SlashCommandEvent event) { + event.deferReply(true).queue(); + StandardGuildMessageChannel channel = event.getOption("channel").getAsChannel().asStandardGuildMessageChannel(); String type = event.getOption("type").getAsString(); boolean webhook = event.getOption("webhook").getAsBoolean(); if (!channel.canTalk()) { - event.reply("I do not have permission to send messages in " + channel.getAsMention()).setEphemeral(true).queue(); + event.getHook().sendMessage("I do not have permission to send messages in " + channel.getAsMention()).setEphemeral(true).queue(); return; } @@ -58,7 +60,7 @@ public class SetChannelCommand extends SDLinkSlashCommand { result = setChannel(channel, type); } - event.reply(result.getMessage()).setEphemeral(true).queue(); + event.getHook().sendMessage(result.getMessage()).setEphemeral(true).queue(); } private Result setChannel(StandardGuildMessageChannel channel, String type) { diff --git a/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/verification/StaffUnverifyCommand.java b/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/verification/StaffUnverifyCommand.java index 8deb315..f934c1b 100644 --- a/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/verification/StaffUnverifyCommand.java +++ b/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/verification/StaffUnverifyCommand.java @@ -34,11 +34,13 @@ public class StaffUnverifyCommand extends SDLinkSlashCommand { @Override protected void execute(SlashCommandEvent event) { + event.deferReply(true).queue(); + sdlinkDatabase.reloadCollection("verifiedaccounts"); List accounts = sdlinkDatabase.findAll(SDLinkAccount.class); if (accounts.isEmpty()) { - event.reply("Sorry, but this server does not contain any stored players in its database").setEphemeral(true).queue(); + event.getHook().sendMessage("Sorry, but this server does not contain any stored players in its database").setEphemeral(true).queue(); return; } @@ -48,13 +50,13 @@ public class StaffUnverifyCommand extends SDLinkSlashCommand { Member member = event.getGuild().getMemberById(user.getId()); if (member == null) { - event.reply(user.getEffectiveName() + " is not a member of this discord server").setEphemeral(true).queue(); + event.getHook().sendMessage(user.getEffectiveName() + " is not a member of this discord server").setEphemeral(true).queue(); return; } MinecraftAccount minecraftAccount = MinecraftAccount.of(mcname); Result result = minecraftAccount.unverifyAccount(member, event.getGuild()); - event.reply(result.getMessage()).setEphemeral(true).queue(); + event.getHook().sendMessage(result.getMessage()).setEphemeral(true).queue(); } } \ No newline at end of file diff --git a/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/verification/StaffVerifyAccountCommand.java b/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/verification/StaffVerifyAccountCommand.java index 44c3627..14de0e3 100644 --- a/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/verification/StaffVerifyAccountCommand.java +++ b/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/verification/StaffVerifyAccountCommand.java @@ -30,20 +30,22 @@ public class StaffVerifyAccountCommand extends SDLinkSlashCommand { @Override protected void execute(SlashCommandEvent event) { + event.deferReply(true).queue(); + String mcname = event.getOption("mcname").getAsString(); User user = event.getOption("discorduser").getAsUser(); Member member = event.getGuild().getMemberById(user.getId()); if (member == null) { - event.reply(user.getEffectiveName() + " is not a member of this discord server").setEphemeral(true).queue(); + event.getHook().sendMessage(user.getEffectiveName() + " is not a member of this discord server").setEphemeral(true).queue(); return; } MinecraftAccount minecraftAccount = MinecraftAccount.of(mcname); Result result = minecraftAccount.verifyAccount(member, event.getGuild()); - event.reply(result.getMessage()).setEphemeral(true).queue(); + event.getHook().sendMessage(result.getMessage()).setEphemeral(true).queue(); } } diff --git a/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/verification/UnverifyAccountSlashCommand.java b/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/verification/UnverifyAccountSlashCommand.java index a885719..34eb5b7 100644 --- a/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/verification/UnverifyAccountSlashCommand.java +++ b/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/verification/UnverifyAccountSlashCommand.java @@ -24,11 +24,13 @@ public class UnverifyAccountSlashCommand extends SDLinkSlashCommand { @Override protected void execute(SlashCommandEvent event) { + event.deferReply(true).queue(); + sdlinkDatabase.reloadCollection("verifiedaccounts"); List accounts = sdlinkDatabase.findAll(SDLinkAccount.class); if (accounts.isEmpty()) { - event.reply("Sorry, but this server does not contain any stored players in its database").setEphemeral(true).queue(); + event.getHook().sendMessage("Sorry, but this server does not contain any stored players in its database").setEphemeral(true).queue(); return; } @@ -36,12 +38,12 @@ public class UnverifyAccountSlashCommand extends SDLinkSlashCommand { if (account.getDiscordID() != null && account.getDiscordID().equalsIgnoreCase(event.getMember().getId())) { MinecraftAccount minecraftAccount = MinecraftAccount.of(account.getUsername()); Result result = minecraftAccount.unverifyAccount(event.getMember(), event.getGuild()); - event.reply(result.getMessage()).setEphemeral(true).queue(); + event.getHook().sendMessage(result.getMessage()).setEphemeral(true).queue(); break; } } - event.reply("Sorry, we could not un-verify your Minecraft account. Please try again").setEphemeral(true).queue(); + event.getHook().sendMessage("Sorry, we could not un-verify your Minecraft account. Please try again").setEphemeral(true).queue(); } } \ No newline at end of file diff --git a/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/verification/VerifyAccountCommand.java b/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/verification/VerifyAccountCommand.java index 0e908e7..f7cefda 100644 --- a/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/verification/VerifyAccountCommand.java +++ b/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/verification/VerifyAccountCommand.java @@ -29,10 +29,13 @@ public class VerifyAccountCommand extends SDLinkSlashCommand { @Override protected void execute(SlashCommandEvent event) { + event.deferReply(true).queue(); + + int mcCode = event.getOption("code") != null ? event.getOption("code").getAsInt() : 0; if (mcCode == 0) { - event.reply("You need to provide a verification code").setEphemeral(true).queue(); + event.getHook().sendMessage("You need to provide a verification code").setEphemeral(true).queue(); return; } @@ -40,7 +43,7 @@ public class VerifyAccountCommand extends SDLinkSlashCommand { List accounts = sdlinkDatabase.findAll(SDLinkAccount.class); if (accounts.isEmpty()) { - event.reply("Sorry, but this server does not contain any stored players in its database").setEphemeral(true).queue(); + event.getHook().sendMessage("Sorry, but this server does not contain any stored players in its database").setEphemeral(true).queue(); return; } @@ -51,12 +54,12 @@ public class VerifyAccountCommand extends SDLinkSlashCommand { if (account.getVerifyCode().equalsIgnoreCase(String.valueOf(mcCode))) { MinecraftAccount minecraftAccount = MinecraftAccount.of(account.getUsername()); Result result = minecraftAccount.verifyAccount(event.getMember(), event.getGuild()); - event.reply(result.getMessage()).setEphemeral(true).queue(); + event.getHook().sendMessage(result.getMessage()).setEphemeral(true).queue(); return; } } - event.reply("Sorry, we could not verify your Minecraft account. Please try again").setEphemeral(true).queue(); + event.getHook().sendMessage("Sorry, we could not verify your Minecraft account. Please try again").setEphemeral(true).queue(); } } \ No newline at end of file diff --git a/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/verification/ViewVerifiedAccounts.java b/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/verification/ViewVerifiedAccounts.java index 062ad1f..1de9cf3 100644 --- a/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/verification/ViewVerifiedAccounts.java +++ b/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/verification/ViewVerifiedAccounts.java @@ -36,6 +36,7 @@ public class ViewVerifiedAccounts extends SDLinkSlashCommand { @Override protected void execute(SlashCommandEvent event) { + event.deferReply(true).queue(); try { ButtonEmbedPaginator.Builder paginator = MessageUtil.defaultPaginator(); @@ -47,7 +48,7 @@ public class ViewVerifiedAccounts extends SDLinkSlashCommand { AtomicInteger count = new AtomicInteger(); if (accounts.isEmpty()) { - event.reply("There are no verified accounts for this discord").setEphemeral(true).queue(); + event.getHook().sendMessage("There are no verified accounts for this discord").setEphemeral(true).queue(); return; } @@ -74,8 +75,9 @@ public class ViewVerifiedAccounts extends SDLinkSlashCommand { paginator.setItems(pages); ButtonEmbedPaginator embedPaginator = paginator.build(); - event.replyEmbeds(pages.get(0)).setEphemeral(false).queue(success -> success.retrieveOriginal().queue(msg -> embedPaginator.paginate(msg, 1))); + event.getHook().sendMessageEmbeds(pages.get(0)).setEphemeral(false).queue(success -> embedPaginator.paginate(success, 1)); } catch (Exception e) { + event.getHook().sendMessage("Failed to execute command. Please see your server log").setEphemeral(true).queue(); if (SDLinkConfig.INSTANCE.generalConfig.debugging) e.printStackTrace(); } diff --git a/src/main/java/com/hypherionmc/sdlink/core/discord/events/DiscordEventHandler.java b/src/main/java/com/hypherionmc/sdlink/core/discord/events/DiscordEventHandler.java index c50ace6..bf11213 100644 --- a/src/main/java/com/hypherionmc/sdlink/core/discord/events/DiscordEventHandler.java +++ b/src/main/java/com/hypherionmc/sdlink/core/discord/events/DiscordEventHandler.java @@ -87,8 +87,7 @@ public class DiscordEventHandler extends ListenerAdapter { @Override public void onButtonInteraction(@NotNull ButtonInteractionEvent event) { if (event.getComponentId().equals("sdrefreshbtn")) { - event.getMessage().editMessageEmbeds(ServerStatusSlashCommand.runStatusCommand()).queue(); - event.reply("Success!").setEphemeral(true).queue(); + event.deferEdit().queue(s -> s.editOriginalEmbeds(ServerStatusSlashCommand.runStatusCommand()).queue()); } }