From fdfe1e99b4fd5f8edc34e71467e261d1165b1537 Mon Sep 17 00:00:00 2001 From: Dynxsty Date: Sat, 13 Nov 2021 14:29:58 +0100 Subject: [PATCH 1/6] added Interaction Framework logic --- .../java/net/javadiscord/javabot2/Bot.java | 7 ++ .../InteractionHandlerBuilder.java | 37 +++++++++ .../interaction/InteractionListener.java | 41 ++++++++++ .../interaction/button/ButtonAction.java | 51 ++++++++++++ .../interaction/button/ButtonHandler.java | 9 +++ .../selection_menu/SelectMenuAction.java | 79 +++++++++++++++++++ .../selection_menu/SelectionMenuHandler.java | 9 +++ 7 files changed, 233 insertions(+) create mode 100644 src/main/java/net/javadiscord/javabot2/command/interaction/InteractionHandlerBuilder.java create mode 100644 src/main/java/net/javadiscord/javabot2/command/interaction/InteractionListener.java create mode 100644 src/main/java/net/javadiscord/javabot2/command/interaction/button/ButtonAction.java create mode 100644 src/main/java/net/javadiscord/javabot2/command/interaction/button/ButtonHandler.java create mode 100644 src/main/java/net/javadiscord/javabot2/command/interaction/selection_menu/SelectMenuAction.java create mode 100644 src/main/java/net/javadiscord/javabot2/command/interaction/selection_menu/SelectionMenuHandler.java diff --git a/src/main/java/net/javadiscord/javabot2/Bot.java b/src/main/java/net/javadiscord/javabot2/Bot.java index efa52bb..b39e083 100644 --- a/src/main/java/net/javadiscord/javabot2/Bot.java +++ b/src/main/java/net/javadiscord/javabot2/Bot.java @@ -8,6 +8,7 @@ import com.zaxxer.hikari.HikariDataSource; import lombok.extern.slf4j.Slf4j; import net.javadiscord.javabot2.command.SlashCommandListener; +import net.javadiscord.javabot2.command.interaction.InteractionListener; import net.javadiscord.javabot2.config.BotConfig; import net.javadiscord.javabot2.db.DbHelper; import net.javadiscord.javabot2.systems.moderation.ModerationService; @@ -81,9 +82,15 @@ public static void main(String[] args) { "commands/moderation.yaml" ); api.addSlashCommandCreateListener(commandListener); + addEventListeners(api); initScheduledTasks(api); } + private static void addEventListeners(DiscordApi api) { + api.addButtonClickListener(new InteractionListener()); + api.addSelectMenuChooseListener(new InteractionListener()); + } + /** * Initializes all the basic data sources that are needed by the bot's other * capabilities. This should be called before logging in diff --git a/src/main/java/net/javadiscord/javabot2/command/interaction/InteractionHandlerBuilder.java b/src/main/java/net/javadiscord/javabot2/command/interaction/InteractionHandlerBuilder.java new file mode 100644 index 0000000..c4eb0f8 --- /dev/null +++ b/src/main/java/net/javadiscord/javabot2/command/interaction/InteractionHandlerBuilder.java @@ -0,0 +1,37 @@ +package net.javadiscord.javabot2.command.interaction; + +import net.javadiscord.javabot2.command.interaction.button.ButtonAction; +import net.javadiscord.javabot2.command.interaction.selection_menu.SelectMenuAction; +import org.javacord.api.entity.message.component.ActionRow; +import org.javacord.api.entity.message.component.LowLevelComponent; +import org.javacord.api.interaction.callback.InteractionImmediateResponseBuilder; + +import java.util.ArrayList; +import java.util.List; + +public class InteractionHandlerBuilder { + + private final InteractionImmediateResponseBuilder responseBuilder; + private final List buttons = new ArrayList<>(); + private final List selectMenus = new ArrayList<>(); + + public InteractionHandlerBuilder(InteractionImmediateResponseBuilder responseBuilder) { + this.responseBuilder = responseBuilder; + } + + public InteractionHandlerBuilder addButtons(ButtonAction... buttonActions) { + for (var action : buttonActions) buttons.add(action.getButton()); + return this; + } + + public InteractionHandlerBuilder addSelectionMenus(SelectMenuAction... selectMenuActions) { + for (var action : selectMenuActions) { + selectMenus.add(action.getSelectMenu()); + } + return this; + } + + public InteractionImmediateResponseBuilder getResponseBuilder() { + return responseBuilder.addComponents(ActionRow.of(buttons), ActionRow.of(selectMenus)); + } +} diff --git a/src/main/java/net/javadiscord/javabot2/command/interaction/InteractionListener.java b/src/main/java/net/javadiscord/javabot2/command/interaction/InteractionListener.java new file mode 100644 index 0000000..f0d483d --- /dev/null +++ b/src/main/java/net/javadiscord/javabot2/command/interaction/InteractionListener.java @@ -0,0 +1,41 @@ +package net.javadiscord.javabot2.command.interaction; + +import net.javadiscord.javabot2.command.ResponseException; +import net.javadiscord.javabot2.command.interaction.button.ButtonHandler; +import net.javadiscord.javabot2.command.interaction.selection_menu.SelectionMenuHandler; +import org.javacord.api.event.interaction.ButtonClickEvent; +import org.javacord.api.event.interaction.SelectMenuChooseEvent; +import org.javacord.api.listener.interaction.ButtonClickListener; +import org.javacord.api.listener.interaction.SelectMenuChooseListener; + +public class InteractionListener implements ButtonClickListener, SelectMenuChooseListener { + + @Override + public void onButtonClick(ButtonClickEvent event) { + var id = event.getButtonInteraction().getCustomId().split(":"); + ButtonHandler handler = (ButtonHandler) getHandlerByName(id[0]); + try { + handler.handleButtonInteraction(event.getButtonInteraction()).respond(); + } catch (ResponseException e) { e.printStackTrace(); } + } + + @Override + public void onSelectMenuChoose(SelectMenuChooseEvent event) { + var id = event.getSelectMenuInteraction().getCustomId().split(":"); + SelectionMenuHandler handler = (SelectionMenuHandler) getHandlerByName(id[0]); + try { + handler.handleSelectMenuInteraction(event.getSelectMenuInteraction()).respond(); + } catch (ResponseException e) { e.printStackTrace(); } + } + + private Object getHandlerByName(String name) { + try { + return Class.forName(name) + .getDeclaredConstructor() + .newInstance(); + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } +} diff --git a/src/main/java/net/javadiscord/javabot2/command/interaction/button/ButtonAction.java b/src/main/java/net/javadiscord/javabot2/command/interaction/button/ButtonAction.java new file mode 100644 index 0000000..2396bf4 --- /dev/null +++ b/src/main/java/net/javadiscord/javabot2/command/interaction/button/ButtonAction.java @@ -0,0 +1,51 @@ +package net.javadiscord.javabot2.command.interaction.button; + +import org.javacord.api.entity.message.component.ActionRow; +import org.javacord.api.entity.message.component.Button; +import org.javacord.api.entity.message.component.ButtonStyle; + +import java.util.ArrayList; +import java.util.List; + +public class ButtonAction { + + private final String label; + private final ButtonStyle buttonStyle; + private Class handler; + private final List params; + + public ButtonAction(String label, ButtonStyle buttonStyle) { + this.params = new ArrayList<>(); + + this.label = label; + this.buttonStyle = buttonStyle; + } + + public ButtonAction handledBy(Class handler) { + this.handler = handler; + return this; + } + + public ButtonAction addParam(Object param) { + params.add(String.valueOf(param)); + return this; + } + + public String getCustomId() { + StringBuilder id = new StringBuilder(handler.getName()); + for (String param : params) { + id.append(":").append(param); + } + return id.toString(); + } + + public ButtonStyle getButtonStyle() { return buttonStyle; } + + public String getLabel() { return label; } + + public Class getHandler() { return handler; } + + public List getParams() { return params; } + + public Button getButton() { return Button.create(getCustomId(), buttonStyle, label); } +} diff --git a/src/main/java/net/javadiscord/javabot2/command/interaction/button/ButtonHandler.java b/src/main/java/net/javadiscord/javabot2/command/interaction/button/ButtonHandler.java new file mode 100644 index 0000000..0c62f95 --- /dev/null +++ b/src/main/java/net/javadiscord/javabot2/command/interaction/button/ButtonHandler.java @@ -0,0 +1,9 @@ +package net.javadiscord.javabot2.command.interaction.button; + +import net.javadiscord.javabot2.command.ResponseException; +import org.javacord.api.interaction.ButtonInteraction; +import org.javacord.api.interaction.callback.InteractionImmediateResponseBuilder; + +public interface ButtonHandler { + InteractionImmediateResponseBuilder handleButtonInteraction(ButtonInteraction interaction) throws ResponseException; +} diff --git a/src/main/java/net/javadiscord/javabot2/command/interaction/selection_menu/SelectMenuAction.java b/src/main/java/net/javadiscord/javabot2/command/interaction/selection_menu/SelectMenuAction.java new file mode 100644 index 0000000..20c259e --- /dev/null +++ b/src/main/java/net/javadiscord/javabot2/command/interaction/selection_menu/SelectMenuAction.java @@ -0,0 +1,79 @@ +package net.javadiscord.javabot2.command.interaction.selection_menu; + +import org.javacord.api.entity.message.component.ActionRow; +import org.javacord.api.entity.message.component.SelectMenu; +import org.javacord.api.entity.message.component.SelectMenuOption; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class SelectMenuAction { + + private Class handler; + private List options; + private String placeholder = "Make a selection"; + private int minValue = 0; + private int maxValue = 25; + private boolean disabled = false; + + public SelectMenuAction() { + this.options = new ArrayList<>(); + } + + public SelectMenuAction setMaxValue(int maxValue) { + this.maxValue = maxValue; + return this; + } + + public SelectMenuAction setMinValue(int minValue) { + this.minValue = minValue; + return this; + } + + public SelectMenuAction setPlaceholder(String placeholder) { + this.placeholder = placeholder; + return this; + } + + public SelectMenuAction setDisabled(boolean disabled) { + this.disabled = disabled; + return this; + } + + public SelectMenuAction handledBy(Class handler) { + this.handler = handler; + return this; + } + + public SelectMenuAction addOption(SelectMenuOption option) { + options.add(option); + return this; + } + + public SelectMenuAction addOptions(SelectMenuOption... options) { + this.options.addAll(Arrays.asList(options)); + return this; + } + + public String getCustomId() { + return handler.getName(); + } + + public Class getHandler() { return handler; } + + public List getOptions() { return options; } + + public SelectMenu getSelectMenu() { + if (options.isEmpty()) throw new IllegalStateException("SelectMenu options may not be empty!"); + if (minValue > maxValue) throw new IllegalArgumentException("minValue may not be greater than maxValue!"); + return SelectMenu.create( + getCustomId(), + placeholder, + minValue, + Math.min(maxValue, options.size()), // maxValue cannot be greater than the provided amount of options + options, + disabled + ); + } +} diff --git a/src/main/java/net/javadiscord/javabot2/command/interaction/selection_menu/SelectionMenuHandler.java b/src/main/java/net/javadiscord/javabot2/command/interaction/selection_menu/SelectionMenuHandler.java new file mode 100644 index 0000000..fc7e628 --- /dev/null +++ b/src/main/java/net/javadiscord/javabot2/command/interaction/selection_menu/SelectionMenuHandler.java @@ -0,0 +1,9 @@ +package net.javadiscord.javabot2.command.interaction.selection_menu; + +import net.javadiscord.javabot2.command.ResponseException; +import org.javacord.api.interaction.SelectMenuInteraction; +import org.javacord.api.interaction.callback.InteractionImmediateResponseBuilder; + +public interface SelectionMenuHandler { + InteractionImmediateResponseBuilder handleSelectMenuInteraction(SelectMenuInteraction interaction) throws ResponseException; +} From 1ff18bbd3ec8b38dfaff39d8ed9496dec04e0e71 Mon Sep 17 00:00:00 2001 From: Dynxsty Date: Sat, 13 Nov 2021 14:48:52 +0100 Subject: [PATCH 2/6] replaced static `.create(...)` methods with interaction builders --- .../interaction/button/ButtonAction.java | 33 +++++++++++++++++-- .../selection_menu/SelectMenuAction.java | 17 +++++----- 2 files changed, 40 insertions(+), 10 deletions(-) diff --git a/src/main/java/net/javadiscord/javabot2/command/interaction/button/ButtonAction.java b/src/main/java/net/javadiscord/javabot2/command/interaction/button/ButtonAction.java index 2396bf4..7600342 100644 --- a/src/main/java/net/javadiscord/javabot2/command/interaction/button/ButtonAction.java +++ b/src/main/java/net/javadiscord/javabot2/command/interaction/button/ButtonAction.java @@ -1,7 +1,8 @@ package net.javadiscord.javabot2.command.interaction.button; -import org.javacord.api.entity.message.component.ActionRow; +import org.javacord.api.entity.emoji.Emoji; import org.javacord.api.entity.message.component.Button; +import org.javacord.api.entity.message.component.ButtonBuilder; import org.javacord.api.entity.message.component.ButtonStyle; import java.util.ArrayList; @@ -13,6 +14,9 @@ public class ButtonAction { private final ButtonStyle buttonStyle; private Class handler; private final List params; + private boolean disabled = false; + private String url; + private Emoji emoji; public ButtonAction(String label, ButtonStyle buttonStyle) { this.params = new ArrayList<>(); @@ -31,6 +35,21 @@ public ButtonAction addParam(Object param) { return this; } + public ButtonAction setDisabled(boolean disabled) { + this.disabled = disabled; + return this; + } + + public ButtonAction setUrl(String url) { + this.url = url; + return this; + } + + public ButtonAction setEmoji(Emoji emoji) { + this.emoji = emoji; + return this; + } + public String getCustomId() { StringBuilder id = new StringBuilder(handler.getName()); for (String param : params) { @@ -47,5 +66,15 @@ public String getCustomId() { public List getParams() { return params; } - public Button getButton() { return Button.create(getCustomId(), buttonStyle, label); } + public Button getButton() { + var builder = new ButtonBuilder() + .setLabel(label) + .setStyle(buttonStyle) + .setDisabled(disabled); + if (buttonStyle != ButtonStyle.LINK) builder.setCustomId(getCustomId()); + if (url != null && !url.isBlank() && !url.isEmpty() && buttonStyle == ButtonStyle.LINK) builder.setUrl(url); + if (emoji != null) builder.setEmoji(emoji); + + return builder.build(); + } } diff --git a/src/main/java/net/javadiscord/javabot2/command/interaction/selection_menu/SelectMenuAction.java b/src/main/java/net/javadiscord/javabot2/command/interaction/selection_menu/SelectMenuAction.java index 20c259e..01c9828 100644 --- a/src/main/java/net/javadiscord/javabot2/command/interaction/selection_menu/SelectMenuAction.java +++ b/src/main/java/net/javadiscord/javabot2/command/interaction/selection_menu/SelectMenuAction.java @@ -2,6 +2,7 @@ import org.javacord.api.entity.message.component.ActionRow; import org.javacord.api.entity.message.component.SelectMenu; +import org.javacord.api.entity.message.component.SelectMenuBuilder; import org.javacord.api.entity.message.component.SelectMenuOption; import java.util.ArrayList; @@ -67,13 +68,13 @@ public String getCustomId() { public SelectMenu getSelectMenu() { if (options.isEmpty()) throw new IllegalStateException("SelectMenu options may not be empty!"); if (minValue > maxValue) throw new IllegalArgumentException("minValue may not be greater than maxValue!"); - return SelectMenu.create( - getCustomId(), - placeholder, - minValue, - Math.min(maxValue, options.size()), // maxValue cannot be greater than the provided amount of options - options, - disabled - ); + return new SelectMenuBuilder() + .setCustomId(getCustomId()) + .setPlaceholder(placeholder) + .setMinimumValues(minValue) + .setMaximumValues(Math.min(maxValue, options.size())) // maxValue cannot be greater than the provided amount of options + .addOptions(options) + .setDisabled(disabled) + .build(); } } From 1aafd8cb248ea22d8cf9f3e9bdbe1e5aa1c08910 Mon Sep 17 00:00:00 2001 From: Dynxsty Date: Sat, 13 Nov 2021 16:05:34 +0100 Subject: [PATCH 3/6] added some javadoc --- .../InteractionHandlerBuilder.java | 18 +++- .../interaction/InteractionListener.java | 22 ++++- .../interaction/button/ButtonAction.java | 80 ++++++++++++++-- .../interaction/button/ButtonHandler.java | 4 + .../selection_menu/SelectMenuAction.java | 96 +++++++++++++++---- .../selection_menu/SelectionMenuHandler.java | 4 + 6 files changed, 197 insertions(+), 27 deletions(-) diff --git a/src/main/java/net/javadiscord/javabot2/command/interaction/InteractionHandlerBuilder.java b/src/main/java/net/javadiscord/javabot2/command/interaction/InteractionHandlerBuilder.java index c4eb0f8..298f542 100644 --- a/src/main/java/net/javadiscord/javabot2/command/interaction/InteractionHandlerBuilder.java +++ b/src/main/java/net/javadiscord/javabot2/command/interaction/InteractionHandlerBuilder.java @@ -9,8 +9,10 @@ import java.util.ArrayList; import java.util.List; +/** + * Class that is used to build message interactions. + */ public class InteractionHandlerBuilder { - private final InteractionImmediateResponseBuilder responseBuilder; private final List buttons = new ArrayList<>(); private final List selectMenus = new ArrayList<>(); @@ -19,11 +21,21 @@ public InteractionHandlerBuilder(InteractionImmediateResponseBuilder responseBui this.responseBuilder = responseBuilder; } + /** + * Adds one or multiple {@link ButtonAction}s. + * @param buttonActions an array of {@link ButtonAction}s. + * @return the current instance in order to allow chain call methods. + */ public InteractionHandlerBuilder addButtons(ButtonAction... buttonActions) { for (var action : buttonActions) buttons.add(action.getButton()); return this; } + /** + * Adds one or multiple {@link SelectMenuAction}s. + * @param selectMenuActions an array of {@link SelectMenuAction}s. + * @return the current instance in order to allow chain call methods. + */ public InteractionHandlerBuilder addSelectionMenus(SelectMenuAction... selectMenuActions) { for (var action : selectMenuActions) { selectMenus.add(action.getSelectMenu()); @@ -31,6 +43,10 @@ public InteractionHandlerBuilder addSelectionMenus(SelectMenuAction... selectMen return this; } + /** + * Returns the provided Response Builder. + * @return the current instance in order to allow chain call methods. + */ public InteractionImmediateResponseBuilder getResponseBuilder() { return responseBuilder.addComponents(ActionRow.of(buttons), ActionRow.of(selectMenus)); } diff --git a/src/main/java/net/javadiscord/javabot2/command/interaction/InteractionListener.java b/src/main/java/net/javadiscord/javabot2/command/interaction/InteractionListener.java index f0d483d..0bf373d 100644 --- a/src/main/java/net/javadiscord/javabot2/command/interaction/InteractionListener.java +++ b/src/main/java/net/javadiscord/javabot2/command/interaction/InteractionListener.java @@ -8,6 +8,11 @@ import org.javacord.api.listener.interaction.ButtonClickListener; import org.javacord.api.listener.interaction.SelectMenuChooseListener; +import java.lang.reflect.InvocationTargetException; + +/** + * Listener for all supported interactions. + */ public class InteractionListener implements ButtonClickListener, SelectMenuChooseListener { @Override @@ -16,7 +21,9 @@ public void onButtonClick(ButtonClickEvent event) { ButtonHandler handler = (ButtonHandler) getHandlerByName(id[0]); try { handler.handleButtonInteraction(event.getButtonInteraction()).respond(); - } catch (ResponseException e) { e.printStackTrace(); } + } catch (ResponseException e) { + e.printStackTrace(); + } } @Override @@ -25,15 +32,24 @@ public void onSelectMenuChoose(SelectMenuChooseEvent event) { SelectionMenuHandler handler = (SelectionMenuHandler) getHandlerByName(id[0]); try { handler.handleSelectMenuInteraction(event.getSelectMenuInteraction()).respond(); - } catch (ResponseException e) { e.printStackTrace(); } + } catch (ResponseException e) { + e.printStackTrace(); + } } + /** + * Tries to get a Class by the specified name + * (Class paths are baked into the button id) and returns it. + * @param name the class name + * @return The handler class + */ private Object getHandlerByName(String name) { try { return Class.forName(name) .getDeclaredConstructor() .newInstance(); - } catch (Exception e) { + } catch (ClassNotFoundException | NoSuchMethodException | InstantiationException | + IllegalAccessException | InvocationTargetException e) { e.printStackTrace(); return null; } diff --git a/src/main/java/net/javadiscord/javabot2/command/interaction/button/ButtonAction.java b/src/main/java/net/javadiscord/javabot2/command/interaction/button/ButtonAction.java index 7600342..7c83fc8 100644 --- a/src/main/java/net/javadiscord/javabot2/command/interaction/button/ButtonAction.java +++ b/src/main/java/net/javadiscord/javabot2/command/interaction/button/ButtonAction.java @@ -8,6 +8,9 @@ import java.util.ArrayList; import java.util.List; +/** + * Class that represents a single Button Interaction. + */ public class ButtonAction { private final String label; @@ -18,38 +21,75 @@ public class ButtonAction { private String url; private Emoji emoji; + /** + * Constructor of the Button Action. + * @param label the button's label + * @param buttonStyle the button's {@link ButtonStyle} + */ public ButtonAction(String label, ButtonStyle buttonStyle) { this.params = new ArrayList<>(); - this.label = label; this.buttonStyle = buttonStyle; } + /** + * Sets a handler class for this button interaction. + * The class must extend {@link ButtonHandler}. + * @param handler a class that should handle the button interaction. + * @return the current instance in order to allow chain call methods. + */ public ButtonAction handledBy(Class handler) { this.handler = handler; return this; } + /** + * Adds a parameter, which is later baked into the button id. + * @param param an object that is later converted to a string to bake it into the button id as a parameter. + * @return the current instance in order to allow chain call methods. + */ public ButtonAction addParam(Object param) { params.add(String.valueOf(param)); return this; } + /** + * Enables/Disables the button based on the provided boolean. + * @param disabled the boolean which enables/disables the button. + * @return the current instance in order to allow chain call methods. + */ public ButtonAction setDisabled(boolean disabled) { this.disabled = disabled; return this; } + /** + * Sets an Url for this button. + * This only works for buttons with the {@link ButtonStyle#LINK} button style. + * @param url the url as a string. + * @return the current instance in order to allow chain call methods. + */ public ButtonAction setUrl(String url) { this.url = url; return this; } + /** + * Sets an Emoji for this button. + * @param emoji the emoji which should be used. + * @return the current instance in order to allow chain call methods. + */ public ButtonAction setEmoji(Emoji emoji) { this.emoji = emoji; return this; } + /** + * Returns the compiled button id. Every parameter is seperated with a colon. + * The first argument is always the handler's class path. + * After that, all set params ({@link ButtonAction#addParam(Object)}) get appended to the id. + * @return the compiled button id. + */ public String getCustomId() { StringBuilder id = new StringBuilder(handler.getName()); for (String param : params) { @@ -58,21 +98,49 @@ public String getCustomId() { return id.toString(); } - public ButtonStyle getButtonStyle() { return buttonStyle; } + /** + * Returns the button's {@link ButtonStyle}. + * @return the button's {@link ButtonStyle}. + */ + public ButtonStyle getButtonStyle() { + return buttonStyle; + } - public String getLabel() { return label; } + /** + * Returns the button's label. + * @return the button's label + */ + public String getLabel() { + return label; + } - public Class getHandler() { return handler; } + /** + * Returns the button's handler class. + * @return The handler class + */ + public Class getHandler() { + return handler; + } - public List getParams() { return params; } + /** + * Returns a list with all parameters of the button. + * @return A List with all parameters. + */ + public List getParams() { + return params; + } + /** + * Returns the complete button. + * @return the compiled button. + */ public Button getButton() { var builder = new ButtonBuilder() .setLabel(label) .setStyle(buttonStyle) .setDisabled(disabled); if (buttonStyle != ButtonStyle.LINK) builder.setCustomId(getCustomId()); - if (url != null && !url.isBlank() && !url.isEmpty() && buttonStyle == ButtonStyle.LINK) builder.setUrl(url); + if (url != null && url.length() > 0 && buttonStyle == ButtonStyle.LINK) builder.setUrl(url); if (emoji != null) builder.setEmoji(emoji); return builder.build(); diff --git a/src/main/java/net/javadiscord/javabot2/command/interaction/button/ButtonHandler.java b/src/main/java/net/javadiscord/javabot2/command/interaction/button/ButtonHandler.java index 0c62f95..c64b037 100644 --- a/src/main/java/net/javadiscord/javabot2/command/interaction/button/ButtonHandler.java +++ b/src/main/java/net/javadiscord/javabot2/command/interaction/button/ButtonHandler.java @@ -4,6 +4,10 @@ import org.javacord.api.interaction.ButtonInteraction; import org.javacord.api.interaction.callback.InteractionImmediateResponseBuilder; +/** + * An interface that should be implemented by any class that is utilizing + * button interactions. + */ public interface ButtonHandler { InteractionImmediateResponseBuilder handleButtonInteraction(ButtonInteraction interaction) throws ResponseException; } diff --git a/src/main/java/net/javadiscord/javabot2/command/interaction/selection_menu/SelectMenuAction.java b/src/main/java/net/javadiscord/javabot2/command/interaction/selection_menu/SelectMenuAction.java index 01c9828..221393e 100644 --- a/src/main/java/net/javadiscord/javabot2/command/interaction/selection_menu/SelectMenuAction.java +++ b/src/main/java/net/javadiscord/javabot2/command/interaction/selection_menu/SelectMenuAction.java @@ -1,6 +1,5 @@ package net.javadiscord.javabot2.command.interaction.selection_menu; -import org.javacord.api.entity.message.component.ActionRow; import org.javacord.api.entity.message.component.SelectMenu; import org.javacord.api.entity.message.component.SelectMenuBuilder; import org.javacord.api.entity.message.component.SelectMenuOption; @@ -9,72 +8,135 @@ import java.util.Arrays; import java.util.List; +/** + * Class that represents a single Select Menu Interaction. + */ public class SelectMenuAction { private Class handler; private List options; - private String placeholder = "Make a selection"; + private String placeholder; private int minValue = 0; private int maxValue = 25; private boolean disabled = false; + /** + * Constructor of the Select Menu Action. + */ public SelectMenuAction() { this.options = new ArrayList<>(); } - public SelectMenuAction setMaxValue(int maxValue) { - this.maxValue = maxValue; - return this; - } - - public SelectMenuAction setMinValue(int minValue) { - this.minValue = minValue; + /** + * Sets a handler class for this select menu interaction. + * The class must extend {@link SelectionMenuHandler}. + * @param handler a class that should handle the select menu interaction. + * @return the current instance in order to allow chain call methods. + */ + public SelectMenuAction handledBy(Class handler) { + this.handler = handler; return this; } + /** + * Sets the select menu's placeholder text. + * @param placeholder the select menu's placeholder. + * @return the current instance in order to allow chain call methods. + */ public SelectMenuAction setPlaceholder(String placeholder) { this.placeholder = placeholder; return this; } + /** + * Enables/Disables the select menu based on the provided boolean. + * @param disabled the boolean which enables/disables the select menu. + * @return the current instance in order to allow chain call methods. + */ public SelectMenuAction setDisabled(boolean disabled) { this.disabled = disabled; return this; } - public SelectMenuAction handledBy(Class handler) { - this.handler = handler; + /** + * Sets the min value for select menu options. + * @param minValue the min value for select menu options. + * @return the current instance in order to allow chain call methods. + */ + public SelectMenuAction setMinValue(int minValue) { + this.minValue = minValue; + return this; + } + + /** + * Sets the max value for select menu options. + * @param maxValue the max value for select menu options. + * @return the current instance in order to allow chain call methods. + */ + public SelectMenuAction setMaxValue(int maxValue) { + this.maxValue = maxValue; return this; } + /** + * Adds a single option to the select menu. + * @param option a single {@link SelectMenuOption}. + * @return the current instance in order to allow chain call methods. + */ public SelectMenuAction addOption(SelectMenuOption option) { options.add(option); return this; } + /** + * Adds multiple options to the select menu. + * @param options an array with all {@link SelectMenuOption}s. + * @return the current instance in order to allow chain call methods. + */ public SelectMenuAction addOptions(SelectMenuOption... options) { this.options.addAll(Arrays.asList(options)); return this; } + /** + * Returns the select menu's custom id. + * (which is just the path of the handler class.) + * @return the custom id. + */ public String getCustomId() { return handler.getName(); } - public Class getHandler() { return handler; } + /** + * Returns the select menu's handler class. + * @return the select menu's handler class. + */ + public Class getHandler() { + return handler; + } - public List getOptions() { return options; } + /** + * Returns a list with all options. + * @return a list with all options. + */ + public List getOptions() { + return options; + } + /** + * Returns the complete select menu. + * @return the complete select menu. + */ public SelectMenu getSelectMenu() { if (options.isEmpty()) throw new IllegalStateException("SelectMenu options may not be empty!"); if (minValue > maxValue) throw new IllegalArgumentException("minValue may not be greater than maxValue!"); - return new SelectMenuBuilder() + var builder = new SelectMenuBuilder() .setCustomId(getCustomId()) - .setPlaceholder(placeholder) .setMinimumValues(minValue) .setMaximumValues(Math.min(maxValue, options.size())) // maxValue cannot be greater than the provided amount of options .addOptions(options) - .setDisabled(disabled) - .build(); + .setDisabled(disabled); + if (placeholder != null && placeholder.length() > 0) builder.setPlaceholder(placeholder); + return builder.build(); } } diff --git a/src/main/java/net/javadiscord/javabot2/command/interaction/selection_menu/SelectionMenuHandler.java b/src/main/java/net/javadiscord/javabot2/command/interaction/selection_menu/SelectionMenuHandler.java index fc7e628..a054fcd 100644 --- a/src/main/java/net/javadiscord/javabot2/command/interaction/selection_menu/SelectionMenuHandler.java +++ b/src/main/java/net/javadiscord/javabot2/command/interaction/selection_menu/SelectionMenuHandler.java @@ -4,6 +4,10 @@ import org.javacord.api.interaction.SelectMenuInteraction; import org.javacord.api.interaction.callback.InteractionImmediateResponseBuilder; +/** + * An interface that should be implemented by any class that is utilizing + * select menu interactions. + */ public interface SelectionMenuHandler { InteractionImmediateResponseBuilder handleSelectMenuInteraction(SelectMenuInteraction interaction) throws ResponseException; } From e6ba92a9cb220f20e0b9124dc5de7b398a976ae8 Mon Sep 17 00:00:00 2001 From: Dynxsty Date: Sat, 13 Nov 2021 16:12:48 +0100 Subject: [PATCH 4/6] fixed indentation --- .../InteractionHandlerBuilder.java | 66 ++--- .../interaction/InteractionListener.java | 74 ++--- .../interaction/button/ButtonAction.java | 264 +++++++++--------- .../interaction/button/ButtonHandler.java | 2 +- .../selection_menu/SelectMenuAction.java | 252 ++++++++--------- .../selection_menu/SelectionMenuHandler.java | 2 +- 6 files changed, 330 insertions(+), 330 deletions(-) diff --git a/src/main/java/net/javadiscord/javabot2/command/interaction/InteractionHandlerBuilder.java b/src/main/java/net/javadiscord/javabot2/command/interaction/InteractionHandlerBuilder.java index 298f542..8c1cf0e 100644 --- a/src/main/java/net/javadiscord/javabot2/command/interaction/InteractionHandlerBuilder.java +++ b/src/main/java/net/javadiscord/javabot2/command/interaction/InteractionHandlerBuilder.java @@ -13,41 +13,41 @@ * Class that is used to build message interactions. */ public class InteractionHandlerBuilder { - private final InteractionImmediateResponseBuilder responseBuilder; - private final List buttons = new ArrayList<>(); - private final List selectMenus = new ArrayList<>(); + private final InteractionImmediateResponseBuilder responseBuilder; + private final List buttons = new ArrayList<>(); + private final List selectMenus = new ArrayList<>(); - public InteractionHandlerBuilder(InteractionImmediateResponseBuilder responseBuilder) { - this.responseBuilder = responseBuilder; - } + public InteractionHandlerBuilder(InteractionImmediateResponseBuilder responseBuilder) { + this.responseBuilder = responseBuilder; + } - /** - * Adds one or multiple {@link ButtonAction}s. - * @param buttonActions an array of {@link ButtonAction}s. - * @return the current instance in order to allow chain call methods. - */ - public InteractionHandlerBuilder addButtons(ButtonAction... buttonActions) { - for (var action : buttonActions) buttons.add(action.getButton()); - return this; - } + /** + * Adds one or multiple {@link ButtonAction}s. + * @param buttonActions an array of {@link ButtonAction}s. + * @return the current instance in order to allow chain call methods. + */ + public InteractionHandlerBuilder addButtons(ButtonAction... buttonActions) { + for (var action : buttonActions) buttons.add(action.getButton()); + return this; + } - /** - * Adds one or multiple {@link SelectMenuAction}s. - * @param selectMenuActions an array of {@link SelectMenuAction}s. - * @return the current instance in order to allow chain call methods. - */ - public InteractionHandlerBuilder addSelectionMenus(SelectMenuAction... selectMenuActions) { - for (var action : selectMenuActions) { - selectMenus.add(action.getSelectMenu()); - } - return this; - } + /** + * Adds one or multiple {@link SelectMenuAction}s. + * @param selectMenuActions an array of {@link SelectMenuAction}s. + * @return the current instance in order to allow chain call methods. + */ + public InteractionHandlerBuilder addSelectionMenus(SelectMenuAction... selectMenuActions) { + for (var action : selectMenuActions) { + selectMenus.add(action.getSelectMenu()); + } + return this; + } - /** - * Returns the provided Response Builder. - * @return the current instance in order to allow chain call methods. - */ - public InteractionImmediateResponseBuilder getResponseBuilder() { - return responseBuilder.addComponents(ActionRow.of(buttons), ActionRow.of(selectMenus)); - } + /** + * Returns the provided Response Builder. + * @return the current instance in order to allow chain call methods. + */ + public InteractionImmediateResponseBuilder getResponseBuilder() { + return responseBuilder.addComponents(ActionRow.of(buttons), ActionRow.of(selectMenus)); + } } diff --git a/src/main/java/net/javadiscord/javabot2/command/interaction/InteractionListener.java b/src/main/java/net/javadiscord/javabot2/command/interaction/InteractionListener.java index 0bf373d..676ad73 100644 --- a/src/main/java/net/javadiscord/javabot2/command/interaction/InteractionListener.java +++ b/src/main/java/net/javadiscord/javabot2/command/interaction/InteractionListener.java @@ -15,43 +15,43 @@ */ public class InteractionListener implements ButtonClickListener, SelectMenuChooseListener { - @Override - public void onButtonClick(ButtonClickEvent event) { - var id = event.getButtonInteraction().getCustomId().split(":"); - ButtonHandler handler = (ButtonHandler) getHandlerByName(id[0]); - try { - handler.handleButtonInteraction(event.getButtonInteraction()).respond(); - } catch (ResponseException e) { - e.printStackTrace(); - } - } + @Override + public void onButtonClick(ButtonClickEvent event) { + var id = event.getButtonInteraction().getCustomId().split(":"); + ButtonHandler handler = (ButtonHandler) getHandlerByName(id[0]); + try { + handler.handleButtonInteraction(event.getButtonInteraction()).respond(); + } catch (ResponseException e) { + e.printStackTrace(); + } + } - @Override - public void onSelectMenuChoose(SelectMenuChooseEvent event) { - var id = event.getSelectMenuInteraction().getCustomId().split(":"); - SelectionMenuHandler handler = (SelectionMenuHandler) getHandlerByName(id[0]); - try { - handler.handleSelectMenuInteraction(event.getSelectMenuInteraction()).respond(); - } catch (ResponseException e) { - e.printStackTrace(); - } - } + @Override + public void onSelectMenuChoose(SelectMenuChooseEvent event) { + var id = event.getSelectMenuInteraction().getCustomId().split(":"); + SelectionMenuHandler handler = (SelectionMenuHandler) getHandlerByName(id[0]); + try { + handler.handleSelectMenuInteraction(event.getSelectMenuInteraction()).respond(); + } catch (ResponseException e) { + e.printStackTrace(); + } + } - /** - * Tries to get a Class by the specified name - * (Class paths are baked into the button id) and returns it. - * @param name the class name - * @return The handler class - */ - private Object getHandlerByName(String name) { - try { - return Class.forName(name) - .getDeclaredConstructor() - .newInstance(); - } catch (ClassNotFoundException | NoSuchMethodException | InstantiationException | - IllegalAccessException | InvocationTargetException e) { - e.printStackTrace(); - return null; - } - } + /** + * Tries to get a Class by the specified name + * (Class paths are baked into the button id) and returns it. + * @param name the class name + * @return The handler class + */ + private Object getHandlerByName(String name) { + try { + return Class.forName(name) + .getDeclaredConstructor() + .newInstance(); + } catch (ClassNotFoundException | NoSuchMethodException | InstantiationException | + IllegalAccessException | InvocationTargetException e) { + e.printStackTrace(); + return null; + } + } } diff --git a/src/main/java/net/javadiscord/javabot2/command/interaction/button/ButtonAction.java b/src/main/java/net/javadiscord/javabot2/command/interaction/button/ButtonAction.java index 7c83fc8..18374bd 100644 --- a/src/main/java/net/javadiscord/javabot2/command/interaction/button/ButtonAction.java +++ b/src/main/java/net/javadiscord/javabot2/command/interaction/button/ButtonAction.java @@ -13,136 +13,136 @@ */ public class ButtonAction { - private final String label; - private final ButtonStyle buttonStyle; - private Class handler; - private final List params; - private boolean disabled = false; - private String url; - private Emoji emoji; - - /** - * Constructor of the Button Action. - * @param label the button's label - * @param buttonStyle the button's {@link ButtonStyle} - */ - public ButtonAction(String label, ButtonStyle buttonStyle) { - this.params = new ArrayList<>(); - this.label = label; - this.buttonStyle = buttonStyle; - } - - /** - * Sets a handler class for this button interaction. - * The class must extend {@link ButtonHandler}. - * @param handler a class that should handle the button interaction. - * @return the current instance in order to allow chain call methods. - */ - public ButtonAction handledBy(Class handler) { - this.handler = handler; - return this; - } - - /** - * Adds a parameter, which is later baked into the button id. - * @param param an object that is later converted to a string to bake it into the button id as a parameter. - * @return the current instance in order to allow chain call methods. - */ - public ButtonAction addParam(Object param) { - params.add(String.valueOf(param)); - return this; - } - - /** - * Enables/Disables the button based on the provided boolean. - * @param disabled the boolean which enables/disables the button. - * @return the current instance in order to allow chain call methods. - */ - public ButtonAction setDisabled(boolean disabled) { - this.disabled = disabled; - return this; - } - - /** - * Sets an Url for this button. - * This only works for buttons with the {@link ButtonStyle#LINK} button style. - * @param url the url as a string. - * @return the current instance in order to allow chain call methods. - */ - public ButtonAction setUrl(String url) { - this.url = url; - return this; - } - - /** - * Sets an Emoji for this button. - * @param emoji the emoji which should be used. - * @return the current instance in order to allow chain call methods. - */ - public ButtonAction setEmoji(Emoji emoji) { - this.emoji = emoji; - return this; - } - - /** - * Returns the compiled button id. Every parameter is seperated with a colon. - * The first argument is always the handler's class path. - * After that, all set params ({@link ButtonAction#addParam(Object)}) get appended to the id. - * @return the compiled button id. - */ - public String getCustomId() { - StringBuilder id = new StringBuilder(handler.getName()); - for (String param : params) { - id.append(":").append(param); - } - return id.toString(); - } - - /** - * Returns the button's {@link ButtonStyle}. - * @return the button's {@link ButtonStyle}. - */ - public ButtonStyle getButtonStyle() { - return buttonStyle; - } - - /** - * Returns the button's label. - * @return the button's label - */ - public String getLabel() { - return label; - } - - /** - * Returns the button's handler class. - * @return The handler class - */ - public Class getHandler() { - return handler; - } - - /** - * Returns a list with all parameters of the button. - * @return A List with all parameters. - */ - public List getParams() { - return params; - } - - /** - * Returns the complete button. - * @return the compiled button. - */ - public Button getButton() { - var builder = new ButtonBuilder() - .setLabel(label) - .setStyle(buttonStyle) - .setDisabled(disabled); - if (buttonStyle != ButtonStyle.LINK) builder.setCustomId(getCustomId()); - if (url != null && url.length() > 0 && buttonStyle == ButtonStyle.LINK) builder.setUrl(url); - if (emoji != null) builder.setEmoji(emoji); - - return builder.build(); - } + private final String label; + private final ButtonStyle buttonStyle; + private Class handler; + private final List params; + private boolean disabled = false; + private String url; + private Emoji emoji; + + /** + * Constructor of the Button Action. + * @param label the button's label + * @param buttonStyle the button's {@link ButtonStyle} + */ + public ButtonAction(String label, ButtonStyle buttonStyle) { + this.params = new ArrayList<>(); + this.label = label; + this.buttonStyle = buttonStyle; + } + + /** + * Sets a handler class for this button interaction. + * The class must extend {@link ButtonHandler}. + * @param handler a class that should handle the button interaction. + * @return the current instance in order to allow chain call methods. + */ + public ButtonAction handledBy(Class handler) { + this.handler = handler; + return this; + } + + /** + * Adds a parameter, which is later baked into the button id. + * @param param an object that is later converted to a string to bake it into the button id as a parameter. + * @return the current instance in order to allow chain call methods. + */ + public ButtonAction addParam(Object param) { + params.add(String.valueOf(param)); + return this; + } + + /** + * Enables/Disables the button based on the provided boolean. + * @param disabled the boolean which enables/disables the button. + * @return the current instance in order to allow chain call methods. + */ + public ButtonAction setDisabled(boolean disabled) { + this.disabled = disabled; + return this; + } + + /** + * Sets an Url for this button. + * This only works for buttons with the {@link ButtonStyle#LINK} button style. + * @param url the url as a string. + * @return the current instance in order to allow chain call methods. + */ + public ButtonAction setUrl(String url) { + this.url = url; + return this; + } + + /** + * Sets an Emoji for this button. + * @param emoji the emoji which should be used. + * @return the current instance in order to allow chain call methods. + */ + public ButtonAction setEmoji(Emoji emoji) { + this.emoji = emoji; + return this; + } + + /** + * Returns the compiled button id. Every parameter is seperated with a colon. + * The first argument is always the handler's class path. + * After that, all set params ({@link ButtonAction#addParam(Object)}) get appended to the id. + * @return the compiled button id. + */ + public String getCustomId() { + StringBuilder id = new StringBuilder(handler.getName()); + for (String param : params) { + id.append(":").append(param); + } + return id.toString(); + } + + /** + * Returns the button's {@link ButtonStyle}. + * @return the button's {@link ButtonStyle}. + */ + public ButtonStyle getButtonStyle() { + return buttonStyle; + } + + /** + * Returns the button's label. + * @return the button's label + */ + public String getLabel() { + return label; + } + + /** + * Returns the button's handler class. + * @return The handler class + */ + public Class getHandler() { + return handler; + } + + /** + * Returns a list with all parameters of the button. + * @return A List with all parameters. + */ + public List getParams() { + return params; + } + + /** + * Returns the complete button. + * @return the compiled button. + */ + public Button getButton() { + var builder = new ButtonBuilder() + .setLabel(label) + .setStyle(buttonStyle) + .setDisabled(disabled); + if (buttonStyle != ButtonStyle.LINK) builder.setCustomId(getCustomId()); + if (url != null && url.length() > 0 && buttonStyle == ButtonStyle.LINK) builder.setUrl(url); + if (emoji != null) builder.setEmoji(emoji); + + return builder.build(); + } } diff --git a/src/main/java/net/javadiscord/javabot2/command/interaction/button/ButtonHandler.java b/src/main/java/net/javadiscord/javabot2/command/interaction/button/ButtonHandler.java index c64b037..d94bd5f 100644 --- a/src/main/java/net/javadiscord/javabot2/command/interaction/button/ButtonHandler.java +++ b/src/main/java/net/javadiscord/javabot2/command/interaction/button/ButtonHandler.java @@ -9,5 +9,5 @@ * button interactions. */ public interface ButtonHandler { - InteractionImmediateResponseBuilder handleButtonInteraction(ButtonInteraction interaction) throws ResponseException; + InteractionImmediateResponseBuilder handleButtonInteraction(ButtonInteraction interaction) throws ResponseException; } diff --git a/src/main/java/net/javadiscord/javabot2/command/interaction/selection_menu/SelectMenuAction.java b/src/main/java/net/javadiscord/javabot2/command/interaction/selection_menu/SelectMenuAction.java index 221393e..da97923 100644 --- a/src/main/java/net/javadiscord/javabot2/command/interaction/selection_menu/SelectMenuAction.java +++ b/src/main/java/net/javadiscord/javabot2/command/interaction/selection_menu/SelectMenuAction.java @@ -13,130 +13,130 @@ */ public class SelectMenuAction { - private Class handler; - private List options; - private String placeholder; - private int minValue = 0; - private int maxValue = 25; - private boolean disabled = false; - - /** - * Constructor of the Select Menu Action. - */ - public SelectMenuAction() { - this.options = new ArrayList<>(); - } - - /** - * Sets a handler class for this select menu interaction. - * The class must extend {@link SelectionMenuHandler}. - * @param handler a class that should handle the select menu interaction. - * @return the current instance in order to allow chain call methods. - */ - public SelectMenuAction handledBy(Class handler) { - this.handler = handler; - return this; - } - - /** - * Sets the select menu's placeholder text. - * @param placeholder the select menu's placeholder. - * @return the current instance in order to allow chain call methods. - */ - public SelectMenuAction setPlaceholder(String placeholder) { - this.placeholder = placeholder; - return this; - } - - /** - * Enables/Disables the select menu based on the provided boolean. - * @param disabled the boolean which enables/disables the select menu. - * @return the current instance in order to allow chain call methods. - */ - public SelectMenuAction setDisabled(boolean disabled) { - this.disabled = disabled; - return this; - } - - /** - * Sets the min value for select menu options. - * @param minValue the min value for select menu options. - * @return the current instance in order to allow chain call methods. - */ - public SelectMenuAction setMinValue(int minValue) { - this.minValue = minValue; - return this; - } - - /** - * Sets the max value for select menu options. - * @param maxValue the max value for select menu options. - * @return the current instance in order to allow chain call methods. - */ - public SelectMenuAction setMaxValue(int maxValue) { - this.maxValue = maxValue; - return this; - } - - /** - * Adds a single option to the select menu. - * @param option a single {@link SelectMenuOption}. - * @return the current instance in order to allow chain call methods. - */ - public SelectMenuAction addOption(SelectMenuOption option) { - options.add(option); - return this; - } - - /** - * Adds multiple options to the select menu. - * @param options an array with all {@link SelectMenuOption}s. - * @return the current instance in order to allow chain call methods. - */ - public SelectMenuAction addOptions(SelectMenuOption... options) { - this.options.addAll(Arrays.asList(options)); - return this; - } - - /** - * Returns the select menu's custom id. - * (which is just the path of the handler class.) - * @return the custom id. - */ - public String getCustomId() { - return handler.getName(); - } - - /** - * Returns the select menu's handler class. - * @return the select menu's handler class. - */ - public Class getHandler() { - return handler; - } - - /** - * Returns a list with all options. - * @return a list with all options. - */ - public List getOptions() { - return options; - } - - /** - * Returns the complete select menu. - * @return the complete select menu. - */ - public SelectMenu getSelectMenu() { - if (options.isEmpty()) throw new IllegalStateException("SelectMenu options may not be empty!"); - if (minValue > maxValue) throw new IllegalArgumentException("minValue may not be greater than maxValue!"); - var builder = new SelectMenuBuilder() - .setCustomId(getCustomId()) - .setMinimumValues(minValue) - .setMaximumValues(Math.min(maxValue, options.size())) // maxValue cannot be greater than the provided amount of options - .addOptions(options) - .setDisabled(disabled); - if (placeholder != null && placeholder.length() > 0) builder.setPlaceholder(placeholder); - return builder.build(); - } + private Class handler; + private List options; + private String placeholder; + private int minValue = 0; + private int maxValue = 25; + private boolean disabled = false; + + /** + * Constructor of the Select Menu Action. + */ + public SelectMenuAction() { + this.options = new ArrayList<>(); + } + + /** + * Sets a handler class for this select menu interaction. + * The class must extend {@link SelectionMenuHandler}. + * @param handler a class that should handle the select menu interaction. + * @return the current instance in order to allow chain call methods. + */ + public SelectMenuAction handledBy(Class handler) { + this.handler = handler; + return this; + } + + /** + * Sets the select menu's placeholder text. + * @param placeholder the select menu's placeholder. + * @return the current instance in order to allow chain call methods. + */ + public SelectMenuAction setPlaceholder(String placeholder) { + this.placeholder = placeholder; + return this; + } + + /** + * Enables/Disables the select menu based on the provided boolean. + * @param disabled the boolean which enables/disables the select menu. + * @return the current instance in order to allow chain call methods. + */ + public SelectMenuAction setDisabled(boolean disabled) { + this.disabled = disabled; + return this; + } + + /** + * Sets the min value for select menu options. + * @param minValue the min value for select menu options. + * @return the current instance in order to allow chain call methods. + */ + public SelectMenuAction setMinValue(int minValue) { + this.minValue = minValue; + return this; + } + + /** + * Sets the max value for select menu options. + * @param maxValue the max value for select menu options. + * @return the current instance in order to allow chain call methods. + */ + public SelectMenuAction setMaxValue(int maxValue) { + this.maxValue = maxValue; + return this; + } + + /** + * Adds a single option to the select menu. + * @param option a single {@link SelectMenuOption}. + * @return the current instance in order to allow chain call methods. + */ + public SelectMenuAction addOption(SelectMenuOption option) { + options.add(option); + return this; + } + + /** + * Adds multiple options to the select menu. + * @param options an array with all {@link SelectMenuOption}s. + * @return the current instance in order to allow chain call methods. + */ + public SelectMenuAction addOptions(SelectMenuOption... options) { + this.options.addAll(Arrays.asList(options)); + return this; + } + + /** + * Returns the select menu's custom id. + * (which is just the path of the handler class.) + * @return the custom id. + */ + public String getCustomId() { + return handler.getName(); + } + + /** + * Returns the select menu's handler class. + * @return the select menu's handler class. + */ + public Class getHandler() { + return handler; + } + + /** + * Returns a list with all options. + * @return a list with all options. + */ + public List getOptions() { + return options; + } + + /** + * Returns the complete select menu. + * @return the complete select menu. + */ + public SelectMenu getSelectMenu() { + if (options.isEmpty()) throw new IllegalStateException("SelectMenu options may not be empty!"); + if (minValue > maxValue) throw new IllegalArgumentException("minValue may not be greater than maxValue!"); + var builder = new SelectMenuBuilder() + .setCustomId(getCustomId()) + .setMinimumValues(minValue) + .setMaximumValues(Math.min(maxValue, options.size())) // maxValue cannot be greater than the provided amount of options + .addOptions(options) + .setDisabled(disabled); + if (placeholder != null && placeholder.length() > 0) builder.setPlaceholder(placeholder); + return builder.build(); + } } diff --git a/src/main/java/net/javadiscord/javabot2/command/interaction/selection_menu/SelectionMenuHandler.java b/src/main/java/net/javadiscord/javabot2/command/interaction/selection_menu/SelectionMenuHandler.java index a054fcd..254ed29 100644 --- a/src/main/java/net/javadiscord/javabot2/command/interaction/selection_menu/SelectionMenuHandler.java +++ b/src/main/java/net/javadiscord/javabot2/command/interaction/selection_menu/SelectionMenuHandler.java @@ -9,5 +9,5 @@ * select menu interactions. */ public interface SelectionMenuHandler { - InteractionImmediateResponseBuilder handleSelectMenuInteraction(SelectMenuInteraction interaction) throws ResponseException; + InteractionImmediateResponseBuilder handleSelectMenuInteraction(SelectMenuInteraction interaction) throws ResponseException; } From e5c09d3dd5740fbd8f6971b8a2a9492cc24bfd61 Mon Sep 17 00:00:00 2001 From: Dynxsty Date: Sat, 13 Nov 2021 16:56:51 +0100 Subject: [PATCH 5/6] added missing javadoc --- src/main/java/net/javadiscord/javabot2/Bot.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/java/net/javadiscord/javabot2/Bot.java b/src/main/java/net/javadiscord/javabot2/Bot.java index b39e083..7f5b434 100644 --- a/src/main/java/net/javadiscord/javabot2/Bot.java +++ b/src/main/java/net/javadiscord/javabot2/Bot.java @@ -86,6 +86,11 @@ public static void main(String[] args) { initScheduledTasks(api); } + /** + * Adds all the bot's event listeners to the JDA instance, except for the + * main {@link SlashCommandListener}. + * @param api the {@link DiscordApi} instance to add listeners to. + */ private static void addEventListeners(DiscordApi api) { api.addButtonClickListener(new InteractionListener()); api.addSelectMenuChooseListener(new InteractionListener()); From 70c1d51aa8ddadff5b83c9a78b6debaba3309616 Mon Sep 17 00:00:00 2001 From: Dynxsty Date: Sat, 13 Nov 2021 16:57:56 +0100 Subject: [PATCH 6/6] replaced JDA typo (lol) with Javacord --- src/main/java/net/javadiscord/javabot2/Bot.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/net/javadiscord/javabot2/Bot.java b/src/main/java/net/javadiscord/javabot2/Bot.java index 7f5b434..2e9294b 100644 --- a/src/main/java/net/javadiscord/javabot2/Bot.java +++ b/src/main/java/net/javadiscord/javabot2/Bot.java @@ -87,7 +87,7 @@ public static void main(String[] args) { } /** - * Adds all the bot's event listeners to the JDA instance, except for the + * Adds all the bot's event listeners to the Javacord instance, except for the * main {@link SlashCommandListener}. * @param api the {@link DiscordApi} instance to add listeners to. */