-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add string bounds for slash commands (#45)
* Add string length * Throw if `@Length` is used on a non STRING option type * Throw if `@LongRange`/`@DoubleRange` is used on a non INTEGER/NUMBER option type * Update docs
- Loading branch information
Showing
6 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/main/java/com/freya02/botcommands/api/application/slash/annotations/Length.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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} | ||
* <br><b>This is only for string types !</b> | ||
*/ | ||
@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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/test/java/com/freya02/botcommands/test/commands/slash/SlashLength.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |