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

Improve implementation of command data #2258

Merged
merged 3 commits into from
Jan 10, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -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
* <br>This enables you to have the entirety of this command to be localized.
Expand All @@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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}.
* <br>This will not affect options within subcommands.
* Use {@link SubcommandData#removeOptions(Predicate)} instead.
*
* <p><b>Example: Remove all options</b>
* <pre>{@code
* command.removeOptions(option -> true);
* }</pre>
* <p><b>Example: Remove all options that are required</b>
* <pre>{@code
* command.removeOptions(option -> option.isRequired());
* }</pre>
*
* @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.
* <br>This will not affect options within subcommands.
* Use {@link SubcommandData#removeOptionByName(String)} instead.
*
* @param name
* The <b>case-sensitive</b> option name
*
* @return True, if any options were removed
*/
default boolean removeOptionByName(@Nonnull String name)
{
return removeOptions(option -> option.getName().equals(name));
}

/**
* Removes all subcommands that evaluate to {@code true} under the provided {@code condition}.
* <br>This will not apply to subcommands within subcommand groups.
* Use {@link SubcommandGroupData#removeSubcommand(Predicate)} instead.
*
* <p><b>Example: Remove all subcommands</b>
* <pre>{@code
* command.removeSubcommands(subcommand -> true);
* }</pre>
*
* @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.
* <br>This will not apply to subcommands within subcommand groups.
* Use {@link SubcommandGroupData#removeSubcommandByName(String)} instead.
*
* @param name
* The <b>case-sensitive</b> subcommand name
*
* @return True, if any subcommands were removed
*/
default boolean removeSubcommandByName(@Nonnull String name)
{
return removeSubcommands(subcommand -> subcommand.getName().equals(name));
}

/**
* Removes all subcommand groups that evaluate to {@code true} under the provided {@code condition}.
*
* <p><b>Example: Remove all subcommand groups</b>
* <pre>{@code
* command.removeSubcommandGroups(group -> true);
* }</pre>
*
* @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 <b>case-sensitive</b> 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.
* <br>These subcommand instances are <b>reconstructed</b>,
* which means that any modifications will not be reflected in the backing state.
*
* @return Immutable list of {@link SubcommandData}
*/
Expand All @@ -145,8 +249,6 @@ public interface SlashCommandData extends CommandData

/**
* The {@link SubcommandGroupData Subcommand Groups} in this command.
* <br>These subcommand group instances are <b>reconstructed</b>,
* which means that any modifications will not be reflected in the backing state.
*
* @return Immutable list of {@link SubcommandGroupData}
*/
Expand All @@ -155,16 +257,14 @@ public interface SlashCommandData extends CommandData

/**
* The options for this command.
* <br>These option instances are <b>reconstructed</b>,
* which means that any modifications will not be reflected in the backing state.
*
* @return Immutable list of {@link OptionData}
*/
@Nonnull
List<OptionData> getOptions();

/**
* Adds up to 25 options to this command.
* Adds up to {@value CommandData#MAX_OPTIONS} options to this command.
*
* <p>Required options must be added before non-required options!
*
Expand All @@ -176,7 +276,7 @@ public interface SlashCommandData extends CommandData
* <li>If you try to mix subcommands/options/groups in one command.</li>
* <li>If the option type is {@link OptionType#SUB_COMMAND} or {@link OptionType#SUB_COMMAND_GROUP}.</li>
* <li>If this option is required and you already added a non-required option.</li>
* <li>If more than 25 options are provided.</li>
* <li>If more than {@value CommandData#MAX_OPTIONS} options are provided.</li>
* <li>If the option name is not unique</li>
* <li>If null is provided</li>
* </ul>
Expand All @@ -187,7 +287,7 @@ public interface SlashCommandData extends CommandData
SlashCommandData addOptions(@Nonnull OptionData... options);

/**
* Adds up to 25 options to this command.
* Adds up to {@value CommandData#MAX_OPTIONS} options to this command.
*
* <p>Required options must be added before non-required options!
*
Expand All @@ -199,7 +299,7 @@ public interface SlashCommandData extends CommandData
* <li>If you try to mix subcommands/options/groups in one command.</li>
* <li>If the option type is {@link OptionType#SUB_COMMAND} or {@link OptionType#SUB_COMMAND_GROUP}.</li>
* <li>If this option is required and you already added a non-required option.</li>
* <li>If more than 25 options are provided.</li>
* <li>If more than {@value CommandData#MAX_OPTIONS} options are provided.</li>
* <li>If the option name is not unique</li>
* <li>If null is provided</li>
* </ul>
Expand All @@ -221,9 +321,9 @@ default SlashCommandData addOptions(@Nonnull Collection<? extends OptionData> op
* @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
Expand All @@ -237,7 +337,7 @@ default SlashCommandData addOptions(@Nonnull Collection<? extends OptionData> op
* <li>If the option type is {@link OptionType#SUB_COMMAND} or {@link OptionType#SUB_COMMAND_GROUP}.</li>
* <li>If the provided option type does not support auto-complete</li>
* <li>If this option is required and you already added a non-required option.</li>
* <li>If more than 25 options are provided.</li>
* <li>If more than {@value CommandData#MAX_OPTIONS} options are provided.</li>
* <li>If the option name is not unique</li>
* <li>If null is provided</li>
* </ul>
Expand All @@ -260,9 +360,9 @@ default SlashCommandData addOption(@Nonnull OptionType type, @Nonnull String nam
* @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)})
*
Expand All @@ -272,7 +372,7 @@ default SlashCommandData addOption(@Nonnull OptionType type, @Nonnull String nam
* <li>If the option type is {@link OptionType#UNKNOWN UNKNOWN}.</li>
* <li>If the option type is {@link OptionType#SUB_COMMAND} or {@link OptionType#SUB_COMMAND_GROUP}.</li>
* <li>If this option is required and you already added a non-required option.</li>
* <li>If more than 25 options are provided.</li>
* <li>If more than {@value CommandData#MAX_OPTIONS} options are provided.</li>
* <li>If the option name is not unique</li>
* <li>If null is provided</li>
* </ul>
Expand All @@ -294,17 +394,17 @@ default SlashCommandData addOption(@Nonnull OptionType type, @Nonnull String nam
* @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
* <ul>
* <li>If you try to mix subcommands/options/groups in one command.</li>
* <li>If the option type is {@link OptionType#UNKNOWN UNKNOWN}.</li>
* <li>If the option type is {@link OptionType#SUB_COMMAND} or {@link OptionType#SUB_COMMAND_GROUP}.</li>
* <li>If this option is required and you already added a non-required option.</li>
* <li>If more than 25 options are provided.</li>
* <li>If more than {@value CommandData#MAX_OPTIONS} options are provided.</li>
* <li>If the option name is not unique</li>
* <li>If null is provided</li>
* </ul>
Expand All @@ -318,13 +418,13 @@ default SlashCommandData addOption(@Nonnull OptionType type, @Nonnull String nam
}

/**
* Add up to 25 {@link SubcommandData Subcommands} to this command.
* Add up to {@value CommandData#MAX_OPTIONS} {@link SubcommandData Subcommands} to this command.
*
* @param subcommands
* The subcommands to add
*
* @throws IllegalArgumentException
* If null, more than 25 subcommands, or duplicate subcommand names are provided.
* If null, more than {@value CommandData#MAX_OPTIONS} subcommands, or duplicate subcommand names are provided.
* Also throws if you try to mix subcommands/options/groups in one command.
*
* @return The builder instance, for chaining
Expand All @@ -333,13 +433,13 @@ 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.
*
* @param subcommands
* The subcommands to add
*
* @throws IllegalArgumentException
* If null, more than 25 subcommands, or duplicate subcommand names are provided.
* If null, more than {@value CommandData#MAX_OPTIONS} subcommands, or duplicate subcommand names are provided.
* Also throws if you try to mix subcommands/options/groups in one command.
*
* @return The builder instance, for chaining
Expand All @@ -352,13 +452,13 @@ 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.
*
* @param groups
* The subcommand groups to add
*
* @throws IllegalArgumentException
* If null, more than 25 subcommand groups, or duplicate group names are provided.
* If null, more than {@value CommandData#MAX_OPTIONS} subcommand groups, or duplicate group names are provided.
* Also throws if you try to mix subcommands/options/groups in one command.
*
* @return The builder instance, for chaining
Expand All @@ -367,13 +467,13 @@ 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.
*
* @param groups
* The subcommand groups to add
*
* @throws IllegalArgumentException
* If null, more than 25 subcommand groups, or duplicate group names are provided.
* If null, more than {@value CommandData#MAX_OPTIONS} subcommand groups, or duplicate group names are provided.
* Also throws if you try to mix subcommands/options/groups in one command.
*
* @return The builder instance, for chaining
Expand Down
Loading