public class CommandClientImpl extends java.lang.Object implements CommandClient, net.dv8tion.jda.core.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
Event
s. The primary usage of this is where the CommandClient implementation
takes MessageReceivedEvent
s, and automatically
processes arguments, and provide them to a Command
for
running and execution.
Constructor and Description |
---|
CommandClientImpl(java.lang.String ownerId,
java.lang.String[] coOwnerIds,
java.lang.String prefix,
java.lang.String altprefix,
net.dv8tion.jda.core.entities.Game game,
net.dv8tion.jda.core.OnlineStatus status,
java.lang.String serverInvite,
java.lang.String success,
java.lang.String warning,
java.lang.String error,
java.lang.String carbonKey,
java.lang.String botsKey,
java.lang.String botsOrgKey,
java.util.ArrayList<Command> commands,
boolean useHelp,
java.util.function.Consumer<CommandEvent> helpConsumer,
java.lang.String helpWord,
java.util.concurrent.ScheduledExecutorService executor,
int linkedCacheSize,
AnnotatedModuleCompiler compiler,
GuildSettingsManager manager) |
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 |
linkIds(long callId,
net.dv8tion.jda.core.entities.Message message)
DO NOT USE THIS!
|
void |
onEvent(net.dv8tion.jda.core.events.Event event) |
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.
|
public CommandClientImpl(java.lang.String ownerId, java.lang.String[] coOwnerIds, java.lang.String prefix, java.lang.String altprefix, net.dv8tion.jda.core.entities.Game game, net.dv8tion.jda.core.OnlineStatus status, java.lang.String serverInvite, java.lang.String success, java.lang.String warning, java.lang.String error, java.lang.String carbonKey, java.lang.String botsKey, java.lang.String botsOrgKey, java.util.ArrayList<Command> commands, boolean useHelp, java.util.function.Consumer<CommandEvent> helpConsumer, java.lang.String helpWord, java.util.concurrent.ScheduledExecutorService executor, int linkedCacheSize, AnnotatedModuleCompiler compiler, GuildSettingsManager manager)
public void setListener(CommandListener listener)
CommandClient
CommandListener
to catch
command-related events thrown by this CommandClient
.setListener
in interface CommandClient
listener
- The CommandListenerpublic CommandListener getListener()
CommandClient
CommandListener
.getListener
in interface CommandClient
public java.util.List<Command> getCommands()
CommandClient
Command
s
during this session.getCommands
in interface CommandClient
public java.time.OffsetDateTime getStartTime()
CommandClient
CommandClient
implementation was created.getStartTime
in interface CommandClient
public java.time.OffsetDateTime getCooldown(java.lang.String name)
CommandClient
OffsetDateTime
that the specified cooldown expires.getCooldown
in interface CommandClient
name
- The cooldown namepublic int getRemainingCooldown(java.lang.String name)
CommandClient
getRemainingCooldown
in interface CommandClient
name
- The cooldown namepublic void applyCooldown(java.lang.String name, int seconds)
CommandClient
applyCooldown
in interface CommandClient
name
- The cooldown nameseconds
- The time to make the cooldown lastpublic void cleanCooldowns()
CommandClient
cleanCooldowns
in interface CommandClient
public int getCommandUses(Command command)
CommandClient
Command
during this session, or 0
if the command is not registered to this CommandClient.getCommandUses
in interface CommandClient
command
- The Commandpublic int getCommandUses(java.lang.String name)
CommandClient
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
.
getCommandUses
in interface CommandClient
name
- The name of the Command0
if the name does not match with a Commandpublic void addCommand(Command command)
CommandClient
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.
addCommand
in interface CommandClient
command
- The Command to addpublic void addCommand(Command command, int index)
CommandClient
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
).
addCommand
in interface CommandClient
command
- The Command to addindex
- The index to add the Command at (must follow the specifications 0<=index<=size()
)public void removeCommand(java.lang.String name)
CommandClient
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
).
removeCommand
in interface CommandClient
name
- The name or an alias of the Command to removepublic void addAnnotatedModule(java.lang.Object module)
CommandClient
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.
addAnnotatedModule
in interface CommandClient
module
- An object annotated with JDACommand.Module to compile into commands to be added.public void addAnnotatedModule(java.lang.Object module, java.util.function.Function<Command,java.lang.Integer> mapFunction)
CommandClient
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)
.
addAnnotatedModule
in interface CommandClient
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.public java.lang.String getOwnerId()
CommandClient
getOwnerId
in interface CommandClient
public long getOwnerIdLong()
CommandClient
long
.getOwnerIdLong
in interface CommandClient
long
ID of the owner of the botpublic java.lang.String[] getCoOwnerIds()
CommandClient
getCoOwnerIds
in interface CommandClient
public long[] getCoOwnerIdsLong()
CommandClient
long
Array.getCoOwnerIdsLong
in interface CommandClient
long
ID(s) of any CoOwners of this botpublic java.lang.String getSuccess()
CommandClient
getSuccess
in interface CommandClient
public java.lang.String getWarning()
CommandClient
getWarning
in interface CommandClient
public java.lang.String getError()
CommandClient
getError
in interface CommandClient
public java.util.concurrent.ScheduledExecutorService getScheduleExecutor()
CommandClient
ScheduledExecutorService
held by this client.
This is used for methods such as CommandEvent#async(Runnable)
run code asynchronously.
getScheduleExecutor
in interface CommandClient
public java.lang.String getServerInvite()
CommandClient
getServerInvite
in interface CommandClient
public java.lang.String getPrefix()
CommandClient
getPrefix
in interface CommandClient
public java.lang.String getAltPrefix()
CommandClient
getAltPrefix
in interface CommandClient
public java.lang.String getTextualPrefix()
CommandClient
This is the same as CommandClient.getPrefix()
unless the prefix is the default,
in which case it appears as @Botname.
getTextualPrefix
in interface CommandClient
public int getTotalGuilds()
CommandClient
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.
getTotalGuilds
in interface CommandClient
public java.lang.String getHelpWord()
CommandClient
getHelpWord
in interface CommandClient
public boolean usesLinkedDeletion()
CommandClient
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.
usesLinkedDeletion
in interface CommandClient
true
if the bot uses linked deletion, false
otherwise.CommandClientBuilder#setLinkedCacheSize(int)
public <S> S getSettingsFor(net.dv8tion.jda.core.entities.Guild guild)
CommandClient
Guild
.
The returning object for this is specified via provision of a
GuildSettingsManager
to
CommandClientBuilder#setGuildSettingsManager(GuildSettingsManager)
, more specifically
GuildSettingsManager#getSettings(Guild)
.
getSettingsFor
in interface CommandClient
S
- The type of settings the GuildSettingsManager providesguild
- The Guild to get Settings forGuildSettingsManager#getSettings(Guild)
, can be null
if the implementation
allows it.public <M extends GuildSettingsManager> M getSettingsManager()
CommandClient
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.
getSettingsManager
in interface CommandClient
M
- The type of the GuildSettingsManagernull
if one was not provided when building this CommandClient.public void onEvent(net.dv8tion.jda.core.events.Event event)
onEvent
in interface net.dv8tion.jda.core.hooks.EventListener
public void linkIds(long callId, net.dv8tion.jda.core.entities.Message message)
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.
callId
- The ID of the call Messagemessage
- The Message to link to the ID