public interface CommandClient
This is implemented in CommandClientImpl
alongside implementation of EventListener
to create a
compounded "Client Listener" which catches specific kinds of events thrown by JDA and processes them
automatically to handle and execute Command
s.
Implementations also serve as a useful platforms, carrying reference info such as the bot's Owner ID, prefix, and a support server invite.
For the CommandClientImpl, once initialized, only the following can be modified:
Command
s may be added or removed.CommandListener
may be set.1) Do not build and add more than one CommandClient to an instance JDA, EVER.
2) Always create and add the CommandClientImpl to JDA BEFORE you build it, or there is a
chance some minor errors will occur, especially if JDA has already fired a ReadyEvent
.
3) Do not provide anything other than a String representing a long (and furthermore a User ID) as
an Owner ID or a CoOwner ID. This will generate errors, but not stop the creation of the
CommandClientImpl which will cause several errors to occur very quickly after startup (except
if you provide null
for the Owner ID, that'll just flat out throw an IllegalArgumentException
).
4) Do not provide strings when using CommandClientBuilder#setEmojis(String, String, String)
that are not unicode emojis or that do
not match the custom emote format specified in Emote#getAsMention()
(IE: <:EmoteName:EmoteID>
).
5) Avoid using CommandClientImpl.linkIds(long,
net.dv8tion.jda.core.entities.Message)
. This will create errors and has no real purpose outside
of it's current usage.
Modifier and Type | Method and Description |
---|---|
void |
addAnnotatedModule(java.lang.Object module)
Compiles the provided
Object annotated with JDACommand.Module into a List of Command s and adds them to this CommandClient in
the order they are listed. |
void |
addAnnotatedModule(java.lang.Object module,
java.util.function.Function<Command,java.lang.Integer> mapFunction)
Compiles the provided
Object annotated with JDACommand.Module into a List of Command s and adds them to this CommandClient via
the Function provided. |
void |
addCommand(Command command)
Adds a single
Command to this CommandClient's
registered Commands. |
void |
addCommand(Command command,
int index)
Adds a single
Command to this CommandClient's
registered Commands at the specified index. |
void |
applyCooldown(java.lang.String name,
int seconds)
Applies the specified cooldown with the provided name.
|
void |
cleanCooldowns()
Cleans up expired cooldowns to reduce memory.
|
java.lang.String |
getAltPrefix()
Gets the Client's alternate prefix.
|
java.util.List<Command> |
getCommands()
Returns the list of registered
Command s
during this session. |
int |
getCommandUses(Command command)
Gets the number of uses for the provide
Command
during this session, or 0 if the command is not registered to this CommandClient. |
int |
getCommandUses(java.lang.String name)
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. |
java.time.OffsetDateTime |
getCooldown(java.lang.String name)
Gets the
OffsetDateTime that the specified cooldown expires. |
java.lang.String[] |
getCoOwnerIds()
Gets the ID(s) of any CoOwners of this bot as a String Array.
|
long[] |
getCoOwnerIdsLong()
Gets the ID(s) of any CoOwners of this bot as a
long Array. |
java.lang.String |
getError()
Gets the error emoji.
|
java.lang.String |
getHelpWord()
Gets the word used to invoke a help DM.
|
CommandListener |
getListener()
Returns the current
CommandListener . |
java.lang.String |
getOwnerId()
Gets the ID of the owner of this bot as a String.
|
long |
getOwnerIdLong()
Gets the ID of the owner of this bot as a
long . |
java.lang.String |
getPrefix()
Gets the Client's prefix.
|
int |
getRemainingCooldown(java.lang.String name)
Gets the remaining number of seconds on the specified cooldown.
|
java.util.concurrent.ScheduledExecutorService |
getScheduleExecutor()
Gets the
ScheduledExecutorService held by this client. |
java.lang.String |
getServerInvite()
Gets the invite to the bot's support server.
|
<S> S |
getSettingsFor(net.dv8tion.jda.core.entities.Guild guild)
Returns an Object of the type parameter that should contain settings relating to the specified
Guild . |
<M extends GuildSettingsManager> |
getSettingsManager()
Returns the type of
GuildSettingsManager ,
the same type of one provided when building this CommandClient, or null if one was not provided there. |
java.time.OffsetDateTime |
getStartTime()
Gets the time this
CommandClient
implementation was created. |
java.lang.String |
getSuccess()
Gets the success emoji.
|
java.lang.String |
getTextualPrefix()
Returns the visual representation of the bot's prefix.
|
int |
getTotalGuilds()
Gets an a recently updated count of all the
Guild s
the bot is connected to on all shards. |
java.lang.String |
getWarning()
Gets the warning emoji.
|
void |
removeCommand(java.lang.String name)
Removes a single
Command from this CommandClient's
registered Commands at the index linked to the provided name/alias. |
void |
setListener(CommandListener listener)
Sets the
CommandListener to catch
command-related events thrown by this CommandClient . |
boolean |
usesLinkedDeletion()
Gets whether this CommandClient uses linked deletion.
|
java.lang.String getPrefix()
java.lang.String getAltPrefix()
java.lang.String getTextualPrefix()
This is the same as getPrefix()
unless the prefix is the default,
in which case it appears as @Botname.
void addCommand(Command command)
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.
command
- The Command to addjava.lang.IllegalArgumentException
- If the Command provided has a name or alias that has already been registeredvoid addCommand(Command command, int index)
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
).
command
- The Command to addindex
- The index to add the Command at (must follow the specifications 0<=index<=size()
)java.lang.ArrayIndexOutOfBoundsException
- If index < 0
or index > size()
java.lang.IllegalArgumentException
- If the Command provided has a name or alias that has already been registered to an indexvoid removeCommand(java.lang.String name)
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
).
name
- The name or an alias of the Command to removejava.lang.IllegalArgumentException
- If the name provided was not registered to an indexvoid addAnnotatedModule(java.lang.Object module)
Object
annotated with JDACommand.Module
into a List
of Command
s and adds them to this CommandClient in
the order they are listed.
This is done through the AnnotatedModuleCompiler
provided when building this CommandClient.
module
- An object annotated with JDACommand.Module to compile into commands to be added.java.lang.IllegalArgumentException
- If the Command provided has a name or alias that has already been registeredvoid addAnnotatedModule(java.lang.Object module, java.util.function.Function<Command,java.lang.Integer> mapFunction)
Object
annotated with JDACommand.Module
into a List
of Command
s 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)
.
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.java.lang.ArrayIndexOutOfBoundsException
- If index < 0
or index > size()
java.lang.IllegalArgumentException
- If the Command provided has a name or alias that has already been registered to an indexvoid setListener(CommandListener listener)
CommandListener
to catch
command-related events thrown by this CommandClient
.listener
- The CommandListenerCommandListener getListener()
CommandListener
.java.util.List<Command> getCommands()
Command
s
during this session.java.time.OffsetDateTime getStartTime()
CommandClient
implementation was created.java.time.OffsetDateTime getCooldown(java.lang.String name)
OffsetDateTime
that the specified cooldown expires.name
- The cooldown nameint getRemainingCooldown(java.lang.String name)
name
- The cooldown namevoid applyCooldown(java.lang.String name, int seconds)
name
- The cooldown nameseconds
- The time to make the cooldown lastvoid cleanCooldowns()
int getCommandUses(Command command)
Command
during this session, or 0
if the command is not registered to this CommandClient.command
- The Commandint getCommandUses(java.lang.String name)
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
.
name
- The name of the Command0
if the name does not match with a Commandjava.lang.String getOwnerId()
long getOwnerIdLong()
long
.long
ID of the owner of the botjava.lang.String[] getCoOwnerIds()
long[] getCoOwnerIdsLong()
long
Array.long
ID(s) of any CoOwners of this botjava.lang.String getSuccess()
java.lang.String getWarning()
java.lang.String getError()
java.util.concurrent.ScheduledExecutorService getScheduleExecutor()
ScheduledExecutorService
held by this client.
This is used for methods such as CommandEvent#async(Runnable)
run code asynchronously.
java.lang.String getServerInvite()
int getTotalGuilds()
Guild
s
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:
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.
java.lang.String getHelpWord()
boolean usesLinkedDeletion()
Linking calls is the basic principle of pairing bot responses with their calling
Message
s.
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.
true
if the bot uses linked deletion, false
otherwise.CommandClientBuilder#setLinkedCacheSize(int)
<S> S getSettingsFor(net.dv8tion.jda.core.entities.Guild guild)
Guild
.
The returning object for this is specified via provision of a
GuildSettingsManager
to
CommandClientBuilder#setGuildSettingsManager(GuildSettingsManager)
, more specifically
GuildSettingsManager#getSettings(Guild)
.
S
- The type of settings the GuildSettingsManager providesguild
- The Guild to get Settings forGuildSettingsManager#getSettings(Guild)
, can be null
if the implementation
allows it.<M extends GuildSettingsManager> M getSettingsManager()
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.
M
- The type of the GuildSettingsManagernull
if one was not provided when building this CommandClient.