Argument types - S3nS3IW00/JCommands GitHub Wiki
There are a lot of useful pre-written argument types that can be used while creating a command. Every argument has a result that can be optained inside the action listener. The result depends on the type that is specified in the argument.
The base of the arguments is the Argument<O>
class that is a generic abstract class. The class has a generic parameter that is the type of the argument's result.
There are two main type of arguments: SubArgument<T, R>
and InputArgument<I, O>
Argument that can contains more arguments. Accepts exactly the same input as its name and returns it as String value. The argument is described here in detail.
Argument that accepts a specific type of input. The InputArgument<I, O>
has two generic types. The I
is the input type, the O
is the output type. The possible types of the input can be found here. The return value can be literally anything, but for types that do not supported by default by JCommands require a custom converter. The types supported by default can be found here.
It can be marked as optional with InputArgument#setOptional()
method. Optional arguments must be the last in the list of arguments, so non-optional argument cannot be added after an optional argument. Optional arguments return as an empty Optional<O>
when they haven't been specified or with the value in it in the action event.
Getting optional argument in action listener:
Optional<Attachment> attachment = event.getArguments()[0];
There is one special type of this argument: ComboArgument
It contains a list of key-value pairs. The argument accepts one of the value as input and returns it. The value's type can be only STRING
or LONG
from SlashCommandOptionType
.
For example create a combo argument that contains a few color that the user can choose from:
ComboArgument comboArgument = new ComboArgument("color", "Pick a color", SlashCommandOptionType.LONG);
comboArgument.addChoice("red", 0);
comboArgument.addChoice("green", 1);
comboArgument.addChoice("blue", 2);
The following table shows all the pre-written arguments and its result that can be found in this resource by default. The most of these arguments also expects a generic type, that is the type of the result. If the result type is different than the input type, a converter has to be specified, that converts value from the input type to the result type.
Argument | Valid values | Result |
---|---|---|
StringArgument<O> | Anything The length can be limited with StringLengthPredicate validation |
The exact value as string |
NumberArgument<O> | Long The value can be limited with NumberPredicate validation. |
The exact value as long |
URLArgument | A valid URL | The URL of the value |
MentionArgument<O> | A user mention | The mentioned User |
ChannelArgument<O> | A channel mention | The mentioned ServerChannel |
RoleArgument<O> | A role mention | The mentioned Role |
AttachmentArgument<O> | An attachment The file's size can be limited with whenAboveMaxSize(int) and whenAboveMaxSizeInMb(double) The valid file extensions can be limited with whenInvalidExtension(Set<String>) |
The uploaded Attachment |