Interface Command<M>
-
- Type Parameters:
M
- the class of the messages for which this command can be triggered
- All Known Subinterfaces:
SlashCommandJavacord
public interface Command<M>
A command that can be triggered by messages of the given type.
-
-
Field Summary
Fields Modifier and Type Field Description static String
PARAMETER_SEPARATOR_CHARACTER
The regex pattern string for one parameter separator character.static Pattern
PARAMETER_SEPARATOR_PATTERN
The pattern that is used to split parameters.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description void
execute(CommandContext<? extends M> commandContext)
Executes this command.default List<String>
getAliases()
Returns the aliases for this command.default Optional<String>
getDescription()
Returns the description of this command.static String[]
getParameters(String parameterString, int maxParameters)
Returns an array of parameters from the given parameter string.default RestrictionChainElement
getRestrictionChain()
Returns the restriction rules chain for this command.default Optional<String>
getUsage()
Returns the usage of this command.default boolean
isAsynchronous()
Returns whether this command should be executed asynchronously.
-
-
-
Field Detail
-
PARAMETER_SEPARATOR_CHARACTER
static final String PARAMETER_SEPARATOR_CHARACTER
The regex pattern string for one parameter separator character. It matches one whitespace character.- See Also:
- Constant Field Values
-
PARAMETER_SEPARATOR_PATTERN
static final Pattern PARAMETER_SEPARATOR_PATTERN
The pattern that is used to split parameters. It matches an arbitrary amount of whitespaces.
-
-
Method Detail
-
getAliases
default List<String> getAliases()
Returns the aliases for this command.When injecting a
List<SlashCommandBuilder>
anywhere, all aliases of commands implementingSlashCommandJavacord
have to follow a pre-defined format that is described atSlashCommandJavacord
.The default implementation of this method returns the aliases configured using the
@Alias
annotation. If no alias is configured by annotation, the class name, stripped byCommand
orCmd
suffix and / or prefix if present and the first letter lowercased is used as default.If this method is overwritten and there are annotations, the method overwrite takes precedence.
- Returns:
- the aliases for this command
- See Also:
@Alias
-
getDescription
default Optional<String> getDescription()
Returns the description of this command. This description can for example be displayed in an own help command.When injecting a
List<SlashCommandBuilder>
anywhere, all commands implementingSlashCommandJavacord
have to provide a description.The default implementation of this method returns the description configured using the
@Description
annotation. If no description is configured by annotation, an emptyOptional
is used as default.If this method is overwritten and the annotation is present, the method overwrite takes precedence.
- Returns:
- the description of this command
- See Also:
@Description
-
getUsage
default Optional<String> getUsage()
Returns the usage of this command. This usage can for example be displayed in an own help command.When using the
ParameterParser
, the usage string has to follow a pre-defined format that is described there.The default implementation of this method returns the usage configured using the
@Usage
annotation. If no usage is configured by annotation, an emptyOptional
is used as default.If this method is overwritten and the annotation is present, the method overwrite takes precedence.
- Returns:
- the usage of this command
- See Also:
@Usage
,ParameterParser
-
getRestrictionChain
default RestrictionChainElement getRestrictionChain()
Returns the restriction rules chain for this command.Complex boolean logic can either be formulated using the methods of
RestrictionChainElement
or by providing an ownRestriction
implementation. For the latter also helpers likeChannelJavacord
,RoleJavacord
,ServerJavacord
,UserJavacord
,AllOf
,AnyOf
, orNoneOf
can be used as super classes.The default implementation of this method returns the restrictions configured using the
@RestrictedTo
annotation combined according to the@RestrictionPolicy
annotation. If no@RestrictedTo
annotation is present, this method behaves as if theEveryone
restriction was applied.If this method is overwritten and the annotation is present, the method overwrite takes precedence.
- Returns:
- the restriction rules for this command
- See Also:
@RestrictedTo
,@RestrictionPolicy
-
isAsynchronous
default boolean isAsynchronous()
Returns whether this command should be executed asynchronously.How exactly this is implemented is up to the command handler that evaluates this command. Usually the command will be execute in some thread pool. But it would also be valid for a command handler to execute each asynchronous command execution in a new thread, so using this can add significant overhead if overused. As long as a command is not doing long-running or blocking operations it might be a good idea to not execute the command asynchronously. But if long-running or blocking operations are done in the command code directly, depending on the underlying message framework it might be a good idea to execute the command asynchronously to not block message dispatching which could introduce serious lag to the command execution.
As the command executions are potentially done on different threads, special care must be taken if the command holds state, to make sure this state is accessed in a thread-safe manner. This can of course also happen without the command being configured asynchronously if the underlying message framework dispatches message events on different threads.
The default implementation of this method returns whether the
@Asynchronous
annotation is present on this command.If this method is overwritten and the annotation is present, the method overwrite takes precedence.
- Returns:
- whether this command should be executed asynchronously
- See Also:
@Asynchronous
-
execute
void execute(CommandContext<? extends M> commandContext)
Executes this command. The parameter string in the given command context can be split into single parameters usinggetParameters(String, int)
or semantically parsed into parameters using theParameterParser
.- Parameters:
commandContext
- the command context, usually fully populated but not necessarily- See Also:
getParameters(String, int)
,ParameterParser
-
getParameters
static String[] getParameters(String parameterString, int maxParameters)
Returns an array of parameters from the given parameter string. The parameter string is split at any sequence of whitespace characters. If you expect three parameters, you should setmaxParameters
to four, so you can easily test the length of the returned array whether too many parameters were given to the command.For a syntactically and semantically parsing of the parameter string, you can have a look at the
ParameterParser
for which you can define the command syntax as pattern which is then parsed and returned accordingly.- Parameters:
parameterString
- the parameter string to split into single parametersmaxParameters
- the maximum amount of parameters to return, the last will hold all remaining text- Returns:
- an array of parameters from the given parameter string
- See Also:
ParameterParser
-
-