Class MessageContextImpl
- All Implemented Interfaces:
dev.sympho.bot_utils.access.AccessContext,dev.sympho.bot_utils.access.AccessValidator,dev.sympho.bot_utils.access.ChannelAccessContext,dev.sympho.bot_utils.access.ChannelAccessValidator,dev.sympho.bot_utils.event.ChannelEventContext,dev.sympho.bot_utils.event.EventContext,dev.sympho.bot_utils.event.MessageBasedContext,dev.sympho.bot_utils.event.MessageCreateEventContext,dev.sympho.bot_utils.event.MessageEventContext,dev.sympho.bot_utils.event.MessageIncludedContext,dev.sympho.bot_utils.event.RepliableContext,CommandContext,MessageCommandContext,InstrumentedContext,LazyContext
- Since:
- 1.0
- Version:
- 1.0
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionMessageContextImpl(MessageCreateEvent event, Invocation invocation, Command<?> command, StringSplitter.Async.Iterator args, dev.sympho.bot_utils.access.AccessManager accessManager, dev.sympho.bot_utils.event.reply.ReplyManager replyManager) Initializes a new context. -
Method Summary
Modifier and TypeMethodDescriptionRetrieves the raw argument string as it was received, before being split.Retrieves the canonical invocation of the triggered command, that is, the value ofCommand.invocation().doLoad(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.getChannelArgument(String name, Class<C> type) Retrieves the channel argument associated with the parameter of the given name.Retrieves the ID of the invoked command.<T> @Nullable TgetContext(String key, Class<? extends T> type) Retrieves a context object set byCommandContext.setContext(String, Object, boolean).getFloatArgument(String name) Retrieves the float argument associated with the parameter of the given name.getIntegerArgument(String name) Retrieves the integer argument associated with the parameter of the given name.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.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.Retrieves the invocation that triggered the command.load()Loads remaining internal state, making the context fully ready for use.Retrieves the raw arguments received, keyed by the corresponding parameter name.rawArgs()Retrieves the raw arguments received before parsing, in the order that they were received.booleansetContext(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.Methods inherited from class dev.sympho.bot_utils.event.AbstractRepliableContext
repliesMethods inherited from class dev.sympho.bot_utils.event.AbstractChannelEventContext
event, validatorMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface dev.sympho.bot_utils.access.AccessContext
asGuild, isPrivateMethods inherited from interface dev.sympho.bot_utils.access.ChannelAccessContext
asUserMethods inherited from interface dev.sympho.bot_utils.event.ChannelEventContext
belongs, belongs, belongs, belongs, hasAccess, hasAccess, validate, validatorMethods inherited from interface dev.sympho.modular_commands.api.command.context.CommandContext
caller, callerMember, commandInvocation, getArgument, getArgument, getArgument, getContext, invocation, member, member, requireArgument, requireArgument, requireArgument, requireContext, setContext, setContextMethods inherited from interface dev.sympho.bot_utils.event.EventContext
client, validateMethods inherited from interface dev.sympho.modular_commands.execute.InstrumentedContext
addTagsMethods inherited from interface dev.sympho.modular_commands.api.command.context.MessageCommandContext
callerMember, channelMethods inherited from interface dev.sympho.bot_utils.event.MessageCreateEventContext
channelId, event, fetchMessage, guild, guildId, message, messageId, userMethods inherited from interface dev.sympho.bot_utils.event.RepliableContext
replies, reply, reply, reply, reply, reply, reply, reply
-
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, dev.sympho.bot_utils.access.AccessManager accessManager, dev.sympho.bot_utils.event.reply.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
Performs any required initialization of received arguments before parsing starts.- Returns:
- A mono that completes when initialization is done.
-
tagType
Description copied from interface:InstrumentedContextDetermines the value for thetype tag.- Specified by:
tagTypein interfaceInstrumentedContext- Returns:
- The tag.
-
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
InvalidArgumentExceptionif 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
InvalidArgumentExceptionif 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
InvalidArgumentExceptionif 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
InvalidArgumentExceptionif 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
InvalidArgumentExceptionif 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
InvalidArgumentExceptionif 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
InvalidArgumentExceptionif 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.
-
argString
Description copied from interface:MessageCommandContextRetrieves 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 forMessageCommandContext.rawArgs().- Specified by:
argStringin interfaceMessageCommandContext- Returns:
- The received arguments before splitting.
-
rawArgs
Description copied from interface:MessageCommandContextRetrieves the raw arguments received before parsing, in the order that they were received.- Specified by:
rawArgsin interfaceMessageCommandContext- Returns:
- The raw arguments received. The returned list is unmodifiable.
-
rawArgMap
Description copied from interface:MessageCommandContextRetrieves the raw arguments received, keyed by the corresponding parameter name.- Specified by:
rawArgMapin interfaceMessageCommandContext- Returns:
- The raw arguments received. The returned map is unmodifiable.
-
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
InvalidArgumentExceptionif the value received is invalid. - Implementation Requirements:
- By default, parses a string argument of the same name.
-
getCommandId
Description copied from interface:InstrumentedContextRetrieves the ID of the invoked command.- Specified by:
getCommandIdin interfaceInstrumentedContext- Returns:
- The command ID.
-
invocation
Description copied from interface:CommandContextRetrieves the invocation that triggered the command.Unlike
CommandContext.commandInvocation(), 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.commandInvocation().- Specified by:
invocationin interfaceCommandContext- Returns:
- The trigger invocation.
-
commandInvocation
Description copied from interface:CommandContextRetrieves the canonical invocation of the triggered command, that is, the value ofCommand.invocation(). This is equivalent to thetriggering invocationafter 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:
commandInvocationin 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:CommandContextRetrieves one of the arguments to the command.- Specified by:
getArgumentin 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
nullif 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:CommandContextRetrieves one of the arguments to the command.- Specified by:
getArgumentin interfaceCommandContext- Type Parameters:
T- The type of the argument.- Parameters:
parameter- The corresponding parameter.- Returns:
- The argument value, or
nullif 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:CommandContextPlaces a context object for subsequent handlers, optionally replacing any existing values under the same key.- Specified by:
setContextin 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:
trueif the given object was placed in the context. Ifreplaceisfalseand 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:CommandContextRetrieves a context object set byCommandContext.setContext(String, Object, boolean).- Specified by:
getContextin 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).
-
initialize
Description copied from interface:LazyContextPartially 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:
- 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:
initializein 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:LazyContextLoads 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:
loadin 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.
-