Skip to content

2.3.0-alpha.1

Compare
Choose a tag to compare
@Kaktushose Kaktushose released this 01 Jul 15:01

Overview

This release updates jda-commands to JDA 5 and brings in first implementations for interactions. No breaking changes were made, this is completely backwards compatible to v2.2.0!

Please note that this is the first release and will still contain some bugs

Slash Commands

This release adds support for slash commands. They will work out of the box, which means that you don't have to change your command classes to make slash commands work.

Enabling Slash Commands

In order to get started with slash commands, you need to build jda-commands a little bit different.

JDACommands.slash(jda, Main.class).startGlobal();

This will register all CommandDefinitions as global commands. Registering guild commands looks similar:

JDACommands.slash(jda, Main.class).guilds(0123456789L).startGuild();

CommandRegistrationPolicy

By calling registrationPolicy(CommandRegistrationPolicy) you can define the operation mode of your bot. You can choose between text, slash, text and slash or migrating where the bot will inform the user that text commands aren't available anymore.

Replying

The bot will always acknowledge incoming interaction events first to prevent time outs when your response building takes longer.

The reply methods of the CommandEvent have been integrated to work with slash commands. So again, no changes needed. To send ephemeral replies there are two ways:

  • Pass it to the reply method:

    event.reply("test", true);
  • Set it as default

    @Command(value = "greet", ephemeral = true)
    public void greet(CommandEvent event) {
        event.reply("Hello");
    }

Parameters

Slash command parameters need a name and a description when being registered. To accomplish that a new annotation has been introduced.

@Command("greet")
public void greet(CommandEvent event, @Param(name = "member", value = "The member to greet") Member member) {
    event.reply("Hello %s", member.getAsMention());
}

The name can be omitted when you compile with the -parameters flag. This will make the method parameter name readable for jda-commands at runtime.

If you also omit the description "empty description" will be used by default.

Choices

Slash command parameters can also have a limited set of choices. This was also achieved by introducing a new annotation.

@Command("food")
public void chooseFood(CommandEvent event, @Choices({"Apples", "Burger", "Pizza"}) String food) {
    event.reply("You chose %s", food);
}

Buttons

Buttons are one of the new components Discord has introduced. They are arranged in so called ActionRows and can either be enabled or disabled.

Defining Buttons

Buttons are defined the same way as commands are:

@Button(label = "Click me!", style = ButtonStyle.DANGER)
public void onButton(ButtonEvent event) {
  event.reply("You clicked me!");
}

Adding Buttons to Messages

Buttons have ids, by default this id is equal to the method name. However you can use your own id by passing it to the @Button annotation.

Buttons can be added to messages by either calling event#with or event#withButtons with the id of the button you want to add. We will discuss the difference in a second. However, each call to a with method will create a new ActionRow.

Have a look at this first simple example:

@Button(label = "Click me!", style = ButtonStyle.DANGER)
public void onButton(ButtonEvent event) {
  event.reply("You clicked me!");
}

@Command("greet")
public void greet(CommandEvent event) {
    event.withButtons("onButton").reply("Hello!");
}

Sometimes we don't want a button to be active all the time. event#withButtons will always enable buttons but by calling event#with you can change this behavior.

@Command("leaderboard")
public void onLeaderboard(CommandEvent event) {
    event.with(Buttons.disabled("onLeft"), Buttons.enabled("onRight")).reply("Page 1");
}

Replying

You can reply to buttons exactly like you reply to commands. Alternatively you can edit the original message (the message the button is attached to) by calling event#edit.

Additional methods are event#clearComponents and event#editComponents.

1) New Features

  • Slash Command and Buttons support

  • @Param and @Choices annotation for parameters

  • compile with -parameters flag and jda-commands will automatically set the parameter name

  • the prefix in help messages will either be the actual prefix or / depending on the execution context

  • Automatically generated command usage in the format prefix command <args> (optional args)

  • new class JDAContext as a bridge between JDA and ShardManager

  • New type adapters for all channel types

  • StateSection as a simple key value storage

2) Changes

  • commands can no longer have empty labels

  • JDACommands#getJda and CommandDispatcher#getJda have been deprecated. Use #getJDAContext instead

  • removed JDACommands#getRouter and JDACommands#setRouter

  • removed CommandDispatcher#getRouter and CommandDispatcher#setRouter

3) Bug Fixes

  • fix Levenshtein distance calculation for multiple labels #52

4) Internal changes

  • added isSlash and OptionMapping to CommandContext

  • new class GenericEvent as a bridge between text and interaction events

  • new parser implementations for interactions

  • new data structure classes CommandTree and TreeNode to generate valid slash command paths

  • reply methods have been extracted to a ReplyAction and ReplyCallback interface with their respective implementations for text and interactions commands.

  • added toCommandData and toSubCommandData methods to CommandDefinition

  • new reflection class ButtonDefinition


You can find the complete changelog here.

JavaDoc for the new classes is currently not available but the wiki will be updated soon:tm:

Maven

<repository>
    <id>jitpack.io</id>
    <url>https://jitpack.io</url>
</repository>
<dependency>
    <groupId>com.github.Kaktushose</groupId>
    <artifactId>jda-commands</artifactId>
    <version>v2.3.0-alpha.1</version>
</dependency>

Gradle

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}
dependencies {
    implementation 'com.github.Kaktushose:jda-commands:v2.3.0-alpha.1'
}