package cc.fascinated.bat.service; import cc.fascinated.bat.common.InteractionBuilder; import cc.fascinated.bat.event.EventListener; import cc.fascinated.bat.model.BatGuild; import cc.fascinated.bat.model.BatUser; import lombok.NonNull; import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent; import net.dv8tion.jda.api.events.interaction.component.StringSelectInteractionEvent; import net.dv8tion.jda.api.hooks.ListenerAdapter; import net.dv8tion.jda.api.interactions.components.buttons.Button; import net.dv8tion.jda.api.interactions.components.selections.SelectOption; import org.jetbrains.annotations.NotNull; import org.springframework.context.annotation.DependsOn; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Objects; import java.util.function.Consumer; /** * @author Fascinated (fascinated7) */ @Service @DependsOn("discordService") public class InteractionService extends ListenerAdapter { public static InteractionService INSTANCE; /** * The interactions to handle */ private final List interactions = new ArrayList<>(); public InteractionService() { INSTANCE = this; DiscordService.JDA.addEventListener(this); } /** * Adds an interaction builder to the service * * @param interactionBuilder - The interaction builder to add */ public void addInteractionBuilder(@NonNull InteractionBuilder interactionBuilder) { this.interactions.add(interactionBuilder); } @Override public void onButtonInteraction(@NotNull ButtonInteractionEvent event) { for (InteractionBuilder interactionBuilder : interactions) { for (Map.Entry> entry : interactionBuilder.getButtonInteractions().entrySet()) { Button button = entry.getKey(); if (!Objects.equals(button.getId(), event.getComponentId())) { continue; } entry.getValue().accept(event); break; } } } @Override public void onStringSelectInteraction(@NotNull StringSelectInteractionEvent event) { for (InteractionBuilder interactionBuilder : interactions) { if (!Objects.equals(interactionBuilder.getSelectMenuId(), event.getComponentId())) { continue; } for (Map.Entry> entry : interactionBuilder.getSelectMenuInteractions().entrySet()) { SelectOption selectOption = entry.getKey(); if (selectOption.getValue().equals(event.getSelectedOptions().get(0).getValue())) { entry.getValue().accept(event); break; } } } } }