Visitar URL original
Interaction Framework by jasonlessenich · Pull Request #19 · Java-Discord/JavaBot2 · GitHub
Skip to content
This repository was archived by the owner on Dec 1, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
added some javadoc
  • Loading branch information
jasonlessenich committed Nov 13, 2021
commit 1aafd8cb248ea22d8cf9f3e9bdbe1e5aa1c08910
Original file line number Diff line number Diff line change
Expand Up @@ -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<LowLevelComponent> buttons = new ArrayList<>();
private final List<LowLevelComponent> selectMenus = new ArrayList<>();
Expand All @@ -19,18 +21,32 @@ 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());
}
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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<? extends ButtonHandler> 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) {
Expand All @@ -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<? extends ButtonHandler> getHandler() { return handler; }
/**
* Returns the button's handler class.
* @return The handler class
*/
public Class<? extends ButtonHandler> getHandler() {
return handler;
}

public List<String> getParams() { return params; }
/**
* Returns a list with all parameters of the button.
* @return A List with all parameters.
*/
public List<String> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Loading