From 96e7518f72f4b29dbb02f1724bc5d6724761726e Mon Sep 17 00:00:00 2001 From: Liam Date: Tue, 9 Jul 2024 23:02:12 +0100 Subject: [PATCH] fix interactions --- pom.xml | 5 - .../cc/fascinated/bat/command/BatCommand.java | 2 + .../cc/fascinated/bat/command/Category.java | 13 ++ .../bat/common/InteractionBuilder.java | 118 +++++++++++++ .../base/commands/general/HelpCommand.java | 162 ++++++------------ .../logging/command/ListSubCommand.java | 88 ++++------ .../command/PunishHistoryCommand.java | 61 ++----- .../bat/service/DiscordService.java | 3 - .../bat/service/InteractionService.java | 79 +++++++++ 9 files changed, 319 insertions(+), 212 deletions(-) create mode 100644 src/main/java/cc/fascinated/bat/common/InteractionBuilder.java create mode 100644 src/main/java/cc/fascinated/bat/service/InteractionService.java diff --git a/pom.xml b/pom.xml index d53a15c..8817203 100644 --- a/pom.xml +++ b/pom.xml @@ -191,10 +191,5 @@ mcutils-java-library 1.2.4 - - com.github.ygimenez - Pagination-Utils - 4.0.6 - diff --git a/src/main/java/cc/fascinated/bat/command/BatCommand.java b/src/main/java/cc/fascinated/bat/command/BatCommand.java index b5bb0b7..6cb0aef 100644 --- a/src/main/java/cc/fascinated/bat/command/BatCommand.java +++ b/src/main/java/cc/fascinated/bat/command/BatCommand.java @@ -8,6 +8,7 @@ import net.dv8tion.jda.api.entities.Member; import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel; import net.dv8tion.jda.api.interactions.IntegrationType; import net.dv8tion.jda.api.interactions.InteractionContextType; +import net.dv8tion.jda.api.interactions.commands.DefaultMemberPermissions; import net.dv8tion.jda.api.interactions.commands.OptionMapping; import net.dv8tion.jda.api.interactions.commands.SlashCommandInteraction; import net.dv8tion.jda.api.interactions.commands.build.OptionData; @@ -66,6 +67,7 @@ public abstract class BatCommand { } info = new InternalCommandInfo(getClass().getAnnotation(CommandInfo.class)); commandData = new CommandDataImpl(info.getName(), info.getDescription()) + .setDefaultPermissions(DefaultMemberPermissions.enabledFor(info.getPermissions())) .setIntegrationTypes(info.isUserInstall() ? EnumSet.of(IntegrationType.USER_INSTALL) : EnumSet.of(IntegrationType.GUILD_INSTALL) diff --git a/src/main/java/cc/fascinated/bat/command/Category.java b/src/main/java/cc/fascinated/bat/command/Category.java index c86c38b..729f29a 100644 --- a/src/main/java/cc/fascinated/bat/command/Category.java +++ b/src/main/java/cc/fascinated/bat/command/Category.java @@ -5,6 +5,10 @@ import lombok.Getter; import lombok.NonNull; import net.dv8tion.jda.api.entities.emoji.Emoji; +import java.util.Arrays; +import java.util.Comparator; +import java.util.List; + /** * @author Fascinated (fascinated7) */ @@ -43,4 +47,13 @@ public enum Category { } return null; } + + /** + * Gets the categories sorted by their name + * + * @return the sorted categories + */ + public static List getSortedByName() { + return Arrays.stream(Category.values()).sorted(Comparator.comparing(Category::getName)).toList(); + } } diff --git a/src/main/java/cc/fascinated/bat/common/InteractionBuilder.java b/src/main/java/cc/fascinated/bat/common/InteractionBuilder.java new file mode 100644 index 0000000..fdb2435 --- /dev/null +++ b/src/main/java/cc/fascinated/bat/common/InteractionBuilder.java @@ -0,0 +1,118 @@ +package cc.fascinated.bat.common; + +import cc.fascinated.bat.event.EventListener; +import cc.fascinated.bat.model.BatGuild; +import cc.fascinated.bat.model.BatUser; +import cc.fascinated.bat.service.InteractionService; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NonNull; +import net.dv8tion.jda.api.entities.emoji.Emoji; +import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent; +import net.dv8tion.jda.api.events.interaction.component.StringSelectInteractionEvent; +import net.dv8tion.jda.api.interactions.components.ActionComponent; +import net.dv8tion.jda.api.interactions.components.ActionRow; +import net.dv8tion.jda.api.interactions.components.ComponentInteraction; +import net.dv8tion.jda.api.interactions.components.ItemComponent; +import net.dv8tion.jda.api.interactions.components.buttons.Button; +import net.dv8tion.jda.api.interactions.components.selections.SelectMenu; +import net.dv8tion.jda.api.interactions.components.selections.SelectOption; +import net.dv8tion.jda.api.interactions.components.selections.StringSelectMenu; +import net.dv8tion.jda.api.utils.data.SerializableData; +import org.springframework.stereotype.Component; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.function.Consumer; + +import static net.dv8tion.jda.api.interactions.components.Component.Type.STRING_SELECT; + +/** + * @author Fascinated (fascinated7) + */ +@Getter +public class InteractionBuilder { + /** + * The button interactions + */ + private final Map> buttonInteractions = new HashMap<>(); + + /** + * The select menu interactions + */ + private final Map> selectMenuInteractions = new HashMap<>(); + + /** + * The select menu id + */ + private String selectMenuId; + + public InteractionBuilder() { + InteractionService.INSTANCE.addInteractionBuilder(this); + } + + /** + * Adds a button to the interaction builder + * + * @param display The display of the button + * @param onClick The consumer to run when the button is clicked + * @return - The interaction builder + */ + public InteractionBuilder addButton(String display, Consumer onClick) { + String id = StringUtils.randomString(8); + this.buttonInteractions.put(Button.primary(id, display), onClick); + return this; + } + + /** + * Adds a URL button to the interaction builder + * + * @param display The display of the button + * @param url The url to open when the button is clicked + * @return - The interaction builder + */ + public InteractionBuilder addUrlButton(String display, String url, Emoji emoji) { + this.buttonInteractions.put(Button.link(url, display).withEmoji(emoji), event -> {}); + return this; + } + + /** + * Adds a string selection to the interaction builder + * + * @param display the name of the selection + * @param description the description of the selection + * @param emoji the emoji of the selection + * @param onClick the consumer to run when the selection is clicked + * @return the interaction builder + */ + public InteractionBuilder addStringSelect(String display, String description, Emoji emoji, Consumer onClick) { + String id = StringUtils.randomString(8); + this.selectMenuInteractions.put(SelectOption.of(display, id).withDescription(description).withEmoji(emoji), onClick); + return this; + } + + /** + * Builds the interactions into an action row + * + * @return The action row + */ + public List build() { + List components = new ArrayList<>(); + if (!this.getButtonInteractions().isEmpty()) { + List