Class CommandClientImpl

java.lang.Object
com.jagrosh.jdautilities.command.impl.CommandClientImpl
All Implemented Interfaces:
CommandClient, net.dv8tion.jda.api.hooks.EventListener

public class CommandClientImpl extends Object implements CommandClient, net.dv8tion.jda.api.hooks.EventListener
An implementation of CommandClient to be used by a bot.

This is a listener usable with JDA, as it implements EventListener in order to catch and use different kinds of Events. The primary usage of this is where the CommandClient implementation takes MessageReceivedEvents, and automatically processes arguments, and provide them to a Command for running and execution.

Author:
John Grosh (jagrosh)
  • Constructor Details

  • Method Details

    • setListener

      public void setListener(CommandListener listener)
      Description copied from interface: CommandClient
      Sets the CommandListener to catch command-related events thrown by this CommandClient.
      Specified by:
      setListener in interface CommandClient
      Parameters:
      listener - The CommandListener
    • getListener

      public CommandListener getListener()
      Description copied from interface: CommandClient
      Returns the current CommandListener.
      Specified by:
      getListener in interface CommandClient
      Returns:
      A possibly-null CommandListener
    • getCommands

      public List<Command> getCommands()
      Description copied from interface: CommandClient
      Returns the list of registered Commands during this session.
      Specified by:
      getCommands in interface CommandClient
      Returns:
      A never-null List of Commands registered during this session
    • getSlashCommands

      public List<SlashCommand> getSlashCommands()
      Description copied from interface: CommandClient
      Returns the list of registered SlashCommands during this session.
      Specified by:
      getSlashCommands in interface CommandClient
      Returns:
      A never-null List of Slash Commands registered during this session
    • getContextMenus

      public List<ContextMenu> getContextMenus()
      Description copied from interface: CommandClient
      Returns the list of registered ContextMenus during this session.
      Specified by:
      getContextMenus in interface CommandClient
      Returns:
      A never-null List of Context Menus registered during this session
    • isManualUpsert

      public boolean isManualUpsert()
      Description copied from interface: CommandClient
      Returns whether manual upsertion is enabled
      Specified by:
      isManualUpsert in interface CommandClient
      Returns:
      The manual upsertion status
    • forcedGuildId

      public String forcedGuildId()
      Description copied from interface: CommandClient
      Returns the forced Guild ID for automatic slash command upserts
      Specified by:
      forcedGuildId in interface CommandClient
      Returns:
      A possibly-null forcedGuildId set in the builder
    • getStartTime

      public OffsetDateTime getStartTime()
      Description copied from interface: CommandClient
      Gets the time this CommandClient implementation was created.
      Specified by:
      getStartTime in interface CommandClient
      Returns:
      The start time of this CommandClient implementation
    • getCooldown

      public OffsetDateTime getCooldown(String name)
      Description copied from interface: CommandClient
      Gets the OffsetDateTime that the specified cooldown expires.
      Specified by:
      getCooldown in interface CommandClient
      Parameters:
      name - The cooldown name
      Returns:
      The expiration time, or null if the cooldown does not exist
    • getRemainingCooldown

      public int getRemainingCooldown(String name)
      Description copied from interface: CommandClient
      Gets the remaining number of seconds on the specified cooldown.
      Specified by:
      getRemainingCooldown in interface CommandClient
      Parameters:
      name - The cooldown name
      Returns:
      The number of seconds remaining
    • applyCooldown

      public void applyCooldown(String name, int seconds)
      Description copied from interface: CommandClient
      Applies the specified cooldown with the provided name.
      Specified by:
      applyCooldown in interface CommandClient
      Parameters:
      name - The cooldown name
      seconds - The time to make the cooldown last
    • cleanCooldowns

      public void cleanCooldowns()
      Description copied from interface: CommandClient
      Cleans up expired cooldowns to reduce memory.
      Specified by:
      cleanCooldowns in interface CommandClient
    • getCommandUses

      public int getCommandUses(Command command)
      Description copied from interface: CommandClient
      Gets the number of uses for the provide Command during this session, or 0 if the command is not registered to this CommandClient.
      Specified by:
      getCommandUses in interface CommandClient
      Parameters:
      command - The Command
      Returns:
      The number of uses for the Command
    • getCommandUses

      public int getCommandUses(String name)
      Description copied from interface: CommandClient
      Gets the number of uses for a Command during this session matching the provided String name, or 0 if there is no Command with the name.

      NOTE: this method WILL NOT get uses for a command if an alias is provided! Also note that child commands ARE NOT tracked and providing names or effective names of child commands will return 0.

      Specified by:
      getCommandUses in interface CommandClient
      Parameters:
      name - The name of the Command
      Returns:
      The number of uses for the Command, or 0 if the name does not match with a Command
    • addCommand

      public void addCommand(Command command)
      Description copied from interface: CommandClient
      Adds a single Command to this CommandClient's registered Commands.

      For CommandClient's containing 20 commands or less, command calls by users will have the bot iterate through the entire ArrayList to find the command called. As expected, this can get fairly hefty if a bot has a lot of Commands registered to it.

      To prevent delay a CommandClient that has more that 20 Commands registered to it will begin to use indexed calls.
      Indexed calls use a HashMap which links their name and their aliases to the index that which they are located at in the ArrayList they are stored.

      This means that all insertion and removal of Commands must reorganize the index maintained by the HashMap.
      For this particular insertion, the Command provided is inserted at the end of the index, meaning it will become the "rightmost" Command in the ArrayList.

      Specified by:
      addCommand in interface CommandClient
      Parameters:
      command - The Command to add
    • addCommand

      public void addCommand(Command command, int index)
      Description copied from interface: CommandClient
      Adds a single Command to this CommandClient's registered Commands at the specified index.

      For CommandClient's containing 20 commands or less, command calls by users will have the bot iterate through the entire ArrayList to find the command called. As expected, this can get fairly hefty if a bot has a lot of Commands registered to it.

      To prevent delay a CommandClient that has more that 20 Commands registered to it will begin to use indexed calls.
      Indexed calls use a HashMap which links their name and their aliases to the index that which they are located at in the ArrayList they are stored.

      This means that all insertion and removal of Commands must reorganize the index maintained by the HashMap.
      For this particular insertion, the Command provided is inserted at the index specified, meaning it will become the Command located at that index in the ArrayList. This will shift the Command previously located at that index as well as any located at greater indices, right one index (size()+1).

      Specified by:
      addCommand in interface CommandClient
      Parameters:
      command - The Command to add
      index - The index to add the Command at (must follow the specifications 0<=index<=size())
    • addSlashCommand

      public void addSlashCommand(SlashCommand command)
      Description copied from interface: CommandClient
      Adds a single SlashCommand to this CommandClient's registered SlashCommand.

      For CommandClient's containing 20 commands or less, command calls by users will have the bot iterate through the entire ArrayList to find the command called. As expected, this can get fairly hefty if a bot has a lot of Commands registered to it.

      To prevent delay a CommandClient that has more that 20 Commands registered to it will begin to use indexed calls.
      Indexed calls use a HashMap which links their name to the index that which they are located at in the ArrayList they are stored.

      This means that all insertion and removal of SlashCommands must reorganize the index maintained by the HashMap.
      For this particular insertion, the SlashCommand provided is inserted at the end of the index, meaning it will become the "rightmost" Command in the ArrayList.

      Specified by:
      addSlashCommand in interface CommandClient
      Parameters:
      command - The Command to add
    • addSlashCommand

      public void addSlashCommand(SlashCommand command, int index)
      Description copied from interface: CommandClient
      Adds a single SlashCommand to this CommandClient's registered Commands at the specified index.

      For CommandClient's containing 20 commands or less, command calls by users will have the bot iterate through the entire ArrayList to find the command called. As expected, this can get fairly hefty if a bot has a lot of Commands registered to it.

      To prevent delay a CommandClient that has more that 20 Commands registered to it will begin to use indexed calls.
      Indexed calls use a HashMap which links their name to the index that which they are located at in the ArrayList they are stored.

      This means that all insertion and removal of Commands must reorganize the index maintained by the HashMap.
      For this particular insertion, the Command provided is inserted at the index specified, meaning it will become the Command located at that index in the ArrayList. This will shift the Command previously located at that index as well as any located at greater indices, right one index (size()+1).

      Specified by:
      addSlashCommand in interface CommandClient
      Parameters:
      command - The Command to add
      index - The index to add the Command at (must follow the specifications 0<=index<=size())
    • addContextMenu

      public void addContextMenu(ContextMenu menu)
      Description copied from interface: CommandClient
      Adds a single ContextMenu to this CommandClient's registered Context Menus.
      Specified by:
      addContextMenu in interface CommandClient
      Parameters:
      menu - The menu to add
    • addContextMenu

      public void addContextMenu(ContextMenu menu, int index)
      Description copied from interface: CommandClient
      Adds a single ContextMenu to this CommandClient's registered Context Menus.
      Specified by:
      addContextMenu in interface CommandClient
      Parameters:
      menu - The menu to add
      index - The index to add the Context Menu at (must follow the specifications 0<=index<=size())
    • removeCommand

      public void removeCommand(String name)
      Description copied from interface: CommandClient
      Removes a single Command from this CommandClient's registered Commands at the index linked to the provided name/alias.

      For CommandClient's containing 20 commands or less, command calls by users will have the bot iterate through the entire ArrayList to find the command called. As expected, this can get fairly hefty if a bot has a lot of Commands registered to it.

      To prevent delay a CommandClient that has more that 20 Commands registered to it will begin to use indexed calls.
      Indexed calls use a HashMap which links their name and their aliases to the index that which they are located at in the ArrayList they are stored.

      This means that all insertion and removal of Commands must reorganize the index maintained by the HashMap.
      For this particular removal, the Command removed is that of the corresponding index retrieved by the name provided. This will shift any Commands located at greater indices, left one index (size()-1).

      Specified by:
      removeCommand in interface CommandClient
      Parameters:
      name - The name or an alias of the Command to remove
    • addAnnotatedModule

      public void addAnnotatedModule(Object module)
      Description copied from interface: CommandClient
      Compiles the provided Object annotated with JDACommand.Module into a List of Commands and adds them to this CommandClient in the order they are listed.

      This is done through the AnnotatedModuleCompiler provided when building this CommandClient.

      Specified by:
      addAnnotatedModule in interface CommandClient
      Parameters:
      module - An object annotated with JDACommand.Module to compile into commands to be added.
    • addAnnotatedModule

      public void addAnnotatedModule(Object module, Function<Command,Integer> mapFunction)
      Description copied from interface: CommandClient
      Compiles the provided Object annotated with JDACommand.Module into a List of Commands and adds them to this CommandClient via the Function provided.

      This is done through the AnnotatedModuleCompiler provided when building this CommandClient.

      The Function will apply each Command in the compiled list and request an int in return.
      Using this int, the command provided will be applied to the CommandClient via CommandClient#addCommand(Command, int).

      Specified by:
      addAnnotatedModule in interface CommandClient
      Parameters:
      module - An object annotated with JDACommand.Module to compile into commands to be added.
      mapFunction - The Function to get indexes for each compiled Command with when adding them to the CommandClient.
    • getOwnerId

      public String getOwnerId()
      Description copied from interface: CommandClient
      Gets the ID of the owner of this bot as a String.
      Specified by:
      getOwnerId in interface CommandClient
      Returns:
      The String ID of the owner of the bot
    • getOwnerIdLong

      public long getOwnerIdLong()
      Description copied from interface: CommandClient
      Gets the ID of the owner of this bot as a long.
      Specified by:
      getOwnerIdLong in interface CommandClient
      Returns:
      The long ID of the owner of the bot
    • getCoOwnerIds

      public String[] getCoOwnerIds()
      Description copied from interface: CommandClient
      Gets the ID(s) of any CoOwners of this bot as a String Array.
      Specified by:
      getCoOwnerIds in interface CommandClient
      Returns:
      The String ID(s) of any CoOwners of this bot
    • getCoOwnerIdsLong

      public long[] getCoOwnerIdsLong()
      Description copied from interface: CommandClient
      Gets the ID(s) of any CoOwners of this bot as a long Array.
      Specified by:
      getCoOwnerIdsLong in interface CommandClient
      Returns:
      The long ID(s) of any CoOwners of this bot
    • getSuccess

      public String getSuccess()
      Description copied from interface: CommandClient
      Gets the success emoji.
      Specified by:
      getSuccess in interface CommandClient
      Returns:
      The success emoji
    • getWarning

      public String getWarning()
      Description copied from interface: CommandClient
      Gets the warning emoji.
      Specified by:
      getWarning in interface CommandClient
      Returns:
      The warning emoji
    • getError

      public String getError()
      Description copied from interface: CommandClient
      Gets the error emoji.
      Specified by:
      getError in interface CommandClient
      Returns:
      The error emoji
    • getScheduleExecutor

      public ScheduledExecutorService getScheduleExecutor()
      Description copied from interface: CommandClient
      Gets the ScheduledExecutorService held by this client.

      This is used for methods such as CommandEvent#async(Runnable) run code asynchronously.

      Specified by:
      getScheduleExecutor in interface CommandClient
      Returns:
      The ScheduledExecutorService held by this client.
    • getServerInvite

      public String getServerInvite()
      Description copied from interface: CommandClient
      Gets the invite to the bot's support server.
      Specified by:
      getServerInvite in interface CommandClient
      Returns:
      A possibly-null server invite
    • getPrefix

      public String getPrefix()
      Description copied from interface: CommandClient
      Gets the Client's prefix.
      Specified by:
      getPrefix in interface CommandClient
      Returns:
      A possibly-null prefix
    • getPrefixes

      public String[] getPrefixes()
      Description copied from interface: CommandClient
      Gets the array of prefixes
      Specified by:
      getPrefixes in interface CommandClient
      Returns:
      A possibly-null list of prefixes
    • getPrefixFunction

      public Function<net.dv8tion.jda.api.events.message.MessageReceivedEvent,String> getPrefixFunction()
      Description copied from interface: CommandClient
      Gets the prefix BiConsumer
      Specified by:
      getPrefixFunction in interface CommandClient
      Returns:
      A possibly-null prefix BiConsumer
    • getAltPrefix

      public String getAltPrefix()
      Description copied from interface: CommandClient
      Gets the Client's alternate prefix.
      Specified by:
      getAltPrefix in interface CommandClient
      Returns:
      A possibly-null alternate prefix
    • getTextualPrefix

      public String getTextualPrefix()
      Description copied from interface: CommandClient
      Returns the visual representation of the bot's prefix.

      This is the same as CommandClient.getPrefix() unless the prefix is the default, in which case it appears as @Botname.

      Specified by:
      getTextualPrefix in interface CommandClient
      Returns:
      A never-null prefix
    • getTotalGuilds

      public int getTotalGuilds()
      Description copied from interface: CommandClient
      Gets an a recently updated count of all the Guilds the bot is connected to on all shards.

      NOTE: This may not always or should not be assumed accurate! Any time a shard joins or leaves a guild it will update the number retrieved by this method but will not update when other shards join or leave guilds. This means that shards will not always retrieve the same value. For instance:

      • 1) Shard A joins 10 Guilds
      • 2) Shard B invokes this method
      • 3) Shard A invokes this method
      The number retrieved by Shard B will be that of the number retrieved by Shard A, minus 10 guilds because Shard B hasn't updated and accounted for those 10 guilds on Shard A.

      This feature requires a Discord Bots API Key to be set!
      To set your Discord Bots API Key, you'll have to retrieve it from the Discord Bots website.

      Specified by:
      getTotalGuilds in interface CommandClient
      Returns:
      A recently updated count of all the Guilds the bot is connected to on all shards.
    • getHelpWord

      public String getHelpWord()
      Description copied from interface: CommandClient
      Gets the word used to invoke a help DM.
      Specified by:
      getHelpWord in interface CommandClient
      Returns:
      The help word
    • usesLinkedDeletion

      public boolean usesLinkedDeletion()
      Description copied from interface: CommandClient
      Gets whether this CommandClient uses linked deletion.

      Linking calls is the basic principle of pairing bot responses with their calling Messages.
      Using this with a basic function such as deletion, this causes bots to delete their Messages as a response to the calling Message being deleted.

      Specified by:
      usesLinkedDeletion in interface CommandClient
      Returns:
      true if the bot uses linked deletion, false otherwise.
      See Also:
    • getSettingsFor

      public <S> S getSettingsFor(net.dv8tion.jda.api.entities.Guild guild)
      Description copied from interface: CommandClient
      Returns an Object of the type parameter that should contain settings relating to the specified Guild.

      The returning object for this is specified via provision of a GuildSettingsManager to CommandClientBuilder#setGuildSettingsManager(GuildSettingsManager), more specifically GuildSettingsManager#getSettings(Guild).

      Specified by:
      getSettingsFor in interface CommandClient
      Type Parameters:
      S - The type of settings the GuildSettingsManager provides
      Parameters:
      guild - The Guild to get Settings for
      Returns:
      The settings object for the Guild, specified in GuildSettingsManager#getSettings(Guild), can be null if the implementation allows it.
    • getSettingsManager

      public <M extends GuildSettingsManager> M getSettingsManager()
      Description copied from interface: CommandClient
      Returns the type of GuildSettingsManager, the same type of one provided when building this CommandClient, or null if one was not provided there.

      This is good if you want to use non-abstract methods specific to your implementation.

      Specified by:
      getSettingsManager in interface CommandClient
      Type Parameters:
      M - The type of the GuildSettingsManager
      Returns:
      The GuildSettingsManager, or null if one was not provided when building this CommandClient.
    • shutdown

      public void shutdown()
      Description copied from interface: CommandClient
      Shuts down internals of the Command Client, such as the threadpool and guild settings manager
      Specified by:
      shutdown in interface CommandClient
    • onEvent

      public void onEvent(net.dv8tion.jda.api.events.GenericEvent event)
      Specified by:
      onEvent in interface net.dv8tion.jda.api.hooks.EventListener
    • upsertInteractions

      public void upsertInteractions(net.dv8tion.jda.api.JDA jda)
      Description copied from interface: CommandClient
      Upserts all interactions to the provided forced server.
      This runs after the ReadyEvent has been fired if CommandClient.isManualUpsert() is false.
      If CommandClient.forcedGuildId() is null, commands will upsert globally. This may take up to an hour.
      Specified by:
      upsertInteractions in interface CommandClient
      Parameters:
      jda - The JDA instance to use
    • upsertInteractions

      public void upsertInteractions(net.dv8tion.jda.api.JDA jda, String serverId)
      Description copied from interface: CommandClient
      Upserts all interactions to the provided server.
      This runs after the ReadyEvent has been fired if CommandClient.isManualUpsert() is false.
      If null is passed for the server, commands will upsert globally. This may take up to an hour.
      Specified by:
      upsertInteractions in interface CommandClient
      Parameters:
      jda - The JDA instance to use
      serverId - The server to upsert interactions for
    • linkIds

      public void linkIds(long callId, net.dv8tion.jda.api.entities.Message message)
      DO NOT USE THIS!

      This is a method necessary for linking a bot's response messages to their corresponding call message ID.
      Using this anywhere in your code can and will break your bot.

      Parameters:
      callId - The ID of the call Message
      message - The Message to link to the ID