Skip to content

v4.0.0-beta.2 | Static Interactions and QoL changes

Latest
Compare
Choose a tag to compare
@Kaktushose Kaktushose released this 19 Apr 10:00

Overview

This release introduces many small QoL features as well as lots of bug fixes.

Static Interactions

You can now disable the request-scoped instances by annotating the interaction class with StaticInstance. This will create one instance of the class that will be used until jda-commands shuts down.

In the following example every global counter command will use the same counter variable. Thus, if you have 2 commands active, pressing the button on the first command will also increase the count for the second command.

@Interaction
@StaticInstance
public class StaticTest {

    private int counter;

    @SlashCommand("global counter")
    public void onCommand(CommandEvent event) {
        event.withButtons("onButton").reply("Global counter");
    }

    @Button("click me")
    public void onButton(ComponentEvent event) {
        counter++;
        event.reply("counter: " + counter);
    }
}

MessageBuilder Components

In the past components were only usable in combination with commands. However, sometimes you want to add a component to a MessageBuilder.

From now on you can use JDACommands#getButton(String id) or respectively JDACommands#getSelectMenu(String id) to access a component outside of commands. The component can be defined anywhere inside the project. The id consists of the simple class name and the method name, e.g. VoteButtons.onUpvote.

Example:

@Interaction
@StaticInstance
public class ComponentAPITest extends ListenerAdapter {

    @Override
    public void onMessageReceived(@NotNull MessageReceivedEvent event) {
        if (!event.getMessage().getContentDisplay().startsWith("!greet")) {
            return;
        }

        var builder = new MessageCreateBuilder().setContent("Hello").addActionRow(jdaCommands.getButton("ComponentAPITest.onButton"));

        event.getChannel().sendMessage(builder.build()).queue();
    }

    @Button("Greet me!")
    public void onButton(ComponentEvent event) {
        event.editReply(false).reply("Hello %s", event.getUser());
    }
}

Please note that the onButton method doesn't need to be defined in the same class it is referred in. This was only done here to keep the example short and sweet. Also be aware that the keepComponents feature doesn't work in this case, because it requires request-scoped instances. Thus you'd need to reattach the button if you set editReply to true.

1) New Features

  • added the skipIndexing boolean to @Produces annotation. If set to true jda-commands will ignore this producer method at indexing. This is useful if you wish to register your dependency providers manually by calling DependencyInjector#registerProvider(Object).

  • Implemented a simple KeyValueStore that can be accessed from every event class by calling kv()

2) Changes

  • jda-commands will now ignore any incoming component or modal events that weren't created by jda-commands. However, foreign commands will still create a error message.

  • when starting jda-commands you can now provide your own DependencyInjector implementation

3) Bug Fixes

  • fixed that Constraints didn't get applied to parameters
  • fixed that using an EmbedDTO to reply overwrote components
  • fixed incorrect parameter name sanitation
  • fixed that root commands didn't adopt the configuration of their subcommands
  • fixed a bug where Modals sometimes didn't get executed

4) Internal changes

  • the SlashCommandUpdater now uses the GuildReadyEvent for initial command setup
  • made the DependencyInjector class an interface and added a default implementation
  • changed the naming scheme of internal ids for defintions and runtimes

You can find the complete changelog here.

Also, checkout the Wiki or the JavaDoc.

Maven

<repository>
    <id>jitpack.io</id>
    <url>https://jitpack.io</url>
</repository>
<dependency>
    <groupId>com.github.Kaktushose</groupId>
    <artifactId>jda-commands</artifactId>
    <version>v4.0.0-beta.2</version>
</dependency>

Gradle

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}
dependencies {
    implementation 'com.github.Kaktushose:jda-commands:v4.0.0-beta.2'
}