diff --git a/src/main/java/net/dv8tion/jda/api/interactions/commands/build/CommandData.java b/src/main/java/net/dv8tion/jda/api/interactions/commands/build/CommandData.java
index 9ee84f2d92..1eb6b78076 100644
--- a/src/main/java/net/dv8tion/jda/api/interactions/commands/build/CommandData.java
+++ b/src/main/java/net/dv8tion/jda/api/interactions/commands/build/CommandData.java
@@ -40,15 +40,20 @@
public interface CommandData extends SerializableData
{
/**
- * The maximum length the name of a command can be.
+ * The maximum length the name of a command can be. ({@value})
*/
int MAX_NAME_LENGTH = 32;
/**
- * The maximum length the description of a command can be.
+ * The maximum length the description of a command can be. ({@value})
*/
int MAX_DESCRIPTION_LENGTH = 100;
+ /**
+ * The maximum amount of options/subcommands/groups that can be added to a command or subcommand. ({@value})
+ */
+ int MAX_OPTIONS = 25;
+
/**
* Sets the {@link LocalizationFunction} for this command
*
This enables you to have the entirety of this command to be localized.
@@ -68,10 +73,10 @@ public interface CommandData extends SerializableData
* Configure the command name.
*
* @param name
- * The name, 1-32 characters (lowercase and alphanumeric for {@link Command.Type#SLASH})
+ * The name, 1-{@value #MAX_NAME_LENGTH} characters (lowercase and alphanumeric for {@link Command.Type#SLASH})
*
* @throws IllegalArgumentException
- * If the name is not between 1-32 characters long, or not lowercase and alphanumeric for slash commands
+ * If the name is not between 1-{@value #MAX_NAME_LENGTH} characters long, or not lowercase and alphanumeric for slash commands
*
* @return The builder instance, for chaining
*/
diff --git a/src/main/java/net/dv8tion/jda/api/interactions/commands/build/SlashCommandData.java b/src/main/java/net/dv8tion/jda/api/interactions/commands/build/SlashCommandData.java
index 7c1d0bbee4..bd184de8d3 100644
--- a/src/main/java/net/dv8tion/jda/api/interactions/commands/build/SlashCommandData.java
+++ b/src/main/java/net/dv8tion/jda/api/interactions/commands/build/SlashCommandData.java
@@ -33,6 +33,7 @@
import java.util.Collection;
import java.util.List;
import java.util.Map;
+import java.util.function.Predicate;
/**
* Extension of {@link CommandData} which allows setting slash-command specific settings such as options and subcommands.
@@ -67,10 +68,10 @@ public interface SlashCommandData extends CommandData
* Configure the description
*
* @param description
- * The description, 1-100 characters
+ * The description, 1-{@value #MAX_DESCRIPTION_LENGTH} characters
*
* @throws IllegalArgumentException
- * If the name is null or not between 1-100 characters
+ * If the name is null or not between 1-{@value #MAX_DESCRIPTION_LENGTH} characters
*
* @return The builder, for chaining
*/
@@ -133,10 +134,113 @@ public interface SlashCommandData extends CommandData
@Nonnull
LocalizationMap getDescriptionLocalizations();
+ /**
+ * Removes all options that evaluate to {@code true} under the provided {@code condition}.
+ *
This will not affect options within subcommands.
+ * Use {@link SubcommandData#removeOptions(Predicate)} instead.
+ *
+ *
Example: Remove all options + *
{@code + * command.removeOptions(option -> true); + * }+ *
Example: Remove all options that are required + *
{@code + * command.removeOptions(option -> option.isRequired()); + * }+ * + * @param condition + * The removal condition (must not throw) + * + * @throws IllegalArgumentException + * If the condition is null + * + * @return True, if any options were removed + */ + boolean removeOptions(@Nonnull Predicate super OptionData> condition); + + /** + * Removes options by the provided name. + *
Example: Remove all subcommands + *
{@code + * command.removeSubcommands(subcommand -> true); + * }+ * + * @param condition + * The removal condition (must not throw) + * + * @throws IllegalArgumentException + * If the condition is null + * + * @return True, if any subcommands were removed + */ + boolean removeSubcommands(@Nonnull Predicate super SubcommandData> condition); + + /** + * Removes subcommands by the provided name. + *
Example: Remove all subcommand groups + *
{@code + * command.removeSubcommandGroups(group -> true); + * }+ * + * @param condition + * The removal condition (must not throw) + * + * @throws IllegalArgumentException + * If the condition is null + * + * @return True, if any subcommand groups were removed + */ + boolean removeSubcommandGroups(@Nonnull Predicate super SubcommandGroupData> condition); + + /** + * Removes subcommand groups by the provided name. + * + * @param name + * The case-sensitive subcommand group name + * + * @return True, if any subcommand groups were removed + */ + default boolean removeSubcommandGroupByName(@Nonnull String name) + { + return removeSubcommandGroups(group -> group.getName().equals(name)); + } + /** * The {@link SubcommandData Subcommands} in this command. - *
Required options must be added before non-required options! * @@ -173,10 +273,10 @@ public interface SlashCommandData extends CommandData * * @throws IllegalArgumentException *
Required options must be added before non-required options! * @@ -196,10 +296,10 @@ public interface SlashCommandData extends CommandData * * @throws IllegalArgumentException *
Valid command layouts are as follows: + *
{@code + * command + * |-- subcommand + * |__ subcommand group + * |__ subcommand + * + * command + * |__ subcommand group + * |__ subcommand + * + * command + * |-- option + * |__ option + * }+ * + * Having an option and subcommand simultaneously is not allowed. * * @param subcommands * The subcommands to add * * @throws IllegalArgumentException - * If null, more than 25 subcommands, or duplicate subcommand names are provided. - * Also throws if you try to mix subcommands/options/groups in one command. + * If null, more than {@value CommandData#MAX_OPTIONS} subcommands, or duplicate subcommand names are provided. + * Also throws if you try adding subcommands when options are already present. * * @return The builder instance, for chaining */ @@ -333,14 +454,35 @@ default SlashCommandData addOption(@Nonnull OptionType type, @Nonnull String nam SlashCommandData addSubcommands(@Nonnull SubcommandData... subcommands); /** - * Add up to 25 {@link SubcommandData Subcommands} to this command. + * Add up to {@value CommandData#MAX_OPTIONS} {@link SubcommandData Subcommands} to this command. + *
Valid command layouts are as follows: + *
{@code + * command + * |-- subcommand + * |__ subcommand group + * |__ subcommand + * + * command + * |__ subcommand group + * |__ subcommand + * + * command + * |-- option + * |__ option + * }+ * + * Having an option and subcommand simultaneously is not allowed. * * @param subcommands * The subcommands to add * * @throws IllegalArgumentException - * If null, more than 25 subcommands, or duplicate subcommand names are provided. - * Also throws if you try to mix subcommands/options/groups in one command. + * If null, more than {@value CommandData#MAX_OPTIONS} subcommands, or duplicate subcommand names are provided. + * Also throws if you try adding subcommands when options are already present. * * @return The builder instance, for chaining */ @@ -352,14 +494,35 @@ default SlashCommandData addSubcommands(@Nonnull Collection extends Subcommand } /** - * Add up to 25 {@link SubcommandGroupData Subcommand-Groups} to this command. + * Add up to {@value CommandData#MAX_OPTIONS} {@link SubcommandGroupData Subcommand-Groups} to this command. + *
Valid command layouts are as follows: + *
{@code + * command + * |-- subcommand + * |__ subcommand group + * |__ subcommand + * + * command + * |__ subcommand group + * |__ subcommand + * + * command + * |-- option + * |__ option + * }+ * + * Having an option and subcommand simultaneously is not allowed. * * @param groups * The subcommand groups to add * * @throws IllegalArgumentException - * If null, more than 25 subcommand groups, or duplicate group names are provided. - * Also throws if you try to mix subcommands/options/groups in one command. + * If null, more than {@value CommandData#MAX_OPTIONS} subcommand groups, or duplicate group names are provided. + * Also throws if you try adding subcommand groups when options are already present. * * @return The builder instance, for chaining */ @@ -367,14 +530,35 @@ default SlashCommandData addSubcommands(@Nonnull Collection extends Subcommand SlashCommandData addSubcommandGroups(@Nonnull SubcommandGroupData... groups); /** - * Add up to 25 {@link SubcommandGroupData Subcommand-Groups} to this command. + * Add up to {@value CommandData#MAX_OPTIONS} {@link SubcommandGroupData Subcommand-Groups} to this command. + *
Valid command layouts are as follows: + *
{@code + * command + * |-- subcommand + * |__ subcommand group + * |__ subcommand + * + * command + * |__ subcommand group + * |__ subcommand + * + * command + * |-- option + * |__ option + * }+ * + * Having an option and subcommand simultaneously is not allowed. * * @param groups * The subcommand groups to add * * @throws IllegalArgumentException - * If null, more than 25 subcommand groups, or duplicate group names are provided. - * Also throws if you try to mix subcommands/options/groups in one command. + * If null, more than {@value CommandData#MAX_OPTIONS} subcommand groups, or duplicate group names are provided. + * Also throws if you try adding subcommand groups when options are already present. * * @return The builder instance, for chaining */ diff --git a/src/main/java/net/dv8tion/jda/api/interactions/commands/build/SubcommandData.java b/src/main/java/net/dv8tion/jda/api/interactions/commands/build/SubcommandData.java index 92b2d6691e..60cfc3a3d2 100644 --- a/src/main/java/net/dv8tion/jda/api/interactions/commands/build/SubcommandData.java +++ b/src/main/java/net/dv8tion/jda/api/interactions/commands/build/SubcommandData.java @@ -25,14 +25,11 @@ import net.dv8tion.jda.api.utils.data.DataObject; import net.dv8tion.jda.api.utils.data.SerializableData; import net.dv8tion.jda.internal.utils.Checks; -import net.dv8tion.jda.internal.utils.Helpers; import net.dv8tion.jda.internal.utils.localization.LocalizationUtils; import javax.annotation.Nonnull; -import java.util.Arrays; -import java.util.Collection; -import java.util.List; -import java.util.Map; +import java.util.*; +import java.util.function.Predicate; import java.util.stream.Stream; /** @@ -40,7 +37,7 @@ */ public class SubcommandData implements SerializableData { - protected final DataArray options = DataArray.empty(); + protected final List
{@code + * command.removeOptions(option -> true); + * }+ *
Example: Remove all options that are required + *
{@code + * command.removeOptions(option -> option.isRequired()); + * }+ * + * @param condition + * The removal condition (must not throw) + * + * @throws IllegalArgumentException + * If the condition is null + * + * @return True, if any options were removed + */ + public boolean removeOptions(@Nonnull Predicate super OptionData> condition) + { + Checks.notNull(condition, "Condition"); + return options.removeIf(condition); + } + + /** + * Removes options by the provided name. + * + * @param name + * The case-sensitive option name + * + * @return True, if any options were removed + */ + public boolean removeOptionByName(@Nonnull String name) + { + return removeOptions(option -> option.getName().equals(name)); + } + + /** + * Adds up to {@value CommandData#MAX_OPTIONS} options to this subcommand. * *
Required options must be added before non-required options!
*
@@ -229,7 +265,7 @@ public SubcommandData setDescriptionLocalizations(@Nonnull Map Required options must be added before non-required options!
*
@@ -272,7 +307,7 @@ public SubcommandData addOptions(@Nonnull OptionData... options)
* @throws IllegalArgumentException
* Example: Remove all subcommands
+ *
*
@@ -294,9 +329,9 @@ public SubcommandData addOptions(@Nonnull Collection extends OptionData> optio
* @param type
* The {@link OptionType}
* @param name
- * The lowercase option name, 1-32 characters
+ * The lowercase option name, 1-{@value OptionData#MAX_NAME_LENGTH} characters
* @param description
- * The option description, 1-100 characters
+ * The option description, 1-{@value OptionData#MAX_DESCRIPTION_LENGTH} characters
* @param required
* Whether this option is required (See {@link OptionData#setRequired(boolean)})
* @param autoComplete
@@ -307,7 +342,7 @@ public SubcommandData addOptions(@Nonnull Collection extends OptionData> optio
*
*
@@ -330,16 +365,16 @@ public SubcommandData addOption(@Nonnull OptionType type, @Nonnull String name,
* @param type
* The {@link OptionType}
* @param name
- * The lowercase option name, 1-32 characters
+ * The lowercase option name, 1-{@value OptionData#MAX_NAME_LENGTH} characters
* @param description
- * The option description, 1-100 characters
+ * The option description, 1-{@value OptionData#MAX_DESCRIPTION_LENGTH} characters
* @param required
* Whether this option is required (See {@link OptionData#setRequired(boolean)})
*
* @throws IllegalArgumentException
*
*
@@ -361,14 +396,14 @@ public SubcommandData addOption(@Nonnull OptionType type, @Nonnull String name,
* @param type
* The {@link OptionType}
* @param name
- * The lowercase option name, 1-32 characters
+ * The lowercase option name, 1-{@value OptionData#MAX_NAME_LENGTH} characters
* @param description
- * The option description, 1-100 characters
+ * The option description, 1-{@value OptionData#MAX_DESCRIPTION_LENGTH} characters
*
* @throws IllegalArgumentException
*
*
@@ -389,10 +424,7 @@ public SubcommandData addOption(@Nonnull OptionType type, @Nonnull String name,
@Nonnull
public List{@code
+ * command.removeSubcommands(subcommand -> true);
+ * }
+ *
+ * @param condition
+ * The removal condition (must not throw)
+ *
+ * @throws IllegalArgumentException
+ * If the condition is null
+ *
+ * @return True, if any subcommands were removed
+ */
+ public boolean removeSubcommand(@Nonnull Predicate super SubcommandData> condition)
+ {
+ Checks.notNull(condition, "Condition");
+ return subcommands.removeIf(condition);
+ }
+
+ /**
+ * Removes subcommands by the provided name.
+ *
+ * @param name
+ * The case-sensitive subcommand name
+ *
+ * @return True, if any subcommands were removed
+ */
+ public boolean removeSubcommandByName(@Nonnull String name)
+ {
+ return removeSubcommand(subcommand -> subcommand.getName().equals(name));
+ }
+
/**
* The {@link SubcommandData Subcommands} in this group.
- *
These subcommand instances are reconstructed,
- * which means that any modifications will not be reflected in the backing state.
*
* @return Immutable list of {@link SubcommandData}
*/
@Nonnull
public List