-
Notifications
You must be signed in to change notification settings - Fork 5
Creating Commands
Hunter Wignall edited this page Sep 13, 2020
·
1 revision
- Create a new class, i.e
MyCommand - Extend
RootCommand:
class MyCommand : RootCommand()- Override
nameanddescriptionat the bare minimum:
IntelliJ: For a full list of open members, click the class name and pressCtrl + O
class MyCommand : RootCommand() {
override val name = "mycommand"
override val description = "Perform my command"
}- To add functionality to this command, override
execute(message: Message, arguments: Arguments): Response:
class MyCommand : RootCommand() {
override val name = "mycommand"
override val description = "Perform my command"
//See net.sourcebot.api.response package for a list of predefined Responses
override fun execute(message: Message, arguments: Arguments) = InfoResponse("MyCommand", "You just ran my command!")
}- Command registration is done inside the main class, in
onEnable:
class MyModule : SourceModule() {
override fun onEnable() {
registerCommands(
MyCommand()
)
}
}- Create a class that extends
Commandfor your subcommand, overriding the values:
Note: We use inner classes for encapsulation purposes but it is not necessary.
class FirstCommand : RootCommand() {
override val name = "first"
override val description = "The first command"
override fun execute(message: Message, arguments: Arguments) = InfoResponse("First")
private class SecondCommand : Command() {
override val name = "second"
override val description = "The second command"
override fun execute(message: Message, arguments: Arguments) = InfoResponse("Second")
}
}- Register the subcommand inside the relevant
RootCommand:
class FirstCommand : RootCommand() {
override val name = "first"
override val description = "The first command"
override fun execute(message: Message, arguments: Arguments) = InfoResponse("First")
private class SecondCommand : Command() {
override val name = "second"
override val description = "The second command"
override fun execute(message: Message, arguments: Arguments) = InfoResponse("Second")
}
//Register subcommands in init
init {
addChildren(
SecondCommand()
)
}
}