Class MessageContextImpl

All Implemented Interfaces:
AccessContext, AccessValidator, ChannelAccessContext, ChannelAccessValidator, ChannelEventContext, EventContext, MessageBasedContext, MessageCreateEventContext, MessageEventContext, MessageIncludedContext, RepliableContext, CommandContext, MessageCommandContext, InstrumentedContext, LazyContext

public final class MessageContextImpl extends AbstractRepliableContext<E> implements MessageCommandContext
Context object for invocations performed through text messages.
Since:
1.0
Version:
1.0
  • Field Details

    • METRIC_NAME_PREFIX

      public static final String METRIC_NAME_PREFIX
      The prefix for metrics in this class.
      See Also:
    • METRIC_NAME_PREFIX_ARGUMENT

      public static final String METRIC_NAME_PREFIX_ARGUMENT
      The prefix for argument parsing metrics.
      See Also:
    • command

      protected final Command<?> command
      The invoked command.
  • Constructor Details

    • MessageContextImpl

      public MessageContextImpl(MessageCreateEvent event, Invocation invocation, Command<?> command, StringSplitter.Async.Iterator args, AccessManager accessManager, ReplyManager replyManager) throws ResultException
      Initializes a new context.
      Parameters:
      event - The event that triggered the invocation.
      invocation - The invocation that triggered execution.
      command - The invoked command.
      args - The raw arguments received.
      accessManager - The access manager to use.
      replyManager - The reply manager to use.
      Throws:
      ResultException - if there is a mismatch between parameters and arguments.
  • Method Details

    • initArgs

      protected Mono<Void> initArgs()
      Performs any required initialization of received arguments before parsing starts.
      Returns:
      A mono that completes when initialization is done.
    • tagType

      public Metrics.Tag.Type tagType()
      Description copied from interface: InstrumentedContext
      Determines the value for the type tag.
      Specified by:
      tagType in interface InstrumentedContext
      Returns:
      The tag.
    • getStringArgument

      protected Mono<String> getStringArgument(String name)
      Retrieves the string argument associated with the parameter of the given name.
      Parameters:
      name - The parameter name.
      Returns:
      The associated string argument. May be empty if missing.
    • getBooleanArgument

      protected Mono<Boolean> getBooleanArgument(String name)
      Retrieves the boolean argument associated with the parameter of the given name.
      Parameters:
      name - The parameter name.
      Returns:
      The associated boolean argument. May be empty if missing or fail with a InvalidArgumentException if the value received is invalid.
    • getIntegerArgument

      protected Mono<Long> getIntegerArgument(String name) throws InvalidArgumentException
      Retrieves the integer argument associated with the parameter of the given name.
      Parameters:
      name - The parameter name.
      Returns:
      The associated integer argument. May be empty if missing or fail with a InvalidArgumentException if the value received is invalid.
      Throws:
      InvalidArgumentException - if the received argument is not a valid integer.
    • getFloatArgument

      protected Mono<Double> getFloatArgument(String name) throws InvalidArgumentException
      Retrieves the float argument associated with the parameter of the given name.
      Parameters:
      name - The parameter name.
      Returns:
      The associated float argument. May be empty if missing or fail with a InvalidArgumentException if the value received is invalid.
      Throws:
      InvalidArgumentException - if the received argument is not a valid float.
    • getSnowflakeArgument

      protected Mono<Snowflake> getSnowflakeArgument(String name, SnowflakeParser.Type type) throws InvalidArgumentException
      Retrieves the snowflake argument associated with the parameter of the given name.
      Parameters:
      name - The parameter name.
      type - The ID type.
      Returns:
      The associated snowflake argument. May be empty if missing or fail with a InvalidArgumentException if the value received is invalid.
      Throws:
      InvalidArgumentException - if the received argument is not a valid snowflake.
    • getUserArgument

      protected Mono<User> getUserArgument(String name)
      Retrieves the user argument associated with the parameter of the given name.
      Parameters:
      name - The parameter name.
      Returns:
      The associated user argument. May be empty if missing or fail with a InvalidArgumentException if the value received is invalid.
    • getRoleArgument

      protected Mono<Role> getRoleArgument(String name)
      Retrieves the role argument associated with the parameter of the given name.
      Parameters:
      name - The parameter name.
      Returns:
      The associated role argument. May be empty if missing or fail with a InvalidArgumentException if the value received is invalid.
    • getChannelArgument

      protected <C extends @NonNull Channel> Mono<C> getChannelArgument(String name, Class<C> type)
      Retrieves the channel argument associated with the parameter of the given name.
      Type Parameters:
      C - The channel type.
      Parameters:
      name - The parameter name.
      type - The channel type.
      Returns:
      The associated channel argument. May be empty if missing or fail with a InvalidArgumentException if the value received is invalid.
    • getAttachmentArgument

      protected Mono<Attachment> getAttachmentArgument(String name)
      Retrieves the attachment argument associated with the parameter of the given name.
      Parameters:
      name - The parameter name.
      Returns:
      The associated attachment argument. May be empty if missing.
    • argString

      public String argString()
      Description copied from interface: MessageCommandContext
      Retrieves the raw argument string as it was received, before being split.

      Does not include the command name(s) (see CommandContext.invocation() for that), only the arguments that are later used for MessageCommandContext.rawArgs().

      Specified by:
      argString in interface MessageCommandContext
      Returns:
      The received arguments before splitting.
    • rawArgs

      public List<String> rawArgs()
      Description copied from interface: MessageCommandContext
      Retrieves the raw arguments received before parsing, in the order that they were received.
      Specified by:
      rawArgs in interface MessageCommandContext
      Returns:
      The raw arguments received. The returned list is unmodifiable.
    • rawArgMap

      public Map<String,String> rawArgMap()
      Description copied from interface: MessageCommandContext
      Retrieves the raw arguments received, keyed by the corresponding parameter name.
      Specified by:
      rawArgMap in interface MessageCommandContext
      Returns:
      The raw arguments received. The returned map is unmodifiable.
    • getMessageArgument

      @SideEffectFree protected Mono<Message> getMessageArgument(String name)
      Retrieves the message argument associated with the parameter of the given name.
      Parameters:
      name - The parameter name.
      Returns:
      The associated message argument. May be empty if missing or fail with a InvalidArgumentException if the value received is invalid.
      Implementation Requirements:
      By default, parses a string argument of the same name.
    • getCommandId

      public String getCommandId()
      Description copied from interface: InstrumentedContext
      Retrieves the ID of the invoked command.
      Specified by:
      getCommandId in interface InstrumentedContext
      Returns:
      The command ID.
    • invocation

      public Invocation invocation()
      Description copied from interface: CommandContext
      Retrieves the invocation that triggered the command.

      Unlike CommandContext.commandInvocation(), the returned value may be different from the command's declared Command.invocation() if it was invoked using an alias (when supported).

      If the command has no aliases, or was invoked through a method that does not support aliases, the return of this method is the same as the return of CommandContext.commandInvocation().

      Specified by:
      invocation in interface CommandContext
      Returns:
      The trigger invocation.
    • commandInvocation

      public Invocation commandInvocation()
      Description copied from interface: CommandContext
      Retrieves the canonical invocation of the triggered command, that is, the value of Command.invocation(). This is equivalent to the triggering invocation after resolving any aliases.

      If the command has no aliases, or was invoked through a method that does not support aliases, the return of this method is the same as the return of CommandContext.invocation().

      Specified by:
      commandInvocation in interface CommandContext
      Returns:
      The normalized trigger invocation.
    • getArgument

      public <T extends @NonNull Object> @Nullable T getArgument(String name, Class<T> argumentType) throws IllegalArgumentException, ClassCastException
      Description copied from interface: CommandContext
      Retrieves one of the arguments to the command.
      Specified by:
      getArgument in interface CommandContext
      Type Parameters:
      T - The type of the argument.
      Parameters:
      name - The name of the corresponding parameter.
      argumentType - The type of the argument.
      Returns:
      The argument value, or null if the received argument is empty (omitted by the caller or an empty parsing result) and does not have a default value.
      Throws:
      IllegalArgumentException - if there is no parameter with the given name.
      ClassCastException - if the given argument type does not match the type of the argument with the given name.
      See Also:
    • getArgument

      public <T extends @NonNull Object> @Nullable T getArgument(@FindDistinct Parameter<? extends T> parameter) throws IllegalArgumentException
      Description copied from interface: CommandContext
      Retrieves one of the arguments to the command.
      Specified by:
      getArgument in interface CommandContext
      Type Parameters:
      T - The type of the argument.
      Parameters:
      parameter - The corresponding parameter.
      Returns:
      The argument value, or null if the received argument is empty (omitted by the caller or an empty parsing result) and does not have a default value.
      Throws:
      IllegalArgumentException - if the given parameter is not present in the invoked command.
    • setContext

      public boolean setContext(String key, @Nullable Object obj, boolean replace)
      Description copied from interface: CommandContext
      Places a context object for subsequent handlers, optionally replacing any existing values under the same key.
      Specified by:
      setContext in interface CommandContext
      Parameters:
      key - The object key.
      obj - The object to store.
      replace - If true, the object will be placed unconditionally, replacing any existing value in that key. Otherwise, it will only be placed if there are no values with the given key.
      Returns:
      true if the given object was placed in the context. If replace is false and there is already an object at the given key, returns false.
    • getContext

      public <T> @Nullable T getContext(String key, Class<? extends T> type) throws IllegalArgumentException, ClassCastException
      Description copied from interface: CommandContext
      Retrieves a context object set by CommandContext.setContext(String, Object, boolean).
      Specified by:
      getContext in interface CommandContext
      Type Parameters:
      T - The type of the object.
      Parameters:
      key - The object key.
      type - The object class.
      Returns:
      The context object.
      Throws:
      IllegalArgumentException - if there is no context object with the given key.
      ClassCastException - if the context object with the given key is not compatible with the given type (not the same or a subtype).
    • initialize

      public Mono<Void> initialize(io.micrometer.observation.ObservationRegistry observations)
      Description copied from interface: LazyContext
      Partially initializes internal state, making the context minimally ready for handling to start.

      The only parts of the context API that are guaranteed to be ready to use before this method is called are the ones that retrieve pieces of the invocation context, that is EventContext.event(), CommandContext.caller(), CommandContext.channel(), etc, as well as this method itself. All other methods have undefined behavior.

      After this method is called and the returned mono completes successfully, the following pieces of the API also become ready to be used:

      Calling any part of the API other than those listed will continue to result in undefined behavior until LazyContext.load() is called and successfully completes.

      This method is idempotent; if it is called multiple times, the context will still be initialized only once, and all the returned Monos will only complete once it has finished loading.

      Specified by:
      initialize in interface LazyContext
      Parameters:
      observations - The registry to use for observations.
      Returns:
      A Mono that completes once the context is initialized.
    • doLoad

      public Mono<CommandResult> doLoad(io.micrometer.observation.ObservationRegistry observations)
      Performs the LazyContext.load() operation.
      Parameters:
      observations - The registry to use for observations.
      Returns:
      The result.
      See Also:
    • load

      public Mono<CommandResult> load()
      Description copied from interface: LazyContext
      Loads remaining internal state, making the context fully ready for use. It must only be called after LazyContext.initialize(ObservationRegistry) completes successfully, otherwise its behavior is undefined.

      Until this method is called and the returned mono completes successfully, all methods other than this one (and those specified in LazyContext.initialize(ObservationRegistry) have undefined behavior.

      This method is idempotent; if it is called multiple times, the context will still be loaded only once, and all the returned Monos will only complete once it has finished loading, with the same result.

      Specified by:
      load in interface LazyContext
      Returns:
      A Mono that completes empty once internal values are successfully loaded. If a situation where the invocation should be terminated is encountered, it emits the appropriate failure result.