Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add select menu default values #1295

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.javacord.api.entity.message.component.internal.SelectMenuBuilderDelegate;
import org.javacord.api.util.internal.DelegateFactory;
import java.util.List;
import java.util.Set;

public class SelectMenuBuilder implements LowLevelComponentBuilder {
private final SelectMenuBuilderDelegate delegate = DelegateFactory.createSelectMenuBuilderDelegate();
Expand Down Expand Up @@ -168,6 +169,89 @@ public SelectMenuBuilder setDisabled(boolean isDisabled) {
return this;
}

/**
* Add a default value to the select menu.
*
* <p>Only usable with
*
* <p>{@link ComponentType#SELECT_MENU_USER},
*
* <p>{@link ComponentType#SELECT_MENU_ROLE},
*
* <p>{@link ComponentType#SELECT_MENU_MENTIONABLE} or
*
* <p>{@link ComponentType#SELECT_MENU_CHANNEL}.
*
* @param defaultValue The default value.
* @return The builder.
*/
public SelectMenuBuilder addDefaultValue(SelectMenuDefaultValue defaultValue) {
delegate.addDefaultValue(defaultValue);
return this;
}

/**
* Removes a default value from the select menu.
*
* <p>Only usable with
*
* <p>{@link ComponentType#SELECT_MENU_USER},
*
* <p>{@link ComponentType#SELECT_MENU_ROLE},
*
* <p>{@link ComponentType#SELECT_MENU_MENTIONABLE} or
*
* <p>{@link ComponentType#SELECT_MENU_CHANNEL}.
*
* @param defaultValue The default value.
* @return The builder.
*/
public SelectMenuBuilder removeDefaultValue(SelectMenuDefaultValue defaultValue) {
delegate.removeDefaultValue(defaultValue);
return this;
}

/**
* Adds all given default values to the select menu.
*
* <p>Only usable with
*
* <p>{@link ComponentType#SELECT_MENU_USER},
*
* <p>{@link ComponentType#SELECT_MENU_ROLE},
*
* <p>{@link ComponentType#SELECT_MENU_MENTIONABLE} or
*
* <p>{@link ComponentType#SELECT_MENU_CHANNEL}.
*
* @param defaultValues The default values.
* @return The builder.
*/
public SelectMenuBuilder addDefaultValues(Set<SelectMenuDefaultValue> defaultValues) {
defaultValues.forEach(delegate::addDefaultValue);
return this;
}

/**
* Removes all default values from the select menu.
*
* <p>Only usable with
*
* <p>{@link ComponentType#SELECT_MENU_USER},
*
* <p>{@link ComponentType#SELECT_MENU_ROLE},
*
* <p>{@link ComponentType#SELECT_MENU_MENTIONABLE} or
*
* <p>{@link ComponentType#SELECT_MENU_CHANNEL}.
*
* @return The builder.
*/
public SelectMenuBuilder removeAllDefaultValues() {
delegate.removeAllDefaultValues();
return this;
}

/**
* Create a copy of an existing select menu.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.javacord.api.entity.message.component;

public interface SelectMenuDefaultValue {

/**
* Get the id of a user, role, or channel.
*
* @return The id of a user, role, or channel.
*/
long getId();

/**
* Get type of value that id represents.
*
* @return Type of value that id represents. Either user, role, or channel.
*/
SelectMenuDefaultValueType getType();

/**
* Creates a new select menu default value with the given values.
*
* @param id The id for the default value.
* @param type The type for the default value.
* @return The created select menu default value.
*/
static SelectMenuDefaultValue create(long id, SelectMenuDefaultValueType type) {
return new SelectMenuDefaultValueBuilder()
.setId(id)
.setType(type)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.javacord.api.entity.message.component;

import org.javacord.api.entity.message.component.internal.SelectMenuDefaultValueBuilderDelegate;
import org.javacord.api.util.internal.DelegateFactory;

public class SelectMenuDefaultValueBuilder {

private final SelectMenuDefaultValueBuilderDelegate delegate =
DelegateFactory.createSelectMenuDefaultValueBuilderDelegate();

/**
* Set the id for the select menu default value.
*
* @param id The id.
* @return The builder.
*/
public SelectMenuDefaultValueBuilder setId(long id) {
delegate.setId(id);
return this;
}

/**
* Set the type for the select menu default value.
*
* @param type The type.
* @return The builder.
*/
public SelectMenuDefaultValueBuilder setType(SelectMenuDefaultValueType type) {
delegate.setType(type);
return this;
}

/**
* Creates a {@link SelectMenuDefaultValue} instance with the given values.
*
* @return The created select menu default value instance.
*/
public SelectMenuDefaultValue build() {
return delegate.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.javacord.api.entity.message.component;

import java.util.Arrays;

public enum SelectMenuDefaultValueType {

USER("user"),
ROLE("role"),
CHANNEL("channel");

private final String jsonName;

SelectMenuDefaultValueType(final String jsonName) {
this.jsonName = jsonName;
}

/**
* Get the string that is being put into JSON in order to communicate with Discord API.
*
* @return The type name that is being put into JSON.
*/
public String getJsonName() {
return this.jsonName;
}

/**
* Convert a string to the SelectMenuDefaultValueType enum value.
*
* @param type The string to be converted. Should be either "user", "role" or "channel".
* @return The SelectMenuDefaultValueType enum value after conversion.
* @throws IllegalArgumentException If type is not "user", "role" or "channel".
*/
public static SelectMenuDefaultValueType fromString(String type) {
return Arrays
.stream(SelectMenuDefaultValueType.values())
.filter(defaultValueType -> defaultValueType.getJsonName().equals(type))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException(
"Select menu default value type should be either \"user\", \"role\", or \"channel\".")
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.javacord.api.entity.channel.ChannelType;
import org.javacord.api.entity.message.component.ComponentType;
import org.javacord.api.entity.message.component.SelectMenu;
import org.javacord.api.entity.message.component.SelectMenuDefaultValue;
import org.javacord.api.entity.message.component.SelectMenuOption;

public interface SelectMenuBuilderDelegate extends ComponentBuilderDelegate {
Expand Down Expand Up @@ -84,6 +85,20 @@ public interface SelectMenuBuilderDelegate extends ComponentBuilderDelegate {
*/
void setDisabled(boolean disabled);

/**
* Add a default value to the select menu.
*
* @param defaultValue The default value to add.
*/
void addDefaultValue(SelectMenuDefaultValue defaultValue);

/**
* Remove a default value from the select menu.
*
* @param defaultValue The default value to remove.
*/
void removeDefaultValue(SelectMenuDefaultValue defaultValue);

/**
* Creates a {@link SelectMenu} instance with the given values.
*
Expand All @@ -96,6 +111,11 @@ public interface SelectMenuBuilderDelegate extends ComponentBuilderDelegate {
*/
void removeAllOptions();

/**
* Removes all default values from the select menu.
*/
void removeAllDefaultValues();

/**
* Get the custom ID of the select menu.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.javacord.api.entity.message.component.internal;

import org.javacord.api.entity.message.component.SelectMenuDefaultValue;
import org.javacord.api.entity.message.component.SelectMenuDefaultValueType;

public interface SelectMenuDefaultValueBuilderDelegate {

/**
* Copy the give select menu default value.
*
* @param selectMenuDefaultValue The select menu default value.
*/
void copy(SelectMenuDefaultValue selectMenuDefaultValue);

/**
* Set the id of a user, role, or channel.
*
* @param id The id.
*/
void setId(long id);

/**
* Set the type of value that id represents.
*
* @param type The type.
*/
void setType(SelectMenuDefaultValueType type);

/**
* Build the select menu default value.
*
* @return The default value.
*/
SelectMenuDefaultValue build();
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import org.javacord.api.entity.Mentionable;
import org.javacord.api.entity.channel.ServerChannel;
import org.javacord.api.entity.message.component.ComponentType;
import org.javacord.api.entity.message.component.SelectMenuDefaultValue;
import org.javacord.api.entity.message.component.SelectMenuOption;
import org.javacord.api.entity.permission.Role;
import org.javacord.api.entity.user.User;

import java.util.List;
import java.util.Optional;
import java.util.Set;

public interface SelectMenuInteraction extends MessageComponentInteraction {

Expand Down Expand Up @@ -89,4 +91,19 @@ public interface SelectMenuInteraction extends MessageComponentInteraction {
*/
int getMaximumValues();

/**
* Get all default values from the select menu.
* Only available if the select menu is of type
*
* <p>{@link ComponentType#SELECT_MENU_USER},
*
* <p>{@link ComponentType#SELECT_MENU_ROLE},
*
* <p>{@link ComponentType#SELECT_MENU_MENTIONABLE} or
*
* <p>{@link ComponentType#SELECT_MENU_CHANNEL}.
*
* @return All default values.
*/
Set<SelectMenuDefaultValue> getDefaultValues();
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.javacord.api.entity.message.component.internal.ActionRowBuilderDelegate;
import org.javacord.api.entity.message.component.internal.ButtonBuilderDelegate;
import org.javacord.api.entity.message.component.internal.SelectMenuBuilderDelegate;
import org.javacord.api.entity.message.component.internal.SelectMenuDefaultValueBuilderDelegate;
import org.javacord.api.entity.message.component.internal.SelectMenuOptionBuilderDelegate;
import org.javacord.api.entity.message.component.internal.TextInputBuilderDelegate;
import org.javacord.api.entity.message.embed.internal.EmbedBuilderDelegate;
Expand Down Expand Up @@ -172,6 +173,15 @@ public static SelectMenuOptionBuilderDelegate createSelectMenuOptionBuilderDeleg
return delegateFactoryDelegate.createSelectMenuOptionBuilderDelegate();
}

/**
* Creates a new select menu default value builder delegate.
*
* @return A new select menu default value builder delegate.
*/
public static SelectMenuDefaultValueBuilderDelegate createSelectMenuDefaultValueBuilderDelegate() {
return delegateFactoryDelegate.createSelectMenuDefaultValueBuilderDelegate();
}

/**
* Creates a new mention builder delegate.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.javacord.api.entity.message.component.internal.ActionRowBuilderDelegate;
import org.javacord.api.entity.message.component.internal.ButtonBuilderDelegate;
import org.javacord.api.entity.message.component.internal.SelectMenuBuilderDelegate;
import org.javacord.api.entity.message.component.internal.SelectMenuDefaultValueBuilderDelegate;
import org.javacord.api.entity.message.component.internal.SelectMenuOptionBuilderDelegate;
import org.javacord.api.entity.message.component.internal.TextInputBuilderDelegate;
import org.javacord.api.entity.message.embed.internal.EmbedBuilderDelegate;
Expand Down Expand Up @@ -448,6 +449,13 @@ TextableRegularServerChannelUpdaterDelegate createTextableRegularServerChannelUp
*/
SelectMenuOptionBuilderDelegate createSelectMenuOptionBuilderDelegate();

/**
* Creates a new select menu default value builder delegate.
*
* @return A new select menu default value builder delegate.
*/
SelectMenuDefaultValueBuilderDelegate createSelectMenuDefaultValueBuilderDelegate();

/**
* Creates a new sticker builder delegate.
*
Expand Down
Loading
Loading