Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
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
20 changes: 15 additions & 5 deletions src/main/java/net/dv8tion/jda/api/components/Component.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,15 @@ enum Type
/** A text input field */
TEXT_INPUT(4, false, true),
/** A select menu of users */
USER_SELECT(5, true, false),
USER_SELECT(5, true, true),
/** A select menu of roles */
ROLE_SELECT(6, true, false),
ROLE_SELECT(6, true, true),
/** A select menu of users and roles */
MENTIONABLE_SELECT(7, true, false),
MENTIONABLE_SELECT(7, true, true),
/** A select menu of channels */
CHANNEL_SELECT(8, true, false),
CHANNEL_SELECT(8, true, true),
SECTION(9, true, false),
TEXT_DISPLAY(10, true, false),
TEXT_DISPLAY(10, true, true),
THUMBNAIL(11, true, false),
MEDIA_GALLERY(12, true, false),
FILE_DISPLAY(13, true, false),
Expand Down Expand Up @@ -197,6 +197,16 @@ public boolean isModalCompatible()
return modalCompatible;
}

/**
* Whether this component is an {@link net.dv8tion.jda.api.components.selections.EntitySelectMenu EntitySelectMenu}
*
* @return {@code true} is this is a type of {@link net.dv8tion.jda.api.components.selections.EntitySelectMenu EntitySelectMenu}
*/
public boolean isEntitySelectMenu()
{
return this == MENTIONABLE_SELECT || this == CHANNEL_SELECT || this == USER_SELECT || this == ROLE_SELECT;
}

/**
* Maps the provided type id to the respective enum instance.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* Represents a component that can be added directly to a modal, this includes:
* <ul>
* <li>{@link net.dv8tion.jda.api.components.label.Label Label}</li>
* <li>{@link net.dv8tion.jda.api.components.textdisplay.TextDisplay TextDisplay}</li>
* </ul>
*/
public interface ModalTopLevelComponent extends Component
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
package net.dv8tion.jda.api.components;

import net.dv8tion.jda.api.components.label.Label;
import net.dv8tion.jda.api.components.textdisplay.TextDisplay;

import javax.annotation.Nonnull;

/**
* Represents a union of {@link ModalTopLevelComponent ModalTopLevelComponents} that can be one of:
* <ul>
* <li>{@link Label}</li>
* <li>{@link TextDisplay}</li>
* <li>{@link UnknownComponent}, detectable via {@link #isUnknownComponent()}</li>
* </ul>
*/
Expand Down Expand Up @@ -51,6 +53,28 @@ public interface ModalTopLevelComponentUnion extends ModalTopLevelComponent, ICo
@Nonnull
Label asLabel();

/**
* Casts this union to a {@link TextDisplay}.
* This method exists for developer discoverability.
*
* <p>Note: This is effectively equivalent to using the cast operator:
* <pre><code>
* //These are the same!
* TextDisplay textDisplay = union.asTextDisplay();
* TextDisplay textDisplay2 = (TextDisplay) union;
* </code></pre>
*
* You can use {@link #getType()} to see if the component is of type {@link net.dv8tion.jda.api.components.Component.Type#TEXT_DISPLAY TEXT_DISPLAY} to validate
* whether you can call this method in addition to normal instanceof checks: <code>component instanceof TextDisplay</code>
*
* @throws IllegalStateException
* If the component represented by this union is not actually a {@link TextDisplay}.
*
* @return The component as a {@link TextDisplay}
*/
@Nonnull
TextDisplay asTextDisplay();

@Nonnull
@Override
ModalTopLevelComponentUnion withUniqueId(int uniqueId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
* <ul>
* <li>{@link net.dv8tion.jda.api.components.textinput.TextInput TextInput}</li>
* <li>{@link net.dv8tion.jda.api.components.selections.StringSelectMenu StringSelectMenu}</li>
* <li>{@link net.dv8tion.jda.api.components.selections.EntitySelectMenu EntitySelectMenu}</li>
* </ul>
*/
public interface LabelChildComponent extends Component
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import net.dv8tion.jda.api.components.Component;
import net.dv8tion.jda.api.components.IComponentUnion;
import net.dv8tion.jda.api.components.selections.EntitySelectMenu;
import net.dv8tion.jda.api.components.selections.StringSelectMenu;
import net.dv8tion.jda.api.components.textinput.TextInput;

Expand All @@ -28,6 +29,7 @@
* <ul>
* <li>{@link TextInput}</li>
* <li>{@link StringSelectMenu}</li>
* <li>{@link EntitySelectMenu}</li>
* </ul>
*/
public interface LabelChildComponentUnion extends LabelChildComponent, IComponentUnion
Expand Down Expand Up @@ -76,6 +78,34 @@ public interface LabelChildComponentUnion extends LabelChildComponent, IComponen
@Nonnull
StringSelectMenu asStringSelectMenu();

/**
* Casts this union to a {@link EntitySelectMenu}.
* This method exists for developer discoverability.
*
* <p>Note: This is effectively equivalent to using the cast operator:
* <pre><code>
* //These are the same!
* EntitySelectMenu menu = union.asEntitySelectMenu();
* EntitySelectMenu menu2 = (EntitySelectMenu) union;
* </code></pre>
*
* You can use {@link #getType()} to see if the component is one of:
* <ul>
* <li>{@link Component.Type#USER_SELECT USER_SELECT}</li>
* <li>{@link Component.Type#ROLE_SELECT ROLE_SELECT}</li>
* <li>{@link Component.Type#MENTIONABLE_SELECT MENTIONABLE_SELECT}</li>
* <li>{@link Component.Type#CHANNEL_SELECT CHANNEL_SELECT}</li>
* </ul>
* to validate whether you can call this method in addition to normal instanceof checks: <code>component instanceof EntitySelectMenu</code>
*
* @throws IllegalStateException
* If the component represented by this union is not actually a {@link EntitySelectMenu}.
*
* @return The component as a {@link EntitySelectMenu}
*/
@Nonnull
EntitySelectMenu asEntitySelectMenu();

@Nonnull
@Override
LabelChildComponentUnion withUniqueId(int uniqueId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import net.dv8tion.jda.api.components.ActionComponent;
import net.dv8tion.jda.api.components.Component;
import net.dv8tion.jda.api.components.actionrow.ActionRow;
import net.dv8tion.jda.api.components.label.LabelChildComponent;
import net.dv8tion.jda.api.entities.ISnowflake;
import net.dv8tion.jda.api.entities.Role;
import net.dv8tion.jda.api.entities.UserSnowflake;
Expand Down Expand Up @@ -72,7 +73,7 @@
* @see EntitySelectInteraction
* @see StringSelectMenu
*/
public interface EntitySelectMenu extends SelectMenu
public interface EntitySelectMenu extends SelectMenu, LabelChildComponent
{
@Nonnull
@Override
Expand Down Expand Up @@ -655,7 +656,7 @@ public EntitySelectMenu build()
Checks.check(minValues <= maxValues, "Min values cannot be greater than max values!");
EnumSet<ChannelType> channelTypes = componentType == Type.CHANNEL_SELECT ? this.channelTypes : EnumSet.noneOf(ChannelType.class);
List<DefaultValue> defaultValues = new ArrayList<>(this.defaultValues);
return new EntitySelectMenuImpl(customId, uniqueId, placeholder, minValues, maxValues, disabled, componentType, channelTypes, defaultValues);
return new EntitySelectMenuImpl(customId, uniqueId, placeholder, minValues, maxValues, disabled, componentType, channelTypes, defaultValues, required);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ default SelectMenu asEnabled()
*/
int getMaxValues();

/**
* Whether the user must populate this select menu in Modals, or {@code null} if not set.
*
* @return Whether this menu must be populated, or null
*/
@Nullable
Boolean isRequired();

/**
* Creates a new preconfigured {@link SelectMenu.Builder} with the same settings used for this select menu.
* <br>This can be useful to create an updated version of this menu without needing to rebuild it from scratch.
Expand All @@ -136,6 +144,7 @@ abstract class Builder<T extends SelectMenu, B extends Builder<T, B>>
protected String placeholder;
protected int minValues = 1, maxValues = 1;
protected boolean disabled = false;
protected Boolean required = null;

protected Builder(@Nonnull String customId)
{
Expand Down Expand Up @@ -311,6 +320,24 @@ public B setDisabled(boolean disabled)
return (B) this;
}

/**
* Configure whether the user must populate this select menu if inside a Modal.
* <br>This defaults to {@code true} in Modals when unset.
*
* <p>This only has an effect in Modals!
*
* @param required
* Whether this menu is required
*
* @return The same builder instance for chaining
*/
@Nonnull
public B setRequired(@Nullable Boolean required)
{
this.required = required;
return (B) this;
}

/**
* The custom id used to identify the select menu.
*
Expand Down Expand Up @@ -390,6 +417,17 @@ public boolean isDisabled()
return disabled;
}

/**
* Whether the user must populate this select menu in Modals, or {@code null} if not set.
*
* @return Whether this menu must be populated, or null
*/
@Nullable
public Boolean isRequired()
{
return required;
}

/**
* Creates a new {@link SelectMenu} instance if all requirements are satisfied.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,6 @@ default StringSelectMenu withDisabled(boolean disabled)
@Nonnull
List<SelectOption> getOptions();

/**
* Whether the user must populate this select menu in Modals, or {@code null} if not set.
*
* @return Whether this menu must be populated, or null
*/
@Nullable
Boolean isRequired();

/**
* Creates a new preconfigured {@link Builder} with the same settings used for this select menu.
* <br>This can be useful to create an updated version of this menu without needing to rebuild it from scratch.
Expand Down Expand Up @@ -153,7 +145,6 @@ static Builder create(@Nonnull String customId)
class Builder extends SelectMenu.Builder<StringSelectMenu, StringSelectMenu.Builder>
{
private final List<SelectOption> options = new ArrayList<>();
private Boolean required = null;

protected Builder(@Nonnull String customId)
{
Expand Down Expand Up @@ -306,17 +297,6 @@ public List<SelectOption> getOptions()
return options;
}

/**
* Whether the user must populate this select menu in Modals, or {@code null} if not set.
*
* @return Whether this menu must be populated, or null
*/
@Nullable
public Boolean isRequired()
{
return required;
}

/**
* Configures which of the currently applied {@link #getOptions() options} should be selected by default.
*
Expand Down Expand Up @@ -395,24 +375,6 @@ public Builder setDefaultOptions(@Nonnull SelectOption... values)
return setDefaultOptions(Arrays.asList(values));
}

/**
* Configure whether the user must populate this select menu if inside a Modal.
* <br>This defaults to {@code true} in Modals when unset.
*
* <p>This only has an effect in Modals!
*
* @param required
* Whether this menu is required
*
* @return The same builder instance for chaining
*/
@Nonnull
public Builder setRequired(@Nullable Boolean required)
{
this.required = required;
return this;
}

/**
* Creates a new {@link StringSelectMenu} instance if all requirements are satisfied.
* <br>A select menu may not have more than {@value #OPTIONS_MAX_AMOUNT} options at once.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import net.dv8tion.jda.api.components.Component;
import net.dv8tion.jda.api.components.MessageTopLevelComponent;
import net.dv8tion.jda.api.components.ModalTopLevelComponent;
import net.dv8tion.jda.api.components.container.ContainerChildComponent;
import net.dv8tion.jda.api.components.section.SectionContentComponent;
import net.dv8tion.jda.api.entities.Message;
Expand All @@ -37,7 +38,9 @@
*
* <p><b>Requirements:</b> {@linkplain MessageRequest#useComponentsV2() Components V2} needs to be enabled!
*/
public interface TextDisplay extends Component, MessageTopLevelComponent, ContainerChildComponent, SectionContentComponent
public interface TextDisplay
extends Component, MessageTopLevelComponent, ModalTopLevelComponent,
ContainerChildComponent, SectionContentComponent
{
/**
* Constructs a new {@link TextDisplay} from the given content.
Expand Down
Loading