Class CommandClientImpl
- All Implemented Interfaces:
CommandClient,net.dv8tion.jda.api.hooks.EventListener
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 Summary
ConstructorsConstructorDescriptionCommandClientImpl(String ownerId, String[] coOwnerIds, String prefix, String altprefix, String[] prefixes, Function<net.dv8tion.jda.api.events.message.MessageReceivedEvent, String> prefixFunction, Function<net.dv8tion.jda.api.events.message.MessageReceivedEvent, Boolean> commandPreProcessFunction, net.dv8tion.jda.api.entities.Activity activity, net.dv8tion.jda.api.OnlineStatus status, String serverInvite, String success, String warning, String error, String carbonKey, String botsKey, ArrayList<Command> commands, ArrayList<SlashCommand> slashCommands, String forcedGuildId, boolean manualUpsert, boolean useHelp, boolean shutdownAutomatically, Consumer<CommandEvent> helpConsumer, String helpWord, ScheduledExecutorService executor, int linkedCacheSize, AnnotatedModuleCompiler compiler, GuildSettingsManager manager) -
Method Summary
Modifier and TypeMethodDescriptionvoidaddAnnotatedModule(Object module) Compiles the providedObjectannotated withJDACommand.Moduleinto aListofCommands and adds them to this CommandClient in the order they are listed.voidaddAnnotatedModule(Object module, Function<Command, Integer> mapFunction) Compiles the providedObjectannotated withJDACommand.Moduleinto aListofCommands and adds them to this CommandClient via theFunctionprovided.voidaddCommand(Command command) Adds a singleCommandto this CommandClient's registered Commands.voidaddCommand(Command command, int index) Adds a singleCommandto this CommandClient's registered Commands at the specified index.voidaddSlashCommand(SlashCommand command) Adds a singleSlashCommandto this CommandClient's registered SlashCommand.voidaddSlashCommand(SlashCommand command, int index) Adds a singleSlashCommandto this CommandClient's registered Commands at the specified index.voidapplyCooldown(String name, int seconds) Applies the specified cooldown with the provided name.voidCleans up expired cooldowns to reduce memory.Returns the forced Guild ID for automatic slash command upsertsGets the Client's alternate prefix.Returns the list of registeredCommands during this session.intgetCommandUses(Command command) Gets the number of uses for the provideCommandduring this session, or0if the command is not registered to this CommandClient.intgetCommandUses(String name) Gets the number of uses for aCommandduring this session matching the provided String name, or0if there is no Command with the name.getCooldown(String name) Gets theOffsetDateTimethat the specified cooldown expires.String[]Gets the ID(s) of any CoOwners of this bot as a String Array.long[]Gets the ID(s) of any CoOwners of this bot as alongArray.getError()Gets the error emoji.Gets the word used to invoke a help DM.Returns the currentCommandListener.Gets the ID of the owner of this bot as a String.longGets the ID of the owner of this bot as along.Gets the Client's prefix.String[]Gets the array of prefixesGets the prefix BiConsumerintgetRemainingCooldown(String name) Gets the remaining number of seconds on the specified cooldown.Gets theScheduledExecutorServiceheld by this client.Gets the invite to the bot's support server.<S> SgetSettingsFor(net.dv8tion.jda.api.entities.Guild guild) Returns an Object of the type parameter that should contain settings relating to the specifiedGuild.<M extends GuildSettingsManager>
MReturns the type ofGuildSettingsManager, the same type of one provided when building this CommandClient, ornullif one was not provided there.Returns the list of registeredSlashCommands during this session.Gets the time thisCommandClientimplementation was created.Gets the success emoji.Returns the visual representation of the bot's prefix.intGets an a recently updated count of all theGuilds the bot is connected to on all shards.Gets the warning emoji.booleanReturns whether manual upsertion is enabledvoidlinkIds(long callId, net.dv8tion.jda.api.entities.Message message) DO NOT USE THIS!voidonEvent(net.dv8tion.jda.api.events.GenericEvent event) voidremoveCommand(String name) Removes a singleCommandfrom this CommandClient's registered Commands at the index linked to the provided name/alias.voidsetListener(CommandListener listener) Sets theCommandListenerto catch command-related events thrown by thisCommandClient.voidshutdown()Shuts down internals of the Command Client, such as the threadpool and guild settings managerbooleanGets whether this CommandClient uses linked deletion.
-
Constructor Details
-
CommandClientImpl
public CommandClientImpl(String ownerId, String[] coOwnerIds, String prefix, String altprefix, String[] prefixes, Function<net.dv8tion.jda.api.events.message.MessageReceivedEvent, String> prefixFunction, Function<net.dv8tion.jda.api.events.message.MessageReceivedEvent, Boolean> commandPreProcessFunction, net.dv8tion.jda.api.entities.Activity activity, net.dv8tion.jda.api.OnlineStatus status, String serverInvite, String success, String warning, String error, String carbonKey, String botsKey, ArrayList<Command> commands, ArrayList<SlashCommand> slashCommands, String forcedGuildId, boolean manualUpsert, boolean useHelp, boolean shutdownAutomatically, Consumer<CommandEvent> helpConsumer, String helpWord, ScheduledExecutorService executor, int linkedCacheSize, AnnotatedModuleCompiler compiler, GuildSettingsManager manager)
-
-
Method Details
-
setListener
Description copied from interface:CommandClientSets theCommandListenerto catch command-related events thrown by thisCommandClient.- Specified by:
setListenerin interfaceCommandClient- Parameters:
listener- The CommandListener
-
getListener
Description copied from interface:CommandClientReturns the currentCommandListener.- Specified by:
getListenerin interfaceCommandClient- Returns:
- A possibly-null CommandListener
-
getCommands
Description copied from interface:CommandClientReturns the list of registeredCommands during this session.- Specified by:
getCommandsin interfaceCommandClient- Returns:
- A never-null List of Commands registered during this session
-
getSlashCommands
Description copied from interface:CommandClientReturns the list of registeredSlashCommands during this session.- Specified by:
getSlashCommandsin interfaceCommandClient- Returns:
- A never-null List of Slash Commands registered during this session
-
isManualUpsert
public boolean isManualUpsert()Description copied from interface:CommandClientReturns whether manual upsertion is enabled- Specified by:
isManualUpsertin interfaceCommandClient- Returns:
- The manual upsertion status
-
forcedGuildId
Description copied from interface:CommandClientReturns the forced Guild ID for automatic slash command upserts- Specified by:
forcedGuildIdin interfaceCommandClient- Returns:
- A possibly-null forcedGuildId set in the builder
-
getStartTime
Description copied from interface:CommandClientGets the time thisCommandClientimplementation was created.- Specified by:
getStartTimein interfaceCommandClient- Returns:
- The start time of this CommandClient implementation
-
getCooldown
Description copied from interface:CommandClientGets theOffsetDateTimethat the specified cooldown expires.- Specified by:
getCooldownin interfaceCommandClient- Parameters:
name- The cooldown name- Returns:
- The expiration time, or null if the cooldown does not exist
-
getRemainingCooldown
Description copied from interface:CommandClientGets the remaining number of seconds on the specified cooldown.- Specified by:
getRemainingCooldownin interfaceCommandClient- Parameters:
name- The cooldown name- Returns:
- The number of seconds remaining
-
applyCooldown
Description copied from interface:CommandClientApplies the specified cooldown with the provided name.- Specified by:
applyCooldownin interfaceCommandClient- Parameters:
name- The cooldown nameseconds- The time to make the cooldown last
-
cleanCooldowns
public void cleanCooldowns()Description copied from interface:CommandClientCleans up expired cooldowns to reduce memory.- Specified by:
cleanCooldownsin interfaceCommandClient
-
getCommandUses
Description copied from interface:CommandClientGets the number of uses for the provideCommandduring this session, or0if the command is not registered to this CommandClient.- Specified by:
getCommandUsesin interfaceCommandClient- Parameters:
command- The Command- Returns:
- The number of uses for the Command
-
getCommandUses
Description copied from interface:CommandClientGets the number of uses for aCommandduring this session matching the provided String name, or0if there is no Command with the name.NOTE: this method WILL NOT get uses for a command if an
aliasis provided! Also note thatchild commandsARE NOT tracked and providing names or effective names of child commands will return0.- Specified by:
getCommandUsesin interfaceCommandClient- Parameters:
name- The name of the Command- Returns:
- The number of uses for the Command, or
0if the name does not match with a Command
-
addCommand
Description copied from interface:CommandClientAdds a singleCommandto 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
ArrayListto 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 aHashMapwhich links theirnameand theiraliasesto 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:
addCommandin interfaceCommandClient- Parameters:
command- The Command to add
-
addCommand
Description copied from interface:CommandClientAdds a singleCommandto 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
ArrayListto 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 aHashMapwhich links theirnameand theiraliasesto 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:
addCommandin interfaceCommandClient- Parameters:
command- The Command to addindex- The index to add the Command at (must follow the specifications0<=index<=size())
-
addSlashCommand
Description copied from interface:CommandClientAdds a singleSlashCommandto 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
ArrayListto 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 aHashMapwhich links theirnameto 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:
addSlashCommandin interfaceCommandClient- Parameters:
command- The Command to add
-
addSlashCommand
Description copied from interface:CommandClientAdds a singleSlashCommandto 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
ArrayListto 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 aHashMapwhich links theirnameto 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:
addSlashCommandin interfaceCommandClient- Parameters:
command- The Command to addindex- The index to add the Command at (must follow the specifications0<=index<=size())
-
removeCommand
Description copied from interface:CommandClientRemoves a singleCommandfrom 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
ArrayListto 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 aHashMapwhich links theirnameand theiraliasesto 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:
removeCommandin interfaceCommandClient- Parameters:
name- The name or an alias of the Command to remove
-
addAnnotatedModule
Description copied from interface:CommandClientCompiles the providedObjectannotated withJDACommand.Moduleinto aListofCommands and adds them to this CommandClient in the order they are listed.This is done through the
AnnotatedModuleCompilerprovided when building this CommandClient.- Specified by:
addAnnotatedModulein interfaceCommandClient- Parameters:
module- An object annotated with JDACommand.Module to compile into commands to be added.
-
addAnnotatedModule
Description copied from interface:CommandClientCompiles the providedObjectannotated withJDACommand.Moduleinto aListofCommands and adds them to this CommandClient via theFunctionprovided.This is done through the
AnnotatedModuleCompilerprovided when building this CommandClient.The Function will
applyeachCommandin the compiled list and request anintin return.
Using thisint, the command provided will be applied to the CommandClient viaCommandClient#addCommand(Command, int).- Specified by:
addAnnotatedModulein interfaceCommandClient- 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
Description copied from interface:CommandClientGets the ID of the owner of this bot as a String.- Specified by:
getOwnerIdin interfaceCommandClient- Returns:
- The String ID of the owner of the bot
-
getOwnerIdLong
public long getOwnerIdLong()Description copied from interface:CommandClientGets the ID of the owner of this bot as along.- Specified by:
getOwnerIdLongin interfaceCommandClient- Returns:
- The
longID of the owner of the bot
-
getCoOwnerIds
Description copied from interface:CommandClientGets the ID(s) of any CoOwners of this bot as a String Array.- Specified by:
getCoOwnerIdsin interfaceCommandClient- Returns:
- The String ID(s) of any CoOwners of this bot
-
getCoOwnerIdsLong
public long[] getCoOwnerIdsLong()Description copied from interface:CommandClientGets the ID(s) of any CoOwners of this bot as alongArray.- Specified by:
getCoOwnerIdsLongin interfaceCommandClient- Returns:
- The
longID(s) of any CoOwners of this bot
-
getSuccess
Description copied from interface:CommandClientGets the success emoji.- Specified by:
getSuccessin interfaceCommandClient- Returns:
- The success emoji
-
getWarning
Description copied from interface:CommandClientGets the warning emoji.- Specified by:
getWarningin interfaceCommandClient- Returns:
- The warning emoji
-
getError
Description copied from interface:CommandClientGets the error emoji.- Specified by:
getErrorin interfaceCommandClient- Returns:
- The error emoji
-
getScheduleExecutor
Description copied from interface:CommandClientGets theScheduledExecutorServiceheld by this client.This is used for methods such as
CommandEvent#async(Runnable)run code asynchronously.- Specified by:
getScheduleExecutorin interfaceCommandClient- Returns:
- The ScheduledExecutorService held by this client.
-
getServerInvite
Description copied from interface:CommandClientGets the invite to the bot's support server.- Specified by:
getServerInvitein interfaceCommandClient- Returns:
- A possibly-null server invite
-
getPrefix
Description copied from interface:CommandClientGets the Client's prefix.- Specified by:
getPrefixin interfaceCommandClient- Returns:
- A possibly-null prefix
-
getPrefixes
Description copied from interface:CommandClientGets the array of prefixes- Specified by:
getPrefixesin interfaceCommandClient- Returns:
- A possibly-null list of prefixes
-
getPrefixFunction
Description copied from interface:CommandClientGets the prefix BiConsumer- Specified by:
getPrefixFunctionin interfaceCommandClient- Returns:
- A possibly-null prefix BiConsumer
-
getAltPrefix
Description copied from interface:CommandClientGets the Client's alternate prefix.- Specified by:
getAltPrefixin interfaceCommandClient- Returns:
- A possibly-null alternate prefix
-
getTextualPrefix
Description copied from interface:CommandClientReturns 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:
getTextualPrefixin interfaceCommandClient- Returns:
- A never-null prefix
-
getTotalGuilds
public int getTotalGuilds()Description copied from interface:CommandClientGets an a recently updated count of all theGuilds 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
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:
getTotalGuildsin interfaceCommandClient- Returns:
- A recently updated count of all the Guilds the bot is connected to on all shards.
-
getHelpWord
Description copied from interface:CommandClientGets the word used to invoke a help DM.- Specified by:
getHelpWordin interfaceCommandClient- Returns:
- The help word
-
usesLinkedDeletion
public boolean usesLinkedDeletion()Description copied from interface:CommandClientGets 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:
usesLinkedDeletionin interfaceCommandClient- Returns:
trueif the bot uses linked deletion,falseotherwise.- See Also:
-
getSettingsFor
public <S> S getSettingsFor(net.dv8tion.jda.api.entities.Guild guild) Description copied from interface:CommandClientReturns an Object of the type parameter that should contain settings relating to the specifiedGuild.The returning object for this is specified via provision of a
GuildSettingsManagertoCommandClientBuilder#setGuildSettingsManager(GuildSettingsManager), more specificallyGuildSettingsManager#getSettings(Guild).- Specified by:
getSettingsForin interfaceCommandClient- 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 benullif the implementation allows it.
-
getSettingsManager
Description copied from interface:CommandClientReturns the type ofGuildSettingsManager, the same type of one provided when building this CommandClient, ornullif one was not provided there.This is good if you want to use non-abstract methods specific to your implementation.
- Specified by:
getSettingsManagerin interfaceCommandClient- Type Parameters:
M- The type of the GuildSettingsManager- Returns:
- The GuildSettingsManager, or
nullif one was not provided when building this CommandClient.
-
shutdown
public void shutdown()Description copied from interface:CommandClientShuts down internals of the Command Client, such as the threadpool and guild settings manager- Specified by:
shutdownin interfaceCommandClient
-
onEvent
public void onEvent(net.dv8tion.jda.api.events.GenericEvent event) - Specified by:
onEventin interfacenet.dv8tion.jda.api.hooks.EventListener
-
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 Messagemessage- The Message to link to the ID
-