-
Notifications
You must be signed in to change notification settings - Fork 22
Getting Started
Sdcf4j uses annotations to indicate commands. Let's start creating a simple info command which gives some information about the bot.
You can create commands in every class which implementes the CommandExecutor interface. The interface has no methods, but is used to mark the class as command executor.
Your class may look like this:
package <your.package>;
import de.btobastian.sdcf4j.CommandExecutor;
public class InfoCommand implements CommandExecutor {
}
Now you have to create a method which will be invoked when someone uses the command. The name, parameters and return-type of the method are dynamic so you have to to decide what you need. Our info command only needs String as return-type and maybe the arguments of the command if you want to support stuff like !info author
. In our example we want to support the following commands:
-
!info
Gives a rough overview about the bot. -
!info author
Gives some information about the author of the bot like name and age. -
!info time
Shows the current time (don't ask me why we need such a command).
package <your.package>;
import de.btobastian.sdcf4j.Command;
import de.btobastian.sdcf4j.CommandExecutor;
public class InfoCommand implements CommandExecutor {
@Command(aliases = {"!info", "!i" }, description = "Shows some information about the bot.", usage = "!info [author|time]")
public String onInfoCommand(String[] args) {
return null; // dummy return type
}
}
The aliases
array contains all commands you can use. In our example it's !info
and as a shortcut !i
. The description and usage is required for help commands. I will come to this later. The args-parameter is a String array which contains all arguments of the command. For !info abc def 123
the array would be ["abc", "def", "123"]
. The returned String is what the bot will "say". You can use void
, int
, etc., too if you don't want an output, a number as output, ...
The implementation is pretty easy now:
package <your.package>;
import de.btobastian.sdcf4j.Command;
import de.btobastian.sdcf4j.CommandExecutor;
import java.text.SimpleDateFormat;
import java.util.Date;
public class InfoCommand implements CommandExecutor {
@Command(aliases = "!info", description = "Shows some information about the bot.", usage = "!info [author|time]")
public String onInfoCommand(String[] args) {
if (args.length > 1) { // more than 1 argument
return "Too many arguments!";
}
if (args.length == 0) { // !info
return "- **Author:** <YourName>\n" +
"- **Language:** Java\n" +
"- **Command-Lib:** sdcf4j";
}
if (args.length == 1) { // 1 argument
if (args[0].equals("author")) { // !info author
return "- **Name:** <YourName>\n" +
"- **Age:** <YourAge>\n" +
"- **Website:** <YourWebsite>";
}
if (args[0].equals("time")) { // !info time
SimpleDateFormat format = new SimpleDateFormat("HH:mm");
Date currentDate = new Date(System.currentTimeMillis());
return "It's" + format.format(currentDate);
}
}
return "Unknown argument!";
}
}
Depending on your library the registration looks a little bit different. I assume you know how to login with your used library so I won't explain this.
DiscordApi api; // Your api instance. Of course it should be initialized!
CommandHandler handler = new JavacordHandler(api);
handler.registerCommand(new InfoCommand());
JDA jda; // Your JDA instance. Of course it should be initialized!
CommandHandler handler = new JDA3Handler(jda);
handler.registerCommand(new InfoCommand());
IClient client; // Your client instance. Of course it should be initialized!
CommandHandler handler = new Discord4JHandler(client);
handler.registerCommand(new InfoCommand());
Note: If your executor contains more than one command you do not have to register it more than one time.