Refactor code to replace GenericSelectMenuInteraction with StringSelectInteraction

The commit replaces instances of `GenericSelectMenuInteractionEvent` with `StringSelectInteractionEvent` to enhance type safety. This renaming results in a more specific interaction event and eliminates the need to check for type instantiation. In addition, a modification has been made to ensure a bid finishes properly in the `SelectMenuAuction` class with `deferEdit`.
This commit is contained in:
Teriuihi 2024-01-14 13:26:43 +01:00
parent b917a8c18f
commit e9db832901
4 changed files with 9 additions and 11 deletions

View File

@ -59,10 +59,10 @@ public class JDAListener extends ListenerAdapter {
} }
@Override @Override
public void onGenericSelectMenuInteraction(@NotNull GenericSelectMenuInteractionEvent event) { public void onStringSelectInteraction(@NotNull StringSelectInteractionEvent event) {
String s = event.getComponentId(); String s = event.getComponentId();
if (s.startsWith("request:") && event instanceof StringSelectInteractionEvent stringSelectInteractionEvent) { if (s.startsWith("request:")) {
RequestManager.onGenericSelectMenuInteraction(stringSelectInteractionEvent); RequestManager.onStringSelectInteraction(event);
} }
} }

View File

@ -7,9 +7,7 @@ import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel; import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel;
import net.dv8tion.jda.api.events.interaction.ModalInteractionEvent; import net.dv8tion.jda.api.events.interaction.ModalInteractionEvent;
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent; import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;
import net.dv8tion.jda.api.events.interaction.component.GenericSelectMenuInteractionEvent;
import net.dv8tion.jda.api.events.interaction.component.StringSelectInteractionEvent; import net.dv8tion.jda.api.events.interaction.component.StringSelectInteractionEvent;
import net.dv8tion.jda.api.interactions.components.selections.SelectMenu;
import net.dv8tion.jda.api.interactions.components.selections.StringSelectMenu; import net.dv8tion.jda.api.interactions.components.selections.StringSelectMenu;
import java.awt.*; import java.awt.*;
@ -58,7 +56,7 @@ public class RequestManager {
return RequestConfig.requests.stream().filter(request -> request.getId().equalsIgnoreCase(id)).findFirst().orElse(null); return RequestConfig.requests.stream().filter(request -> request.getId().equalsIgnoreCase(id)).findFirst().orElse(null);
} }
public static void onGenericSelectMenuInteraction(StringSelectInteractionEvent event) { public static void onStringSelectInteraction(StringSelectInteractionEvent event) {
String[] actions = event.getComponentId().split(":"); String[] actions = event.getComponentId().split(":");
if (actions[1].equals("create")) { if (actions[1].equals("create")) {
String[] selection = event.getSelectedOptions().get(0).getValue().split(":"); String[] selection = event.getSelectedOptions().get(0).getValue().split(":");

View File

@ -19,7 +19,7 @@ public class SelectMenuManager extends ListenerAdapter {
} }
@Override @Override
public void onGenericSelectMenuInteraction(@NotNull GenericSelectMenuInteractionEvent event) { public void onStringSelectInteraction(@NotNull StringSelectInteractionEvent event) {
String selectMenuId = event.getSelectMenu().getId(); String selectMenuId = event.getSelectMenu().getId();
Optional<DiscordSelectMenu> first = buttons.stream() Optional<DiscordSelectMenu> first = buttons.stream()
.filter(discordModal -> discordModal.getSelectMenuId().equalsIgnoreCase(selectMenuId)) .filter(discordModal -> discordModal.getSelectMenuId().equalsIgnoreCase(selectMenuId))
@ -34,8 +34,7 @@ public class SelectMenuManager extends ListenerAdapter {
// .queue(RestAction.getDefaultSuccess(), Util::handleFailure); // .queue(RestAction.getDefaultSuccess(), Util::handleFailure);
return; return;
} }
if (event instanceof StringSelectInteractionEvent stringSelectInteractionEvent) first.get().execute(event);
first.get().execute(stringSelectInteractionEvent);
} }
public @Nullable DiscordSelectMenu getDiscordSelectMenuFor(String buttonId) { public @Nullable DiscordSelectMenu getDiscordSelectMenuFor(String buttonId) {

View File

@ -120,7 +120,7 @@ public class SelectMenuAuction extends DiscordSelectMenu {
embedBuilder.addField("Current Bid", "$" + Util.formatNumber(currentBid) + " by " + event.getMember().getAsMention(), false); embedBuilder.addField("Current Bid", "$" + Util.formatNumber(currentBid) + " by " + event.getMember().getAsMention(), false);
ReplyCallbackAction replyCallbackAction = event.deferReply(true); ReplyCallbackAction replyCallbackAction = event.deferReply(true);
event.getMessage().editMessageEmbeds(embedBuilder.build()).queue( event.deferEdit().queue(edit -> edit.editOriginalEmbeds(embedBuilder.build()).queue(
success -> { success -> {
if (auction.updateExpiry()) if (auction.updateExpiry())
auctionScheduler.updateAuction(auction); auctionScheduler.updateAuction(auction);
@ -134,7 +134,8 @@ public class SelectMenuAuction extends DiscordSelectMenu {
.queue(); .queue();
success.editMessageComponents().setActionRow(auction.getSelectMenu(selectMenuManager, true)).queue(); success.editMessageComponents().setActionRow(auction.getSelectMenu(selectMenuManager, true)).queue();
}, },
error -> replyCallbackAction.setEmbeds(Util.genericErrorEmbed("Error", "Unable to finish your bid")).queue()); error -> replyCallbackAction.setEmbeds(Util.genericErrorEmbed("Error", "Unable to finish your bid")).queue())
);
} }
private MessageEmbed getMessageEmbed(List<MessageEmbed> embeds, StringSelectInteractionEvent event) { private MessageEmbed getMessageEmbed(List<MessageEmbed> embeds, StringSelectInteractionEvent event) {