-
Notifications
You must be signed in to change notification settings - Fork 111
Commands
The internal inheritance for Commands used in JDA-Utilities is that of the Command
object.
Classes created inheriting this class gain the unique traits of commands operated using the Commands Extension.
Using several fields, a command can define properties that make it unique and complex while maintaining a low level of development.
All Commands extending this class can define any number of these fields in a object constructor and then create the command action/response in the abstract execute(CommandEvent event)
body:
public class ExampleCmd extends Command
{
public ExampleCmd()
{
this.name = "example";
this.aliases = new String[]{"test","demo"};
this.help = "gives an example of commands do";
}
@Override
protected void execute(CommandEvent event)
{
event.reply("Hey look! This would be the bot's reply if this was a command!");
}
}
Execution is with the provision of a MessageReceivedEvent-CommandClient wrapper called a CommandEvent and is performed in two steps:
- run - The command runs through a series of conditionals, automatically terminating the command instance if one is not met, and possibly providing an error response.
- execute - The command, now being cleared to run, executes and performs whatever lies in the abstract body method.
Command's can be added to a CommandClient
using CommandClientBuilder#addCommand(Command)
, CommandClientBuilder#addCommands(Command...)
, CommandClient#addCommand(Command, int)
, or CommandClient#addCommand(Command)
.
Note: the differences between the four are documented, but when possible you should try to use the first two, and avoid the second two.
The CommandClient
is a suite and framework for easy creation and implementation of Command
s.
It's standard implementation is CommandClientImpl
and with the provision of certain constants, it is a powerful tool for running commands.
A CommandClient
is built through the use of the CommandClientBuilder
.
CommandClientBuilder builder = new CommandClientBuilder();
After instantiation, several constants can be set so that the CommandClient generated will hold useful information that can be used during Command
execution.
// Set the bot's Owner ID
builder.setOwnerId("12345678910111213");
// Set the bot's prefix
builder.setPrefix("!?!");
// Set the bot's "Playing" status
builder.setGame(Game.of("Hello World"));
// Add Commands
builder.addCommands(new FooCommand(), new BooCommand());
// And More!
Once everything is set, all there is to do is build()
the CommandClient
and add it to JDABuilder
!
CommandClient client = builder.build();
JDABuilder jdaBuilder = new JDABuilder(AccountType.BOT).addEventListener(client);
After that, the rest will be handled by CommandClient
.
NOTE: There are some common mistakes and misconceptions regarding how to implement the built
CommandClient
into an instance ofJDA
.
It must be added to
JDA
as a listener manually.There is not "magic" that occurs when
CommandClientBuilder#build()
happens. It returns aCommandClientImpl
which is like any otherEventListener
and must be added to an instance ofJDA
.It must be added before a
ReadyEvent
is fired or there could be errors.This is because some implementations of certain constants or functions happen
onReady
and if theCommandClient
is added to JDA after it is ready, these constants and functions will remain unimplemented.This can easily be avoided by always adding the built
CommandClient
to an instance ofJDABuilder
before eitherJDABuilder#buildBlocking()
orJDABuilder#buildAsync()
is called!This is not compatible with Selfbots or Userbots.
CommandClient will never correctly work with selfbots and it was never intended to do such a thing.
Only the CommandListener can be changed and Commands can be added/removed after building this.
The only modifiable elements of CommandClient after
CommandClientBuilder#build()
is called is theCommandClient
'sCommandListener
itsCommand
's.