Skip to content

Commit

Permalink
Adds support for min_length and max_length for String in Slash Co…
Browse files Browse the repository at this point in the history
…mmands.
  • Loading branch information
ShindouMihou committed Jul 10, 2022
1 parent 2bdafc6 commit 5a742ef
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,20 @@ public interface SlashCommandOption {
*/
Optional<Double> getDecimalMaxValue();

/**
* If the option is an {@link SlashCommandOptionType#STRING} type, the minimum allowed length.
*
* @return The minimum allowed length.
*/
Optional<Long> getStringMinLength();

/**
* If the option is an {@link SlashCommandOptionType#STRING} type, the maximum allowed length.
*
* @return The maximum allowed length.
*/
Optional<Long> getStringMaxLength();

/**
* Create a new slash command option to be used with a slash command builder.
* This is a convenience method.
Expand Down Expand Up @@ -444,6 +458,32 @@ static SlashCommandOption createStringOption(String name,
.build();
}

/**
* Create a new {@link SlashCommandOptionType#STRING} slash command option to be used with a slash command builder.
* This is a convenience method.
*
* @param name The name of the option.
* @param description The description of the option.
* @param required Whether this option is required
* @param minLength The minimum allowed length.
* @param maxLength The maximum allowed length.
* @return The new slash command option builder.
*/
static SlashCommandOption createStringOption(String name,
String description,
boolean required,
long minLength,
long maxLength) {
return new SlashCommandOptionBuilder()
.setType(SlashCommandOptionType.STRING)
.setName(name)
.setDescription(description)
.setRequired(required)
.setStringMinLength(minLength)
.setStringMaxLength(maxLength)
.build();
}

/**
* Create a new {@link SlashCommandOptionType#STRING} slash command option to be used with a slash command builder.
* This is a convenience method.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,29 @@ public SlashCommandOptionBuilder setDecimalMaxValue(double decimalMaxValue) {
return this;
}

/**
* Sets the {@link SlashCommandOptionType#STRING} min length for the slash command option.
*
* @param stringMinLength The string min length.
* @return The current instance in order to chain call methods
*/
public SlashCommandOptionBuilder setStringMinLength(long stringMinLength) {
delegate.setStringMinLength(stringMinLength);
return this;
}


/**
* Sets the {@link SlashCommandOptionType#STRING} max length for the slash command option.
*
* @param stringMaxLength The string min length.
* @return The current instance in order to chain call methods
*/
public SlashCommandOptionBuilder setStringMaxLength(long stringMaxLength) {
delegate.setStringMaxLength(stringMaxLength);
return this;
}

/**
* Builds the slash command option.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,20 @@ public interface SlashCommandOptionBuilderDelegate {
*/
void setDecimalMaxValue(double decimalMaxValue);

/**
* Sets the {@link SlashCommandOptionType#STRING} min length for the slash command option.
*
* @param stringMinLength The string min length.
*/
void setStringMinLength(long stringMinLength);

/**
* Sets the {@link SlashCommandOptionType#STRING} max length for the slash command option.
*
* @param stringMaxLength The string min length.
*/
void setStringMaxLength(long stringMaxLength);

/**
* Build the slash command option.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public class SlashCommandOptionBuilderDelegateImpl implements SlashCommandOption
private Double decimalMinValue;
private Double decimalMaxValue;

private Long stringMinLength;
private Long stringMaxLength;

@Override
public void setType(SlashCommandOptionType type) {
this.type = type;
Expand Down Expand Up @@ -129,10 +132,20 @@ public void setDecimalMaxValue(double decimalMaxValue) {
this.decimalMaxValue = decimalMaxValue;
}

@Override
public void setStringMinLength(long stringMinLength) {
this.stringMinLength = stringMinLength;
}

@Override
public void setStringMaxLength(long stringMaxLength) {
this.stringMaxLength = stringMaxLength;
}

@Override
public SlashCommandOption build() {
return new SlashCommandOptionImpl(type, name, nameLocalizations, description, descriptionLocalizations,
required, autocompletable, choices, options, channelTypes, longMinValue, longMaxValue,
decimalMinValue, decimalMaxValue);
decimalMinValue, decimalMaxValue, stringMinLength, stringMaxLength);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public class SlashCommandOptionImpl implements SlashCommandOption {
private final Long longMaxValue;
private final Double decimalMinValue;
private final Double decimalMaxValue;
private final Long stringMaxLength;
private final Long stringMinLength;
private final boolean autocomplete;

/**
Expand Down Expand Up @@ -79,12 +81,25 @@ public SlashCommandOptionImpl(JsonNode data) {
longMaxValue = data.has("max_value") ? data.get("max_value").asLong() : null;
decimalMinValue = null;
decimalMaxValue = null;
stringMinLength = null;
stringMaxLength = null;
} else if (type == SlashCommandOptionType.DECIMAL) {
decimalMinValue = data.has("min_value") ? data.get("min_value").asDouble() : null;
decimalMaxValue = data.has("max_value") ? data.get("max_value").asDouble() : null;
longMinValue = null;
longMaxValue = null;
stringMinLength = null;
stringMaxLength = null;
} else if (type == SlashCommandOptionType.STRING) {
stringMinLength = data.has("min_length") ? data.get("min_length").asLong() : null;
stringMaxLength = data.has("max_length") ? data.get("max_length").asLong() : null;
longMinValue = null;
longMaxValue = null;
decimalMinValue = null;
decimalMaxValue = null;
} else {
stringMinLength = null;
stringMaxLength = null;
longMinValue = null;
longMaxValue = null;
decimalMinValue = null;
Expand All @@ -109,6 +124,8 @@ public SlashCommandOptionImpl(JsonNode data) {
* @param longMaxValue The {@link SlashCommandOptionType#LONG} max value
* @param decimalMinValue The {@link SlashCommandOptionType#DECIMAL} min value
* @param decimalMaxValue The {@link SlashCommandOptionType#DECIMAL} max value
* @param stringMinLength The {@link SlashCommandOptionType#STRING} min length.
* @param stringMaxLength The {@link SlashCommandOptionType#STRING} max length.
*/
public SlashCommandOptionImpl(
SlashCommandOptionType type,
Expand All @@ -124,7 +141,9 @@ public SlashCommandOptionImpl(
Long longMinValue,
Long longMaxValue,
Double decimalMinValue,
Double decimalMaxValue
Double decimalMaxValue,
Long stringMinLength,
Long stringMaxLength
) {
this.type = type;
this.name = name;
Expand All @@ -140,6 +159,8 @@ public SlashCommandOptionImpl(
this.longMaxValue = longMaxValue;
this.decimalMinValue = decimalMinValue;
this.decimalMaxValue = decimalMaxValue;
this.stringMinLength = stringMinLength;
this.stringMaxLength = stringMaxLength;
}

@Override
Expand Down Expand Up @@ -212,6 +233,16 @@ public Optional<Double> getDecimalMaxValue() {
return Optional.ofNullable(decimalMaxValue);
}

@Override
public Optional<Long> getStringMinLength() {
return Optional.ofNullable(stringMinLength);
}

@Override
public Optional<Long> getStringMaxLength() {
return Optional.ofNullable(stringMaxLength);
}

/**
* Creates a json node with the option's data.
*
Expand Down Expand Up @@ -250,6 +281,13 @@ public JsonNode toJsonNode() {
if (decimalMaxValue != null) {
node.put("max_value", decimalMaxValue);
}
} else if (type == SlashCommandOptionType.STRING) {
if (stringMinLength != null) {
node.put("min_length", stringMinLength);
}
if (stringMaxLength != null) {
node.put("max_length", stringMaxLength);
}
}
if (!nameLocalizations.isEmpty()) {
ObjectNode nameLocalizationsJsonObject = node.putObject("name_localizations");
Expand Down

0 comments on commit 5a742ef

Please sign in to comment.