Class BaseCommandExecutor<E extends Event,CTX extends InstrumentedContext & LazyContext,H extends Handlers,I extends SmartIterator<String>>

java.lang.Object
dev.sympho.modular_commands.execute.CommandExecutor
dev.sympho.modular_commands.execute.BaseCommandExecutor<E,CTX,H,I>
Type Parameters:
E - The type of event that triggers commands.
CTX - The type of command context.
H - The type of handlers.
I - The iterator type used to traverse the received arguments.
Direct Known Subclasses:
MessageCommandExecutor

public abstract class BaseCommandExecutor<E extends Event,CTX extends InstrumentedContext & LazyContext,H extends Handlers,I extends SmartIterator<String>> extends CommandExecutor
Base implementation for a command executor.
Since:
1.0
Version:
1.0
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected final dev.sympho.bot_utils.access.AccessManager
    The access manager to use for access checks.
    protected final GatewayDiscordClient
    The client to receive events from.
    protected final io.micrometer.core.instrument.MeterRegistry
    The meter registry to use.
    protected final io.micrometer.observation.ObservationRegistry
    The observation registry to use.
    protected final Registry
    The registry to use to look up commands.

    Fields inherited from class dev.sympho.modular_commands.execute.CommandExecutor

    logger
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    BaseCommandExecutor(GatewayDiscordClient client, Registry registry, Optional<dev.sympho.bot_utils.access.AccessManager> accessManager, Optional<io.micrometer.core.instrument.MeterRegistry> meters, Optional<io.micrometer.observation.ObservationRegistry> observations)
    Creates a new instance.
  • Method Summary

    Modifier and Type
    Method
    Description
    protected final <T> Function<Mono<T>,Mono<T>>
    addTags(E event)
    Creates a function that adds the common instrumentation tags for the given event to Monos.
    protected Flux<?>
    Constructs the processing pipeline that processes command events.
    protected abstract Class<H>
    Retrives the command type to use.
    protected boolean
    eventFilter(E event)
    Determines if an event should be processed for commands.
    protected abstract Class<E>
    Retrieves the event type to listen for.
    protected abstract boolean
    Determines whether a parsed arg list (as given by parse(Event)) must always be fully matched to a command.
    protected abstract User
    getCaller(E event)
    Retrieves the user that invoked the command from the triggering event.
    protected abstract Mono<MessageChannel>
    getChannel(E event)
    Retrieves the channel where the command was invoked from the triggering event.
    protected abstract Snowflake
    getChannelId(E event)
    Retrieves the ID of the channel where the command was invoked from the triggering event.
    protected abstract Mono<Guild>
    getGuild(E event)
    Retrieves the guild where the command was invoked, if any, from the triggering event.
    protected abstract Optional<Snowflake>
    getGuildId(E event)
    Retrieves the ID of the guild where the command was invoked, if any, from the triggering event.
    protected abstract InvocationHandler<? super CTX>
    Retrieves the invocation handler specified by the given hander set.
    protected abstract List<? extends ResultHandler<? super CTX>>
    getResultHandlers(H handlers)
    Retrieves the result handlers specified by the given hander set.
    protected abstract CTX
    makeContext(E event, Command<? extends H> command, Invocation invocation, I args)
    Creates a command context from a parsed invocation.
    protected abstract I
    parse(E event)
    Parses the raw args from the event.
    protected abstract Metrics.Tag.Type
    Determines the type tag for this pipeline.

    Methods inherited from class dev.sympho.modular_commands.execute.CommandExecutor

    start, stop

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • client

      protected final GatewayDiscordClient client
      The client to receive events from.
    • registry

      protected final Registry registry
      The registry to use to look up commands.
    • accessManager

      protected final dev.sympho.bot_utils.access.AccessManager accessManager
      The access manager to use for access checks.
    • meters

      protected final io.micrometer.core.instrument.MeterRegistry meters
      The meter registry to use.
    • observations

      protected final io.micrometer.observation.ObservationRegistry observations
      The observation registry to use.
  • Constructor Details

    • BaseCommandExecutor

      protected BaseCommandExecutor(GatewayDiscordClient client, Registry registry, Optional<dev.sympho.bot_utils.access.AccessManager> accessManager, Optional<io.micrometer.core.instrument.MeterRegistry> meters, Optional<io.micrometer.observation.ObservationRegistry> observations)
      Creates a new instance.
      Parameters:
      client - The client to receive events from.
      registry - The registry to use to look up commands.
      accessManager - The access manager to use for access checks. Defaults to AccessManager.basic().
      meters - The meter registry to use. Defaults to a no-op registry.
      observations - The observation registry to use. Defaults to a no-op registry.
  • Method Details

    • buildPipeline

      protected Flux<?> buildPipeline()
      Description copied from class: CommandExecutor
      Constructs the processing pipeline that processes command events.
      Specified by:
      buildPipeline in class CommandExecutor
      Returns:
      The constructed pipeline.
    • tagType

      @Pure protected abstract Metrics.Tag.Type tagType()
      Determines the type tag for this pipeline.
      Returns:
      The tag.
    • addTags

      @SideEffectFree protected final <T> Function<Mono<T>,Mono<T>> addTags(E event)
      Creates a function that adds the common instrumentation tags for the given event to Monos.
      Type Parameters:
      T - The mono value type.
      Parameters:
      event - The event.
      Returns:
      The function.
    • eventType

      @Pure protected abstract Class<E> eventType()
      Retrieves the event type to listen for.
      Returns:
      The event type.
    • commandType

      @Pure protected abstract Class<H> commandType()
      Retrives the command type to use.
      Returns:
      The command type.
    • fullMatch

      @Pure protected abstract boolean fullMatch()
      Determines whether a parsed arg list (as given by parse(Event)) must always be fully matched to a command. If true, the presence of leftover args (as would be received by makeContext(Event, Command, Invocation, SmartIterator)) triggers an error for that event (stopping the processing of that particular event, but without stopping the pipeline).
      Returns:
      true if the parsed args must be fully matched to a command. false if additional args are allowed.
      API Note:
      This check is not related to any user-facing functionality, but purely as an internal sanity check. If an event provides arguments in a way other than the parsed args, with said args being used only to identify the command to execute, it would not make sense for extra args to be received.

      For example, in a slash command, the comand name is provided separately from the arguments, and any subcommands are already labeled as such, so there is no need to evaluate any further args when looking up the command. At the same time, since slash commands (including subcommands) have to be registered with Discord ahead of time to be callable by users, the only way that a slash command event would provide a subcommand that is not found is if the command was registered to Discord without having a corresponding internal handler, without would indicate an inconsistency in the command system (the declared commands do not match the implemented commands), which should be reported to the administrator as a (non-fatal) pipeline error.

    • eventFilter

      @Pure protected boolean eventFilter(E event)
      Determines if an event should be processed for commands. This filter is applied at the start of handling, before any parsing is performed.
      Parameters:
      event - The event to check.
      Returns:
      true if the event should be processed. false if the event should be discarded.
      Implementation Requirements:
      The default is to process all received events.
    • parse

      @SideEffectFree protected abstract I parse(E event)
      Parses the raw args from the event. This includes the names that identify the command and subcommands, as well as any additional args, if allowed.
      Parameters:
      event - The event to parse args from.
      Returns:
      The parsed args. May have no elements if the event should not be handled as a command.
    • makeContext

      @SideEffectFree protected abstract CTX makeContext(E event, Command<? extends H> command, Invocation invocation, I args)
      Creates a command context from a parsed invocation.
      Parameters:
      event - The event being processed.
      command - The identified command.
      invocation - The invocation that mapped to the command. This may be different from the command's declared Command.invocation() if it was invoked using an alias (when supported).
      args - The args that remained after removing the command identifiers. If fullMatch() is true, this will always be empty.
      Returns:
      The context that represents the given invocation.
    • getGuildId

      @SideEffectFree protected abstract Optional<Snowflake> getGuildId(E event)
      Retrieves the ID of the guild where the command was invoked, if any, from the triggering event.
      Parameters:
      event - The triggering event.
      Returns:
      The ID of the guild where the event was invoked.
    • getGuild

      @SideEffectFree protected abstract Mono<Guild> getGuild(E event)
      Retrieves the guild where the command was invoked, if any, from the triggering event.
      Parameters:
      event - The triggering event.
      Returns:
      The guild where the event was invoked.
    • getChannelId

      @SideEffectFree protected abstract Snowflake getChannelId(E event)
      Retrieves the ID of the channel where the command was invoked from the triggering event.
      Parameters:
      event - The triggering event.
      Returns:
      The ID of the channel where the event was invoked.
    • getChannel

      @SideEffectFree protected abstract Mono<MessageChannel> getChannel(E event)
      Retrieves the channel where the command was invoked from the triggering event.
      Parameters:
      event - The triggering event.
      Returns:
      The channel where the event was invoked.
    • getCaller

      @SideEffectFree protected abstract User getCaller(E event)
      Retrieves the user that invoked the command from the triggering event.
      Parameters:
      event - The triggering event.
      Returns:
      The user that invoked the command.
    • getInvocationHandler

      @Pure protected abstract InvocationHandler<? super CTX> getInvocationHandler(H handlers)
      Retrieves the invocation handler specified by the given hander set.
      Parameters:
      handlers - The handler set.
      Returns:
      The invocation handler.
    • getResultHandlers

      @Pure protected abstract List<? extends ResultHandler<? super CTX>> getResultHandlers(H handlers)
      Retrieves the result handlers specified by the given hander set.
      Parameters:
      handlers - The handler set.
      Returns:
      The result handlers.