Class MessageContextImpl

java.lang.Object
dev.sympho.modular_commands.impl.context.MessageContextImpl
All Implemented Interfaces:
CommandContext, MessageCommandContext, AccessValidator, InstrumentedContext, LazyContext

public final class MessageContextImpl extends Object 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

  • 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.
    • makeReplyManager

      protected ReplyManager makeReplyManager()
      Creates the initial reply manager.
      Returns:
      The initial reply manager.
    • 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.
    • getCaller

      public User getCaller()
      Description copied from interface: CommandContext
      Retrieves the user that called the command.
      Specified by:
      getCaller in interface CommandContext
      Returns:
      The calling user.
    • getCallerMember

      public @Nullable Member getCallerMember()
      Description copied from interface: CommandContext
      Retrieves the user that called the command as a guild member as provided by the triggering event, if present.
      Specified by:
      getCallerMember in interface CommandContext
      Returns:
      The calling user as a guild member, or null if the command was invoked in a private channel.
    • getChannel

      public Mono<MessageChannel> getChannel()
      Description copied from interface: CommandContext
      Retrieves the channel that the command was invoked in.
      Specified by:
      getChannel in interface CommandContext
      Returns:
      The invoking channel.
    • getChannelId

      public Snowflake getChannelId()
      Description copied from interface: CommandContext
      Retrieves the ID of the channel that the command was invoked in.
      Specified by:
      getChannelId in interface CommandContext
      Returns:
      The invoking channel's ID.
    • getGuild

      public Mono<Guild> getGuild()
      Description copied from interface: CommandContext
      Retrieves the guild that the command was invoked in, if there is one.
      Specified by:
      getGuild in interface CommandContext
      Returns:
      The invoking guild.
    • getGuildId

      public @Nullable Snowflake getGuildId()
      Description copied from interface: CommandContext
      Retrieves the ID of the guild that the command was invoked in, if there is one.
      Specified by:
      getGuildId in interface CommandContext
      Returns:
      The invoking guild's ID, or null if the command was invoked in a private channel.
    • getEvent

      public MessageCreateEvent getEvent()
      Description copied from interface: CommandContext
      Retrieves the event that triggered the command.
      Specified by:
      getEvent in interface CommandContext
      Specified by:
      getEvent in interface MessageCommandContext
      Returns:
      The trigger event.
    • 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.
    • getInvocation

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

      Unlike CommandContext.getCommandInvocation(), 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.getCommandInvocation().

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

      public Invocation getCommandInvocation()
      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.getInvocation().

      Specified by:
      getCommandInvocation 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).
    • replies

      @Pure public ReplyManager replies() throws IllegalStateException
      Description copied from interface: CommandContext
      Retrieves the reply manager for this instance.

      Note that calling ReplyManager.longTerm() on the returned manager will cause this method to also return the long-term manager from that point on.

      Specified by:
      replies in interface CommandContext
      Returns:
      The reply manager.
      Throws:
      IllegalStateException - if the context was not loaded yet.
    • hasAccess

      public final Mono<Boolean> hasAccess(Group group)
      Description copied from interface: AccessValidator
      Determines whether the invoking user in the current execution context (guild and channel) has access equivalent to the given group.

      Note that while the most straightforward implementation of this interface is to simply check if the caller belongs to the given group, implementations are allowed to add other conditions under which a user has equivalent permissions despite not belonging to the group (or conversely does not have permissions despite belonging to the group).

      Specified by:
      hasAccess in interface AccessValidator
      Parameters:
      group - The group required for access.
      Returns:
      Whether the caller has access equivalent to the given group under the current execution context.
    • validate

      public final Mono<CommandResult> validate(Group group)
      Description copied from interface: AccessValidator
      Determines whether the invoking user in the current execution context (guild and channel) has access equivalent to the given group, otherwise generating an appropriate result.
      Specified by:
      validate in interface AccessValidator
      Parameters:
      group - The group required for access.
      Returns:
      A Mono that is empty if the caller has access equivalent to the given group under the current execution context, or otherwise issues a failure result.
    • 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 CommandContext.getEvent(), CommandContext.getCaller(), CommandContext.getChannel(), 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.