Skip to content
This repository was archived by the owner on Oct 1, 2024. It is now read-only.

Application Commands

LinusDev edited this page Sep 29, 2022 · 10 revisions

Application Commands

Application Commands are ways for users to interact with your application / bot. Discord Documentation about these are availabe here.

LApi provides two different ways to manage these Commands: Either with the Command Manager or manually using requests and event listening.

Command Manager

First make sure that you have the required dependecy in your project 'io.github.lni-dev:lapi-annotation-processor:1.0.1'. If you are using gradle you can add it as dependency like this:

dependencies {
    annotationProcessor 'io.github.lni-dev:lapi-annotation-processor:1.0.1'
}

This will help LApi to automatically recognise your commands. If you cannot add a dependecy as annotation processor or your command classes require special constructors, you can add a custom command provider, to provide your commands to the LApi CommandManager.

Creating your Command

First create a class for your command. Your class must extend from BaseCommand and must be annotated with the @Command annotation. It also must have a default constructor or no constructor at all (java will create the default constructor implicitly):

@Command
public class YourCommand extends BaseCommand {
    //...
}

Now add the following methods:

@Command
public class YourCommand extends BaseCommand {
    @Override
    public @NotNull CommandScope getScope() {
        //...
    }

    @Override
    protected @Nullable ApplicationCommandTemplate create() {
        //...
    }
}

getScope() must return the scope for your command, which can either be CommandScope.GLOBAL or CommandScope.GUILD. Global commands are enabled globally for all guilds and possibly direct messages of your bot. Guild commands can be enabled for each guild indivdually.
create() must return the template for your command. This template describes your command and can be created using a ApplicationCommandBuilder. For restrictions regarding Application Commands, you should read the discord documentation.

Next add the following listener methods:

@Command
public class Main extends BaseCommand {

    //...

    @Override
    public boolean onInteract(@NotNull InteractionCreateEvent event, @NotNull SelectedOptions options, @NotNull InteractionResponseBuilder response) {
        //code...
        return true;
    }

    @Override
    public boolean onAutocomplete(@NotNull InteractionCreateEvent event, @NotNull SelectedOptions options, @NotNull AutocompleteBuilder builder) {
        //code...
        return true;
    }

    @Override
    public void onError(Throwable error) {
        //manage / print error
    }
}

onInteract(...) is called whenever a user executed your command. It has the following arguments:

  • InteractionCreateEvent is the event corresponding to this interaction.
  • SelectedOptions contains the options the user selected when they executed the command.
  • InteractionResponseBuilder can be used to build a response for the user. If your method returns true this response will automatically be sent.

onAutocomplete(...) is called whenever a user is in the progress of filling your commands options, which have autocomplete enabled. It has the following arguments:

  • InteractionCreateEvent is the event corresponding to this interaction.
  • SelectedOptions contains the options the user selected and possibly already filled. SelectedOptions.getFocused() returns the option, the user is currently editing.
  • AutocompleteBuilder can be used to build an autocomplete response for the user. If your method returns 'true' this reponse will automatically be sent.

onError(Throwable error) is called when any problem with your command occurs. This method should not throw anything.

Enabling guild commands for specific guilds

Adding an already existing Command

Custom Command Provider

Manually