Skip to content
Kieran edited this page May 18, 2021 · 4 revisions

Your First Module

Creating a command module in Oktane is as simple as creating a class;

public class MyFirstModule extends ModuleBase<CommandContext> {
}

And adding our first command is just adding a method that returns CommandResult with the Aliases annotation;

public class MyFirstModule extends ModuleBase<CommandContext> {
    @Aliases("ping")
    public CommandResult pong() {
        return message("pong");
    }
}

ModuleBase defines a handful of useful method for returning simple results, e.g. message to represent a simple text response, and nop to convey no-operation. For integration with Reactor a command can also return Mono<CommandResult>;

public class MyFirstModule extends ModuleBase<CommandContext> {
    @Aliases("ping")
    public Mono<CommandResult> pong() {
        return PingService.get()
            .map(Response::getContent)
            .map(this::message);
    }
}

Configuring The CommandHandler

The CommandHandler serves as a layer for interacting with your commands, setting up the handler is as simple as;

 var handler = CommandHandler.<CommandContext>builder()
    .withModules(MyFirstModule.class, CommandContext.class)
    .build();

The withModules method will find all of the command modules within a package and add them to the handler, otherwise the withModule method can be used to add a specific module.

Executing A Command

Once you have your handler setup executing a command is a simple call to CommandHandler#execute;

var result = handler.execute("ping", new CommandContext());