Skip to content

Commit

Permalink
refactor reply
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaktushose committed Dec 4, 2024
1 parent 3a34330 commit 46fa079
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

/**
* Classes annotated with Interaction will be scanned at startup and are eligible for defining interactions such as
* slash commands, buttons, modals or context menus.
* slash commands, buttonContainers, modals or context menus.
*
* @since 4.0.0
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ private Optional<InteractionRuntime> getRuntime(String interactionId) {

/**
* A runtime used for executing interactions. This class holds the instance of the class annotated with
* {@link Interaction Interaction} where commands, buttons, etc. live in. This runtime can only be used once per
* {@link Interaction Interaction} where commands, buttonContainers, etc. live in. This runtime can only be used once per
* command execution.
*
* @since 4.0.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ default void reply(@NotNull EmbedDTO embedDTO, @Nullable Consumer<Message> succe

/**
* Adds an {@link ActionRow} to the reply and adds the passed {@link Component Components} to it.
* For buttons, they must be defined in the same
* For buttonContainers, they must be defined in the same
* {@link com.github.kaktushose.jda.commands.annotations.interactions.Interaction Interaction} as the referring
* {@link SlashCommand Command}.
*
Expand All @@ -159,15 +159,15 @@ default Replyable with(@NotNull Component... components) {
for (Component component : components) {
if (component instanceof Buttons) {
Buttons buttons = (Buttons) component;
buttons.getButtonContainer().forEach(container -> {
String id = String.format("%s%s", context.getInteractionDefinition().getMethod().getDeclaringClass().getSimpleName(), container.getName());
buttons.buttonContainers().forEach(container -> {
String id = String.format("%s%s", context.getInteractionDefinition().getMethod().getDeclaringClass().getSimpleName(), container.name());
context.getInteractionRegistry().getButtons()
.stream()
.filter(it -> it.getDefinitionId().equals(id))
.findFirst()
.map(it -> {
Button jdaButton = it.toButton().withDisabled(!container.isEnabled());
//only assign ids to non-link buttons
Button jdaButton = it.toButton().withDisabled(!container.enabled());
//only assign ids to non-link buttonContainers
if (jdaButton.getUrl() == null) {
jdaButton = jdaButton.withId(it.createCustomId(context.getRuntime().getRuntimeId()));
}
Expand All @@ -177,7 +177,7 @@ default Replyable with(@NotNull Component... components) {
}
if (component instanceof SelectMenus) {
SelectMenus menus = (SelectMenus) component;
menus.getSelectMenuContainer().forEach(container -> {
menus.selectMenuContainers().forEach(container -> {
String id = String.format("%s%s", context.getInteractionDefinition().getMethod().getDeclaringClass().getSimpleName(), container.getName());
context.getInteractionRegistry().getSelectMenus()
.stream()
Expand Down Expand Up @@ -212,12 +212,12 @@ default KeyValueStore kv() {

/**
* Adds an {@link ActionRow} to the reply and adds the passed {@link Component Components} to it.
* The buttons must be defined in the same
* The buttonContainers must be defined in the same
* {@link com.github.kaktushose.jda.commands.annotations.interactions.Interaction Interaction} as the referring
* {@link SlashCommand Command}. This will enable all buttons. To add
* disabled buttons, use {@link #with(Component...)}.
* {@link SlashCommand Command}. This will enable all buttonContainers. To add
* disabled buttonContainers, use {@link #with(Component...)}.
*
* @param buttons the id of the buttons to add
* @param buttons the id of the buttonContainers to add
* @return the current instance for fluent interface
*/
default Replyable withButtons(@NotNull String... buttons) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,27 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
* {@link Component} implementation for buttons. This class can be used to add
* {@link Component} implementation for buttonContainers. This class can be used to add
* {@link com.github.kaktushose.jda.commands.annotations.interactions.Button Buttons} to messages while defining their
* state (enabled or disabled).
*
* @see Replyable#with(Component...)
* @since 2.3.0
*/
public class Buttons implements Component {
public record Buttons(Collection<ButtonContainer> buttonContainers) implements Component {

private final Collection<ButtonContainer> buttons;

private Buttons(Collection<ButtonContainer> buttons) {
this.buttons = buttons;
public Buttons(Collection<ButtonContainer> buttonContainers) {
this.buttonContainers = Collections.unmodifiableCollection(buttonContainers);
}

/**
* Add the buttons with the given ids to the reply message as enabled.
* Add the buttonContainers with the given ids to the reply message as enabled.
*
* @param buttons the id of the buttons to add
* @param buttons the id of the buttonContainers to add
* @return instance of this class used inside the
* {@link com.github.kaktushose.jda.commands.dispatching.reply.ReplyContext}
*/
Expand All @@ -34,9 +33,9 @@ public static Buttons enabled(String... buttons) {
}

/**
* Add the buttons with the given ids to the reply message as disabled.
* Add the buttonContainers with the given ids to the reply message as disabled.
*
* @param buttons the id of the buttons to add
* @param buttons the id of the buttonContainers to add
* @return instance of this class used inside the
* {@link com.github.kaktushose.jda.commands.dispatching.reply.ReplyContext}
*/
Expand All @@ -52,43 +51,8 @@ private static Buttons build(boolean enabled, String... buttons) {
return new Buttons(result);
}

/**
* Gets the {@link ButtonContainer}.
*
* @return the {@link ButtonContainer}
*/
public Collection<ButtonContainer> getButtonContainer() {
return buttons;
}

/**
* Contains information about a single {@link com.github.kaktushose.jda.commands.annotations.interactions.Button Button}.
*/
public static class ButtonContainer {
private final String name;
private final boolean enabled;

private ButtonContainer(String name, boolean enabled) {
this.name = name;
this.enabled = enabled;
}

/**
* Gets the button id.
*
* @return the button id
*/
public String getName() {
return name;
}

/**
* Whether the button is enabled or not.
*
* @return {@code true} if the button is enabled
*/
public boolean isEnabled() {
return enabled;
}
}
public record ButtonContainer(String name, boolean enabled) { }
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
* @see SelectMenus
* @since 2.3.0
*/
public interface Component {
public sealed interface Component permits Buttons, SelectMenus {
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
Expand All @@ -15,12 +16,10 @@
* @see Replyable#with(Component...)
* @since 2.3.0
*/
public class SelectMenus implements Component {
public record SelectMenus(Collection<SelectMenus.SelectMenuContainer> selectMenuContainers) implements Component {

private final Collection<SelectMenus.SelectMenuContainer> selectMenus;

private SelectMenus(Collection<SelectMenus.SelectMenuContainer> selectMenus) {
this.selectMenus = selectMenus;
public SelectMenus(Collection<SelectMenus.SelectMenuContainer> selectMenuContainers) {
this.selectMenuContainers = Collections.unmodifiableCollection(selectMenuContainers);
}

/**
Expand Down Expand Up @@ -53,15 +52,6 @@ private static SelectMenus build(boolean enabled, String... menus) {
return new SelectMenus(result);
}

/**
* Gets the {@link SelectMenuContainer}.
*
* @return the {@link SelectMenuContainer}
*/
public Collection<SelectMenus.SelectMenuContainer> getSelectMenuContainer() {
return selectMenus;
}

/**
* Contains information about a single select menu (either StringSelectMenu or EntitySelectMenu).
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ public Collection<GenericCommandDefinition> getCommands() {
}

/**
* Gets a possibly-empty list of all buttons.
* Gets a possibly-empty list of all buttonContainers.
*
* @return a possibly-empty list of all buttons
* @return a possibly-empty list of all buttonContainers
*/
public Collection<ButtonDefinition> getButtons() {
return definitions.stream()
Expand Down

0 comments on commit 46fa079

Please sign in to comment.