Class MessageContextImpl
- All Implemented Interfaces:
CommandContext
,MessageCommandContext
,AccessValidator
,InstrumentedContext
,LazyContext
- Since:
- 1.0
- Version:
- 1.0
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionMessageContextImpl
(MessageCreateEvent event, Invocation invocation, Command<?> command, StringSplitter.Async.Iterator args, AccessValidator access) Initializes a new context. -
Method Summary
Modifier and TypeMethodDescriptiondoLoad
(io.micrometer.observation.ObservationRegistry observations) Performs theLazyContext.load()
operation.getArgument
(Parameter<? extends T> parameter) Retrieves one of the arguments to the command.getArgument
(String name, Class<T> argumentType) Retrieves one of the arguments to the command.protected Mono<Attachment>
getAttachmentArgument
(String name) Retrieves the attachment argument associated with the parameter of the given name.getBooleanArgument
(String name) Retrieves the boolean argument associated with the parameter of the given name.Retrieves the user that called the command.Retrieves the user that called the command as a guild member as provided by the triggering event, if present.Retrieves the channel that the command was invoked in.getChannelArgument
(String name, Class<C> type) Retrieves the channel argument associated with the parameter of the given name.Retrieves the ID of the channel that the command was invoked in.Retrieves the ID of the invoked command.Retrieves the canonical invocation of the triggered command, that is, the value ofCommand.invocation()
.<T> @Nullable T
getContext
(String key, Class<? extends T> type) Retrieves a context object set byCommandContext.setContext(String, Object, boolean)
.getEvent()
Retrieves the event that triggered the command.getFloatArgument
(String name) Retrieves the float argument associated with the parameter of the given name.getGuild()
Retrieves the guild that the command was invoked in, if there is one.Retrieves the ID of the guild that the command was invoked in, if there is one.getIntegerArgument
(String name) Retrieves the integer argument associated with the parameter of the given name.Retrieves the invocation that triggered the command.getMessageArgument
(String name) Retrieves the message argument associated with the parameter of the given name.getRoleArgument
(String name) Retrieves the role argument associated with the parameter of the given name.getSnowflakeArgument
(String name, SnowflakeParser.Type type) Retrieves the snowflake argument associated with the parameter of the given name.getStringArgument
(String name) Retrieves the string argument associated with the parameter of the given name.getUserArgument
(String name) Retrieves the user argument associated with the parameter of the given name.Determines whether the invoking user in the current execution context (guild and channel) has access equivalent to the given group.initArgs()
Performs any required initialization of received arguments before parsing starts.initialize
(io.micrometer.observation.ObservationRegistry observations) Partially initializes internal state, making the context minimally ready for handling to start.load()
Loads remaining internal state, making the context fully ready for use.protected ReplyManager
Creates the initial reply manager.replies()
Retrieves the reply manager for this instance.boolean
setContext
(String key, @Nullable Object obj, boolean replace) Places a context object for subsequent handlers, optionally replacing any existing values under the same key.tagType()
Determines the value for thetype tag
.final Mono<CommandResult>
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.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface dev.sympho.modular_commands.api.permission.AccessValidator
hasAccess, validate
Methods inherited from interface dev.sympho.modular_commands.api.command.context.CommandContext
belongs, belongs, getArgument, getArgument, getArgument, getClient, getCommandInvocation, getContext, getInvocation, isPrivate, replies, reply, reply, reply, reply, reply, reply, requireArgument, requireArgument, requireArgument, requireContext, setContext, setContext
Methods inherited from interface dev.sympho.modular_commands.execute.InstrumentedContext
addTags
Methods inherited from interface dev.sympho.modular_commands.api.command.context.MessageCommandContext
getMessage, getMessageId
-
Field Details
-
METRIC_NAME_PREFIX
The prefix for metrics in this class.- See Also:
-
METRIC_NAME_PREFIX_ARGUMENT
The prefix for argument parsing metrics.- See Also:
-
command
The invoked command.
-
-
Constructor Details
-
MessageContextImpl
public MessageContextImpl(MessageCreateEvent event, Invocation invocation, Command<?> command, StringSplitter.Async.Iterator args, AccessValidator access) 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.access
- The validator to use for access checks.- Throws:
ResultException
- if there is a mismatch between parameters and arguments.
-
-
Method Details
-
initArgs
Performs any required initialization of received arguments before parsing starts.- Returns:
- A mono that completes when initialization is done.
-
tagType
Description copied from interface:InstrumentedContext
Determines the value for thetype tag
.- Specified by:
tagType
in interfaceInstrumentedContext
- Returns:
- The tag.
-
makeReplyManager
Creates the initial reply manager.- Returns:
- The initial reply manager.
-
getStringArgument
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
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
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
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
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
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
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
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
Description copied from interface:CommandContext
Retrieves the user that called the command.- Specified by:
getCaller
in interfaceCommandContext
- Returns:
- The calling user.
-
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 interfaceCommandContext
- Returns:
- The calling user as a guild member, or
null
if the command was invoked in a private channel.
-
getChannel
Description copied from interface:CommandContext
Retrieves the channel that the command was invoked in.- Specified by:
getChannel
in interfaceCommandContext
- Returns:
- The invoking channel.
-
getChannelId
Description copied from interface:CommandContext
Retrieves the ID of the channel that the command was invoked in.- Specified by:
getChannelId
in interfaceCommandContext
- Returns:
- The invoking channel's ID.
-
getGuild
Description copied from interface:CommandContext
Retrieves the guild that the command was invoked in, if there is one.- Specified by:
getGuild
in interfaceCommandContext
- Returns:
- The invoking guild.
-
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 interfaceCommandContext
- Returns:
- The invoking guild's ID, or
null
if the command was invoked in a private channel.
-
getEvent
Description copied from interface:CommandContext
Retrieves the event that triggered the command.- Specified by:
getEvent
in interfaceCommandContext
- Specified by:
getEvent
in interfaceMessageCommandContext
- Returns:
- The trigger event.
-
getMessageArgument
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
Description copied from interface:InstrumentedContext
Retrieves the ID of the invoked command.- Specified by:
getCommandId
in interfaceInstrumentedContext
- Returns:
- The command ID.
-
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 declaredCommand.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 interfaceCommandContext
- Returns:
- The trigger invocation.
-
getCommandInvocation
Description copied from interface:CommandContext
Retrieves the canonical invocation of the triggered command, that is, the value ofCommand.invocation()
. This is equivalent to thetriggering 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 interfaceCommandContext
- 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 interfaceCommandContext
- 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 interfaceCommandContext
- 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
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 interfaceCommandContext
- Parameters:
key
- The object key.obj
- The object to store.replace
- Iftrue
, 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. Ifreplace
isfalse
and there is already an object at the given key, returnsfalse
.
-
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 byCommandContext.setContext(String, Object, boolean)
.- Specified by:
getContext
in interfaceCommandContext
- 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
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 interfaceCommandContext
- Returns:
- The reply manager.
- Throws:
IllegalStateException
- if the context was not loaded yet.
-
hasAccess
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 interfaceAccessValidator
- Parameters:
group
- The group required for access.- Returns:
- Whether the caller has access equivalent to the given group under the current execution context.
-
validate
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 interfaceAccessValidator
- 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
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:
- Reply manager
- Access and group checking
LazyContext.load()
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 interfaceLazyContext
- Parameters:
observations
- The registry to use for observations.- Returns:
- A Mono that completes once the context is initialized.
-
doLoad
Performs theLazyContext.load()
operation.- Parameters:
observations
- The registry to use for observations.- Returns:
- The result.
- See Also:
-
load
Description copied from interface:LazyContext
Loads remaining internal state, making the context fully ready for use. It must only be called afterLazyContext.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 interfaceLazyContext
- 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.
-