Class CommandRegistrar

java.lang.Object
com.dianxin.tori.api.commands.CommandRegistrar

public class CommandRegistrar extends Object
The central registrar for all Discord interactions including Slash Commands, User Context Menus, and Message Context Menus.

This class handles loading the commands into the bot's memory, validating their annotations/metadata, committing them to the Discord API, and routing incoming interaction events to their respective execution classes.

  • Constructor Details

  • Method Details

    • setReplyConfig

      public CommandRegistrar setReplyConfig(CommandReplyConfig config)
      Sets the custom reply configuration for command interactions.

      This configuration allows developers to customize the messages sent by the registrar when command requirements—such as those defined by annotations like @OwnerOnly or @RequirePermissions—are not met.

      Parameters:
      config - The CommandReplyConfig instance containing the custom messages.
      Returns:
      This CommandRegistrar instance to allow for method chaining.
    • registerSlash

      public CommandRegistrar registerSlash(BaseCommand... cmdInstances)
      Registers one or more legacy slash commands into the system.
      Parameters:
      cmdInstances - An array of BaseCommand instances to register.
      Returns:
      This CommandRegistrar instance for method chaining.
      Throws:
      IllegalStateException - If commands have already been committed to Discord.
      IllegalArgumentException - If a command does not implement MaincommandRegistry.
    • registerSlash

      public CommandRegistrar registerSlash(ModernBaseCommand... cmds)
      Registers one or more modern slash commands into the system.
      Parameters:
      cmds - An array of ModernBaseCommand instances to register.
      Returns:
      This CommandRegistrar instance for method chaining.
      Throws:
      IllegalStateException - If commands have already been committed to Discord.
      IllegalArgumentException - If a command does not implement MaincommandRegistry.
    • registerUserContext

      public CommandRegistrar registerUserContext(BaseUserContextMenu... contextMenu)
      Registers one or more base user context menus.
      Parameters:
      contextMenu - An array of BaseUserContextMenu instances.
      Returns:
      This CommandRegistrar instance for method chaining.
      Throws:
      IllegalStateException - If commands have already been committed.
    • registerUserContext

      public CommandRegistrar registerUserContext(ModernBaseUserContextMenu... contextMenu)
      Registers one or more modern user context menus. Modern context menus rely on the ContextMenu annotation for metadata.
      Parameters:
      contextMenu - An array of ModernBaseUserContextMenu instances.
      Returns:
      This CommandRegistrar instance for method chaining.
      Throws:
      IllegalStateException - If commands have already been committed, or if the interaction name is empty.
      MissingAnnotationException - If the required ContextMenu annotation is absent.
    • registerMessageContext

      public CommandRegistrar registerMessageContext(BaseMessageContextMenu... contextMenus)
      Registers one or more base message context menus.
      Parameters:
      contextMenus - An array of BaseMessageContextMenu instances.
      Returns:
      This CommandRegistrar instance for method chaining.
      Throws:
      IllegalStateException - If commands have already been committed.
    • registerMessageContext

      public CommandRegistrar registerMessageContext(ModernBaseMessageContextMenu... contextMenus)
      Registers one or more modern message context menus. Modern context menus rely on the ContextMenu annotation for metadata.
      Parameters:
      contextMenus - An array of ModernBaseMessageContextMenu instances.
      Returns:
      This CommandRegistrar instance for method chaining.
      Throws:
      IllegalStateException - If commands have already been committed, or if the interaction name is empty.
      MissingAnnotationException - If the required ContextMenu annotation is absent.
    • commitAllCommands

      public void commitAllCommands(@Nullable @Nullable net.dv8tion.jda.api.entities.Guild guild)
      Commits all registered commands (Slash, User Context, Message Context) to the Discord API.

      If a guild is provided, the commands are registered instantly to that specific guild. If the guild is null, they are registered globally (which may take up to an hour to cache on Discord's end).

      Parameters:
      guild - The target Guild to register commands to, or null for global registration.
    • onSlashCommandEvent

      public void onSlashCommandEvent(net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent event)
      Routes an incoming slash command event to the appropriate registered command handler.
      Parameters:
      event - The SlashCommandInteractionEvent triggered by Discord.
    • onUserContextEvent

      public void onUserContextEvent(net.dv8tion.jda.api.events.interaction.command.UserContextInteractionEvent event)
      Routes an incoming user context menu event to the appropriate registered handler.
      Parameters:
      event - The UserContextInteractionEvent triggered by Discord.
    • onMessageContextEvent

      public void onMessageContextEvent(net.dv8tion.jda.api.events.interaction.command.MessageContextInteractionEvent event)
      Routes an incoming message context menu event to the appropriate registered handler.
      Parameters:
      event - The MessageContextInteractionEvent triggered by Discord.