Class OptionData
java.lang.Object
net.dv8tion.jda.api.interactions.commands.build.OptionData
- All Implemented Interfaces:
SerializableData
Builder for a Slash-Command option.
-
Field Summary
Modifier and TypeFieldDescriptionstatic final int
The maximum length of the name of Command Option Choice namesstatic final int
The maximum length aString value
for a choice can be.static final int
The total amount ofchoices
you can set.static final int
The maximum length the description of an option can be.static final int
The maximum length the name of an option can be.static final double
The highest positive amount Discord allows theNUMBER
type to be.static final int
The maximum length for aString option
.static final double
The largest negative amount Discord allows theNUMBER
type to be. -
Constructor Summary
ConstructorDescriptionOptionData
(OptionType type, String name, String description) Create an option builder.OptionData
(OptionType type, String name, String description, boolean isRequired) Create an option builder.OptionData
(OptionType type, String name, String description, boolean isRequired, boolean isAutoComplete) Create an option builder. -
Method Summary
Modifier and TypeMethodDescriptionAdd a predefined choice for this option.Add a predefined choice for this option.Add a predefined choice for this option.addChoices
(Collection<? extends Command.Choice> choices) Adds up to 25 predefined choices for this option.addChoices
(Command.Choice... choices) Adds up to 25 predefined choices for this option.static OptionData
fromData
(DataObject json) Parses the provided serialization back into an OptionData instance.static OptionData
fromOption
(Command.Option option) Converts the providedCommand.Option
into a OptionData instance.TheChannelTypes
this option is restricted to.@Unmodifiable List
<Command.Choice> The choices for this option.The description for this optionThe localizations of this option's description forvarious languages
.The maximum length for strings which can be provided for this option.The maximum value which can be provided for this option.The minimum length for strings which can be provided for this option.The minimum value which can be provided for this option.getName()
The name for this optionThe localizations of this option's name forvarious languages
.getType()
TheOptionType
for this optionboolean
Whether this option supports auto-complete interactions viaCommandAutoCompleteInteractionEvent
.boolean
Whether this option is required.setAutoComplete
(boolean autoComplete) Configure whether this option should support auto-complete interactions viaCommandAutoCompleteInteractionEvent
.setChannelTypes
(Collection<ChannelType> channelTypes) Configure theChannelTypes
to restrict this option to.setChannelTypes
(ChannelType... channelTypes) Configure theChannelTypes
to restrict this option to.setDescription
(String description) Configure the descriptionsetDescriptionLocalization
(DiscordLocale locale, String description) Sets alanguage-specific
localization of this option's description.Sets multiplelanguage-specific
localizations of this option's description.setMaxLength
(int maxLength) Configure the maximum length for strings which can be provided for this option.setMaxValue
(double value) Configure the maximal value which can be provided for this option.setMaxValue
(long value) Configure the maximal value which can be provided for this option.setMinLength
(int minLength) Configure the minimum length for strings which can be provided for this option.setMinValue
(double value) Configure the minimal value which can be provided for this option.setMinValue
(long value) Configure the minimal value which can be provided for this option.Configure the namesetNameLocalization
(DiscordLocale locale, String name) Sets alanguage-specific
localization of this option's name.Sets multiplelanguage-specific
localizations of this option's name.setRequired
(boolean required) Configure whether the user must set this option.setRequiredLength
(int minLength, int maxLength) Configure the minimum and maximum length for strings which can be provided for this option.setRequiredRange
(double minValue, double maxValue) Configure the minimal and maximal value which can be provided for this option.setRequiredRange
(long minValue, long maxValue) Configure the minimal and maximal value which can be provided for this option.toData()
SerializedDataObject
for this object.
-
Field Details
-
MAX_POSITIVE_NUMBER
public static final double MAX_POSITIVE_NUMBERThe highest positive amount Discord allows theNUMBER
type to be.- See Also:
-
MIN_NEGATIVE_NUMBER
public static final double MIN_NEGATIVE_NUMBERThe largest negative amount Discord allows theNUMBER
type to be.- See Also:
-
MAX_NAME_LENGTH
public static final int MAX_NAME_LENGTHThe maximum length the name of an option can be.- See Also:
-
MAX_CHOICE_NAME_LENGTH
public static final int MAX_CHOICE_NAME_LENGTHThe maximum length of the name of Command Option Choice names- See Also:
-
MAX_DESCRIPTION_LENGTH
public static final int MAX_DESCRIPTION_LENGTHThe maximum length the description of an option can be.- See Also:
-
MAX_CHOICE_VALUE_LENGTH
public static final int MAX_CHOICE_VALUE_LENGTHThe maximum length aString value
for a choice can be.- See Also:
-
MAX_CHOICES
public static final int MAX_CHOICESThe total amount ofchoices
you can set.- See Also:
-
MAX_STRING_OPTION_LENGTH
public static final int MAX_STRING_OPTION_LENGTHThe maximum length for aString option
.- See Also:
-
-
Constructor Details
-
OptionData
Create an option builder.
This option is notrequired
by default.- Parameters:
type
- TheOptionType
name
- The option name, up to 32 alphanumeric (with dash) lowercase characters, as defined byMAX_NAME_LENGTH
description
- The option description, up to 100 characters, as defined byMAX_DESCRIPTION_LENGTH
- Throws:
IllegalArgumentException
-
-
OptionData
public OptionData(@Nonnull OptionType type, @Nonnull String name, @Nonnull String description, boolean isRequired) Create an option builder.- Parameters:
type
- TheOptionType
name
- The option name, up to 32 alphanumeric (with dash) lowercase characters, as defined byMAX_NAME_LENGTH
description
- The option description, up to 100 characters, as defined byMAX_DESCRIPTION_LENGTH
isRequired
-True
, if this option is required- Throws:
IllegalArgumentException
-
-
OptionData
public OptionData(@Nonnull OptionType type, @Nonnull String name, @Nonnull String description, boolean isRequired, boolean isAutoComplete) Create an option builder.- Parameters:
type
- TheOptionType
name
- The option name, up to 32 alphanumeric (with dash) lowercase characters, as defined byMAX_NAME_LENGTH
description
- The option description, up to 100 characters, as defined byMAX_DESCRIPTION_LENGTH
isRequired
-True
, if this option is requiredisAutoComplete
- True, if auto-complete should be supported (requiresOptionType.canSupportChoices()
)- Throws:
IllegalArgumentException
-- If
type
is null - If
type
isUNKNOWN
,SUB_COMMAND
, orSUB_COMMAND_GROUP
- If
name
is not alphanumeric (with dash), lowercase and between 1 and 32 characters long - If
description
is not between 1 and 100 characters long - If
OptionType.canSupportChoices()
is false andisAutoComplete
is true
- If
-
-
Method Details
-
getType
TheOptionType
for this option- Returns:
- The
OptionType
-
getName
The name for this option- Returns:
- The name
-
getNameLocalizations
The localizations of this option's name forvarious languages
.- Returns:
- The
LocalizationMap
containing the mapping fromDiscordLocale
to the localized name
-
getDescription
The description for this option- Returns:
- The description
-
getDescriptionLocalizations
The localizations of this option's description forvarious languages
.- Returns:
- The
LocalizationMap
containing the mapping fromDiscordLocale
to the localized description
-
isRequired
public boolean isRequired()Whether this option is required.
This can be configured withsetRequired(boolean)
.Required options must always be set by the command invocation.
- Returns:
- True, if this option is required
-
isAutoComplete
public boolean isAutoComplete()Whether this option supports auto-complete interactions viaCommandAutoCompleteInteractionEvent
.- Returns:
- True, if this option supports auto-complete
-
getChannelTypes
TheChannelTypes
this option is restricted to.
This is empty if the option is not of typeCHANNEL
or not restricted to specific types.- Returns:
EnumSet
ofChannelType
-
getMinValue
The minimum value which can be provided for this option.
This returnsnull
if the value is not set or if the option is not of typeINTEGER
orNUMBER
.- Returns:
- The minimum value for this option
-
getMaxValue
The maximum value which can be provided for this option.
This returnsnull
if the value is not set or if the option is not of typeINTEGER
orNUMBER
.- Returns:
- The maximum value for this option
-
getMinLength
The minimum length for strings which can be provided for this option.
This returnsnull
if the value is not set or if the option is not of typeSTRING
.- Returns:
- The minimum length for strings for this option or
null
-
getMaxLength
The maximum length for strings which can be provided for this option.
This returnsnull
if the value is not set or if the option is not of typeSTRING
.- Returns:
- The maximum length for strings for this option or
null
-
getChoices
The choices for this option.
This is empty by default and can only be configured for specific option types.- Returns:
- Immutable list of
Choices
- See Also:
-
setName
Configure the name- Parameters:
name
- The lowercase alphanumeric (with dash) name,1-32 characters long
- Returns:
- The OptionData instance, for chaining
- Throws:
IllegalArgumentException
- If the name is null, empty, not alphanumeric, or not between 1-32 characters long, as defined byMAX_NAME_LENGTH
-
setNameLocalization
Sets alanguage-specific
localization of this option's name.- Parameters:
locale
- The locale to associate the translated name withname
- The translated name to put- Returns:
- This builder instance, for chaining
- Throws:
IllegalArgumentException
-- If the locale is null
- If the name is null
- If the locale is
DiscordLocale.UNKNOWN
- If the name does not pass the corresponding
name check
-
setNameLocalizations
Sets multiplelanguage-specific
localizations of this option's name.- Parameters:
map
- The map from which to transfer the translated names- Returns:
- This builder instance, for chaining
- Throws:
IllegalArgumentException
-- If the map is null
- If the map contains an
DiscordLocale.UNKNOWN
key - If the map contains a name which does not pass the corresponding
name check
-
setDescription
Configure the description- Parameters:
description
- The description, 1-100 characters, as defined byMAX_DESCRIPTION_LENGTH
- Returns:
- The OptionData instance, for chaining
- Throws:
IllegalArgumentException
- If the name is null, empty, or longer than 100, as defined byMAX_DESCRIPTION_LENGTH
-
setDescriptionLocalization
@Nonnull public OptionData setDescriptionLocalization(@Nonnull DiscordLocale locale, @Nonnull String description) Sets alanguage-specific
localization of this option's description.- Parameters:
locale
- The locale to associate the translated description withdescription
- The translated description to put- Returns:
- This builder instance, for chaining
- Throws:
IllegalArgumentException
-- If the locale is null
- If the description is null
- If the locale is
DiscordLocale.UNKNOWN
- If the description does not pass the corresponding
description check
-
setDescriptionLocalizations
Sets multiplelanguage-specific
localizations of this option's description.- Parameters:
map
- The map from which to transfer the translated descriptions- Returns:
- This builder instance, for chaining
- Throws:
IllegalArgumentException
-- If the map is null
- If the map contains an
DiscordLocale.UNKNOWN
key - If the map contains a description which does not pass the corresponding
description check
-
setRequired
Configure whether the user must set this option.
Required options must always be filled out when using the command.- Parameters:
required
- True, if this option is required- Returns:
- The OptionData instance, for chaining
-
setAutoComplete
Configure whether this option should support auto-complete interactions viaCommandAutoCompleteInteractionEvent
.This is only supported for options which support choices. See
OptionType.canSupportChoices()
.- Parameters:
autoComplete
- True, if auto-complete should be supported- Returns:
- The OptionData instance, for chaining
- Throws:
IllegalStateException
- If this option is already configured to use choices or the option type does not support auto-complete
-
setChannelTypes
Configure theChannelTypes
to restrict this option to. This only applies to options of typeCHANNEL
.- Parameters:
channelTypes
- TheChannelTypes
to restrict this option to or empty array to accept allChannelTypes
- Returns:
- The OptionData instance, for chaining
- Throws:
IllegalArgumentException
-- If
type of this option
is notCHANNEL
- If
channelTypes
containnull
- If
channelTypes
contains non-guild channels
- If
-
setChannelTypes
Configure theChannelTypes
to restrict this option to. This only applies to options of typeCHANNEL
.- Parameters:
channelTypes
- TheChannelTypes
to restrict this option to or empty collection to accept allChannelTypes
- Returns:
- The OptionData instance, for chaining
- Throws:
IllegalArgumentException
-- If
type of this option
is notCHANNEL
- If
channelTypes
is null - If
channelTypes
containnull
- If
channelTypes
contains non-guild channels
- If
-
setMinValue
Configure the minimal value which can be provided for this option.- Parameters:
value
- The minimal value which can be provided for this option.- Returns:
- The OptionData instance, for chaining
- Throws:
IllegalArgumentException
-- If
type of this option
is notINTEGER
orNUMBER
- If
value
is less thanMIN_NEGATIVE_NUMBER
- If
-
setMinValue
Configure the minimal value which can be provided for this option.- Parameters:
value
- The minimal value which can be provided for this option.- Returns:
- The OptionData instance, for chaining
- Throws:
IllegalArgumentException
-- If
type of this option
is notNUMBER
- If
value
is less thanMIN_NEGATIVE_NUMBER
- If
-
setMaxValue
Configure the maximal value which can be provided for this option.- Parameters:
value
- The maximal value which can be provided for this option.- Returns:
- The OptionData instance, for chaining
- Throws:
IllegalArgumentException
-- If
type of this option
is notINTEGER
orNUMBER
- If
value
is greater thanMAX_POSITIVE_NUMBER
- If
-
setMaxValue
Configure the maximal value which can be provided for this option.- Parameters:
value
- The maximal value which can be provided for this option.- Returns:
- The OptionData instance, for chaining
- Throws:
IllegalArgumentException
-- If
type of this option
is notNUMBER
- If
value
is greater thanMAX_POSITIVE_NUMBER
- If
-
setRequiredRange
Configure the minimal and maximal value which can be provided for this option.- Parameters:
minValue
- The minimal value which can be provided for this option.maxValue
- The maximal value which can be provided for this option.- Returns:
- The OptionData instance, for chaining
- Throws:
IllegalArgumentException
-- If
type of this option
is notINTEGER
orNUMBER
- If
minValue
is less than or not equal toMIN_NEGATIVE_NUMBER
- If
maxValue
is greater thanMAX_POSITIVE_NUMBER
- If
-
setRequiredRange
Configure the minimal and maximal value which can be provided for this option.- Parameters:
minValue
- The minimal value which can be provided for this option.maxValue
- The maximal value which can be provided for this option.- Returns:
- The OptionData instance, for chaining
- Throws:
IllegalArgumentException
-- If
type of this option
is notNUMBER
- If
minValue
is less than or not equal toMIN_NEGATIVE_NUMBER
- If
maxValue
is greater thanMAX_POSITIVE_NUMBER
- If
-
setMinLength
Configure the minimum length for strings which can be provided for this option.- Parameters:
minLength
- The minimum length for strings which can be provided for this option.- Returns:
- The OptionData instance, for chaining
- Throws:
IllegalArgumentException
-- If
type of this option
is notSTRING
- If
minLength
is not positive
- If
-
setMaxLength
Configure the maximum length for strings which can be provided for this option.- Parameters:
maxLength
- The maximum length for strings which can be provided for this option.- Returns:
- The OptionData instance, for chaining
- Throws:
IllegalArgumentException
-- If
type of this option
is notSTRING
- If
maxLength
is not positive or greater than 6000
- If
-
setRequiredLength
Configure the minimum and maximum length for strings which can be provided for this option.- Parameters:
minLength
- The minimum length for strings which can be provided for this option.maxLength
- The maximum length for strings which can be provided for this option.- Returns:
- The OptionData instance, for chaining
- Throws:
IllegalArgumentException
-- If
type of this option
is notSTRING
- If
minLength
is greater thanmaxLength
- If
-
addChoice
Add a predefined choice for this option.
The user can only provide one of the choices and cannot specify any other value.- Parameters:
name
- The name used in the client, up to 100 characters long, as defined byMAX_CHOICE_NAME_LENGTH
value
- The value received inOptionMapping
- Returns:
- The OptionData instance, for chaining
- Throws:
IllegalArgumentException
-- If
name
is null, empty, or greater than 100 characters long - If
value
is less thanMIN_NEGATIVE_NUMBER
or greater thanMAX_POSITIVE_NUMBER
- If adding this choice would exceed 25 choices, as defined by
MAX_CHOICES
- If the
OptionType
is notOptionType.NUMBER
- If the option is auto-complete enabled
- If
-
addChoice
Add a predefined choice for this option.
The user can only provide one of the choices and cannot specify any other value.- Parameters:
name
- The name used in the clientvalue
- The value received inOptionMapping
- Returns:
- The OptionData instance, for chaining
- Throws:
IllegalArgumentException
-- If
name
is null, empty, or greater than 100 characters long - If
value
is less thanMIN_NEGATIVE_NUMBER
or greater thanMAX_POSITIVE_NUMBER
- If adding this choice would exceed 25 choices, as defined by
MAX_CHOICES
- If the
OptionType
is notOptionType.INTEGER
- If the option is auto-complete enabled
- If
-
addChoice
Add a predefined choice for this option.
The user can only provide one of the choices and cannot specify any other value.- Parameters:
name
- The name used in the clientvalue
- The value received inOptionMapping
- Returns:
- The OptionData instance, for chaining
- Throws:
IllegalArgumentException
-- If
name
is null, empty, or greater than 100 characters long - If
value
is less thanMIN_NEGATIVE_NUMBER
or greater thanMAX_POSITIVE_NUMBER
- If adding this choice would exceed 25 choices, as defined by
MAX_CHOICES
- If the
OptionType
is notOptionType.STRING
- If the option is auto-complete enabled
- If
-
addChoices
Adds up to 25 predefined choices for this option.
The user can only provide one of the choices and cannot specify any other value.- Parameters:
choices
- The choices to add- Returns:
- The OptionData instance, for chaining
- Throws:
IllegalArgumentException
-- If the
OptionType
does notsupport choices
- If the provided
choices
are null - If the amount of
choices
provided, when combined with the already set choices, would be greater than 25, as defined byMAX_CHOICES
- If the
OptionType
of the choices is not eitherOptionType.INTEGER
,OptionType.STRING
orOptionType.NUMBER
- If the option is auto-complete enabled
- If the
-
addChoices
Adds up to 25 predefined choices for this option.
The user can only provide one of the choices and cannot specify any other value.- Parameters:
choices
- The choices to add- Returns:
- The OptionData instance, for chaining
- Throws:
IllegalArgumentException
-- If the
OptionType
does notsupport choices
- If the provided
choices
collection is null - If the provided
choices
are null - If the amount of
choices
provided, when combined with the already set choices, would be greater than 25, as defined byMAX_CHOICES
- If the
OptionType
of the choices is not eitherOptionType.INTEGER
,OptionType.STRING
orOptionType.NUMBER
- If the option is auto-complete enabled
- If the
-
toData
Description copied from interface:SerializableData
SerializedDataObject
for this object.- Specified by:
toData
in interfaceSerializableData
- Returns:
DataObject
-
fromData
Parses the provided serialization back into an OptionData instance.
This is the reverse function fortoData()
.- Parameters:
json
- The serializedDataObject
representing the option- Returns:
- The parsed OptionData instance, which can be further configured through setters
- Throws:
ParsingException
- If the serialized object is missing required fieldsIllegalArgumentException
- If any of the values are failing the respective checks such as length
-
fromOption
Converts the providedCommand.Option
into a OptionData instance.- Parameters:
option
- The option to convert- Returns:
- An instance of OptionData
- Throws:
IllegalArgumentException
- If null is provided or the option has illegal configuration
-