Interface Command<M>

  • Type Parameters:
    M - the class of the messages for which this command can be triggered

    public interface Command<M>
    A command that can be triggered by messages of the given type.
    • Field Detail

      • PARAMETER_SEPARATOR_CHARACTER

        static final String PARAMETER_SEPARATOR_CHARACTER
        The regex pattern string for one parameter separator character. It matches one whitespaces except newline.
        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 except newlines.
    • Method Detail

      • getAliases

        default List<String> getAliases()
        Returns the aliases for this command.

        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 by Command or Cmd 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. Currently this description is used nowhere, but can for example be displayed in an own help command.

        The default implementation of this method returns the description configured using the @Description annotation. If no description is configured by annotation, an empty Optional 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.

        • Placeholders for free text without whitespaces (in the value) look like <my placeholder>
        • One placeholder for free text with whitespaces (in the value) is allowed as effectively last parameter and looks like <my placeholder...>
        • Literal parameters look like 'literal'
        • Optional parts are enclosed in square brackets like [<optional placeholder>]
        • Alternatives are enclosed in parentheses and are separated by pipe characters like ('all' | 'some' | 'none')
        • Whitespace characters between the defined tokens are optional and ignored
        Examples:
        • @Usage("<coin type> <amount>")
        • @Usage("['all'] ['exact']")
        • @Usage("[<text...>]")
        • @Usage("(<targetLanguage> '|' | <sourceLanguage> <targetLanguage>) <text...>")

        The default implementation of this method returns the usage configured using the @Usage annotation. If no usage is configured by annotation, an empty Optional 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 own Restriction implementation. For the latter also helpers like ChannelJavacord, RoleJavacord, ServerJavacord, UserJavacord, AllOf, AnyOf, or NoneOf 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 the Everyone 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​(M incomingMessage,
                     String prefix,
                     String usedAlias,
                     String parameterString)
        Executes this command. The given parameter string can be split into single parameters using getParameters(String, int) or semantically parsed into parameters using the ParameterParser.
        Parameters:
        incomingMessage - the incoming message that caused this command to be executed
        prefix - the command prefix that was used to trigger this command
        usedAlias - the alias that was used to trigger this command
        parameterString - the remaining text after the command prefix and alias
        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 non-newline whitespace characters. If you expect three parameters, you should set maxParameters 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 parameters
        maxParameters - 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