diff --git a/src/main/java/com/freya02/botcommands/api/application/slash/annotations/DoubleRange.java b/src/main/java/com/freya02/botcommands/api/application/slash/annotations/DoubleRange.java index 3be077d45..c7785d65b 100644 --- a/src/main/java/com/freya02/botcommands/api/application/slash/annotations/DoubleRange.java +++ b/src/main/java/com/freya02/botcommands/api/application/slash/annotations/DoubleRange.java @@ -9,6 +9,7 @@ /** * Allows setting minimum and maximum values on the specified {@link AppOption} + *
This is only for floating point number types ! */ @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.PARAMETER}) diff --git a/src/main/java/com/freya02/botcommands/api/application/slash/annotations/Length.java b/src/main/java/com/freya02/botcommands/api/application/slash/annotations/Length.java new file mode 100644 index 000000000..d8d132853 --- /dev/null +++ b/src/main/java/com/freya02/botcommands/api/application/slash/annotations/Length.java @@ -0,0 +1,27 @@ +package com.freya02.botcommands.api.application.slash.annotations; + +import com.freya02.botcommands.api.application.annotations.AppOption; +import net.dv8tion.jda.api.interactions.commands.build.OptionData; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Allows setting minimum and maximum string length on the specified {@link AppOption} + *
This is only for string types ! + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.PARAMETER) +public @interface Length { + /** + * @return The minimum value of this parameter (included) + */ + int min() default 1; + + /** + * @return The maximum value of this parameter (included) + */ + int max() default OptionData.MAX_STRING_OPTION_LENGTH; +} diff --git a/src/main/java/com/freya02/botcommands/api/application/slash/annotations/LongRange.java b/src/main/java/com/freya02/botcommands/api/application/slash/annotations/LongRange.java index b8ac6b793..ea4e2c7cb 100644 --- a/src/main/java/com/freya02/botcommands/api/application/slash/annotations/LongRange.java +++ b/src/main/java/com/freya02/botcommands/api/application/slash/annotations/LongRange.java @@ -9,6 +9,7 @@ /** * Allows setting minimum and maximum values on the specified {@link AppOption} + *
This is only for integer types ! */ @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.PARAMETER}) diff --git a/src/main/java/com/freya02/botcommands/internal/application/slash/SlashCommandParameter.java b/src/main/java/com/freya02/botcommands/internal/application/slash/SlashCommandParameter.java index 8dc217dee..01f6f0404 100644 --- a/src/main/java/com/freya02/botcommands/internal/application/slash/SlashCommandParameter.java +++ b/src/main/java/com/freya02/botcommands/internal/application/slash/SlashCommandParameter.java @@ -2,6 +2,7 @@ import com.freya02.botcommands.api.application.slash.DefaultValueSupplier; import com.freya02.botcommands.api.application.slash.annotations.DoubleRange; +import com.freya02.botcommands.api.application.slash.annotations.Length; import com.freya02.botcommands.api.application.slash.annotations.LongRange; import com.freya02.botcommands.api.parameters.SlashParameterResolver; import com.freya02.botcommands.api.prefixed.annotations.TextOption; @@ -10,6 +11,7 @@ import gnu.trove.map.TLongObjectMap; import gnu.trove.map.hash.TLongObjectHashMap; import net.dv8tion.jda.api.entities.channel.ChannelType; +import net.dv8tion.jda.api.interactions.commands.OptionType; import net.dv8tion.jda.api.interactions.commands.build.OptionData; import java.lang.reflect.Parameter; @@ -18,6 +20,7 @@ public class SlashCommandParameter extends ApplicationCommandVarArgParameter { private final Number minValue, maxValue; + private final int minLength, maxLength; private final EnumSet channelTypes = EnumSet.noneOf(ChannelType.class); private final TLongObjectMap defaultOptionSupplierMap = new TLongObjectHashMap<>(); @@ -29,11 +32,19 @@ public SlashCommandParameter(Parameter parameter, int index) { final LongRange longRange = ReflectionUtils.getLongRange(parameter); if (longRange != null) { + if (getResolver() == null || getResolver().getOptionType() != OptionType.INTEGER) { + throw new IllegalStateException("Cannot use @" + LongRange.class.getSimpleName() + " on a option that doesn't accept an integer"); + } + minValue = longRange.from(); maxValue = longRange.to(); } else { final DoubleRange doubleRange = ReflectionUtils.getDoubleRange(parameter); if (doubleRange != null) { + if (getResolver() == null || getResolver().getOptionType() != OptionType.NUMBER) { + throw new IllegalStateException("Cannot use @" + DoubleRange.class.getSimpleName() + " on a option that doesn't accept a floating point number"); + } + minValue = doubleRange.from(); maxValue = doubleRange.to(); } else { @@ -47,6 +58,19 @@ public SlashCommandParameter(Parameter parameter, int index) { } } + final Length length = parameter.getAnnotation(Length.class); + if (length != null) { + if (getResolver() == null || getResolver().getOptionType() != OptionType.STRING) { + throw new IllegalStateException("Cannot use @" + Length.class.getSimpleName() + " on a option that doesn't accept a string"); + } + + minLength = length.min(); + maxLength = length.max(); + } else { + minLength = 1; + maxLength = OptionData.MAX_STRING_OPTION_LENGTH; + } + Collections.addAll(channelTypes, AnnotationUtils.getEffectiveChannelTypes(parameter)); } @@ -62,6 +86,14 @@ public Number getMaxValue() { return maxValue; } + public int getMinLength() { + return minLength; + } + + public int getMaxLength() { + return maxLength; + } + public TLongObjectMap getDefaultOptionSupplierMap() { return defaultOptionSupplierMap; } diff --git a/src/main/java/com/freya02/botcommands/internal/application/slash/SlashUtils.java b/src/main/java/com/freya02/botcommands/internal/application/slash/SlashUtils.java index 4c93f2075..6d4f9e872 100644 --- a/src/main/java/com/freya02/botcommands/internal/application/slash/SlashUtils.java +++ b/src/main/java/com/freya02/botcommands/internal/application/slash/SlashUtils.java @@ -88,6 +88,8 @@ public static List getMethodOptions(@NotNull BContext context, @Null } else if (optionType == OptionType.NUMBER) { data.setMinValue(parameter.getMinValue().doubleValue()); data.setMaxValue(parameter.getMaxValue().doubleValue()); + } else if (optionType == OptionType.STRING) { + data.setRequiredLength(parameter.getMinLength(), parameter.getMaxLength()); } if (applicationOptionData.hasAutocompletion()) { diff --git a/src/test/java/com/freya02/botcommands/test/commands/slash/SlashLength.java b/src/test/java/com/freya02/botcommands/test/commands/slash/SlashLength.java new file mode 100644 index 000000000..e9ad1a8a7 --- /dev/null +++ b/src/test/java/com/freya02/botcommands/test/commands/slash/SlashLength.java @@ -0,0 +1,14 @@ +package com.freya02.botcommands.test.commands.slash; + +import com.freya02.botcommands.api.application.ApplicationCommand; +import com.freya02.botcommands.api.application.annotations.AppOption; +import com.freya02.botcommands.api.application.slash.GuildSlashEvent; +import com.freya02.botcommands.api.application.slash.annotations.JDASlashCommand; +import com.freya02.botcommands.api.application.slash.annotations.Length; + +public class SlashLength extends ApplicationCommand { + @JDASlashCommand(name = "length") + public void onSlashLength(GuildSlashEvent event, @AppOption @Length(max = 6) String inviteCode) { + event.reply("Invite code: " + inviteCode).queue(); + } +} \ No newline at end of file