/BotCommands

A JDA command framework (& much more) which helps to develop your bots faster

Primary LanguageKotlinMozilla Public License 2.0MPL-2.0

BotCommands logo

BotCommands version JDA version Snapshots

Discord invite Wiki home Documentation

BotCommands

A Kotlin-first (and Java) framework that makes creating Discord bots a piece of cake, using the JDA library.

Features

The framework being built around events and dependency injection, your project can take advantage of that and avoid passing objects around, while also easily being able to use services provided by the framework.

Commands

  • Automatic registration of commands, resolvers, services, etc... with full dependency injection
  • Can be used with annotations or with code (in Kotlin)

Application commands

  • Slash commands with automatic & customizable argument processing
    • Supports choices, min/max values/length, channel types and autocomplete
    • Options can be grouped into objects
  • Context menu commands (User / Message)
  • Automatic, smart application commands registration
Example
@Command
class SlashBan : ApplicationCommand() {
    @JDASlashCommand(name = "ban", description = "Bans an user")
    suspend fun onSlashBan(
        event: GuildSlashEvent,
        @SlashOption(description = "The user to ban") user: User,
        @SlashOption(description = "Timeframe of messages to delete") timeframe: Long,
        // Use choices that come from the TimeUnit resolver
        @SlashOption(description = "Unit of the timeframe", usePredefinedChoices = true) unit: TimeUnit, // A resolver is used here
        @SlashOption(description = "Why the user gets banned") reason: String = "No reason supplied" // Optional
    ) {
        // ...
        event.reply_("${user.asMention} has been banned for '$reason'", ephemeral = true)
          .deleteDelayed(5.seconds)
          .await()
    }
}

Slash ban example

Text commands

  • Supports prefix and mentions
  • With two parsing modes:
    1. Each parameter is an argument, works the same as slash commands
    2. Manual argument consumption
Example
@Command
class TextBan : TextCommand() {
    @JDATextCommandVariation(path = ["ban"], description = "Bans the mentioned user")
    suspend fun onTextBan(
        event: BaseCommandEvent,
        @TextOption user: User,
        @TextOption(example = "2") timeframe: Long,
        @TextOption unit: TimeUnit, // A resolver is used here
        @TextOption(example = "Get banned") reason: String = "No reason supplied" // Optional
    ) {
        // ...
        event.reply("${user.asMention} has been banned")
            .deleteDelayed(5.seconds)
            .await()
    }
}

Can then be used as @Bot ban @freya02 1 days A totally valid reason

Here's how the help content would look with a subcommand and a few more variations:

Help content example

Components and modals

  • Unlimited data storage for components, with persistent and ephemeral storage
  • Both modals and persistent components have a way to pass data

Event handlers

  • Custom (annotated) event handlers, with priorities and async

Localization

  • Entirely localizable, from the command declaration to the bot responses

Dependency injection

  • Loads everything and passes objects automatically
  • Can create custom conditions to disable services/commands at startup
  • Can be replaced with Spring IoC

Utilities

  • A PostgreSQL (and H2) database abstraction, with logged queries
  • An event waiter with (multiple) preconditions, timeouts and consumers for every completion state
  • Message parsers (tokenizers, see RichTextParser) and emoji resolvers (turning :joy: into 😂)
  • Paginators and menus of different types (using components!)

And way more features!

Getting Started

You are strongly recommended to have some experience with Kotlin (or Java), OOP, JDA and Dependency Injection basics before you start using this library.

Prerequisites

  • An OpenJDK 17+ installation
  • A competent IDE (I recommend IntelliJ IDEA, you can't go wrong with it in Java & Kotlin, + Live Templates)
  • (Only Java) Enable method parameters names, please refer to the wiki page
  • (Recommended) Use HotswapAgent in development, to avoid restarting too often
  • (Recommended) Use stacktrace-decoroutinator, to get clearer stack traces in suspending code

Head over to the wiki to get started, you can also check out the examples.

Installation

BotCommands on maven central

Maven

<dependencies>
  <dependency>
    <groupId>io.github.freya022</groupId>
    <artifactId>BotCommands</artifactId>
    <version>VERSION</version>
  </dependency>
</dependencies>

Gradle

repositories {
    mavenCentral()
}

dependencies {
    implementation 'io.github.freya022:BotCommands:VERSION'
}

Alternatively, you can use Jitpack to use snapshot versions, you can refer to the JDA wiki for more information.

Sample usage

Here is how you would create a slash command that sends a message in a specified channel.

Kotlin
@Command
@RequiresComponents // Disables the command if components are not enabled
class SlashSay(private val buttons: Buttons) : ApplicationCommand() {
    @JDASlashCommand(name = "say", description = "Sends a message in a channel")
    suspend fun onSlashSay(
        event: GuildSlashEvent,
        @SlashOption(description = "Channel to send the message in") channel: TextChannel,
        @SlashOption(description = "What to say") content: String
    ) {
        event.reply_("Done!", ephemeral = true)
            .deleteDelayed(event.hook, 5.seconds)
            .queue()
        channel.sendMessage(content)
            .addActionRow(buttons.danger(EmojiUtils.resolveJDAEmoji("wastebasket")).ephemeral {
                bindTo { buttonEvent ->
                    buttonEvent.deferEdit().queue()
                    buttonEvent.hook.deleteOriginal().await()
                }
            })
            .await()
    }
}
Kotlin (DSL)
@Command
@RequiresComponents // Disables the command if components are not enabled
class SlashSay(private val buttons: Buttons) : GlobalApplicationCommandProvider {
    suspend fun onSlashSay(event: GuildSlashEvent, channel: TextChannel, content: String) {
        event.reply_("Done!", ephemeral = true)
            .deleteDelayed(event.hook, 5.seconds)
            .queue()
        channel.sendMessage(content)
            .addActionRow(buttons.danger(EmojiUtils.resolveJDAEmoji("wastebasket")).ephemeral {
                bindTo { buttonEvent ->
                    buttonEvent.deferEdit().queue()
                    buttonEvent.hook.deleteOriginal().await()
                }
            })
            .await()
    }

    override fun declareGlobalApplicationCommands(manager: GlobalApplicationCommandManager) {
        manager.slashCommand("say", function = ::onSlashSay) {
            description = "Sends a message in a channel"

            option("channel") {
                description = "Channel to send the message in"
            }

            option("content") {
                description = "What to say"
            }
        }
    }
}
Java
@Command
@RequiresComponents // Disables the command if components are not enabled
public class SlashSay extends ApplicationCommand {
    private final Buttons buttons;
  
    public SlashSay(Buttons buttons) {
        this.buttons = buttons;
    }

    @JDASlashCommand(name = "say", description = "Sends a message in a channel")
    public void onSlashSay(
            GuildSlashEvent event,
            @SlashOption(description = "Channel to send the message in") TextChannel channel,
            @SlashOption(description = "What to say") String content
    ) {
        event.reply("Done!")
                .setEphemeral(true)
                .delay(Duration.ofSeconds(5))
                .flatMap(InteractionHook::deleteOriginal)
                .queue();

        final Button deleteButton = buttons.danger(EmojiUtils.resolveJDAEmoji("wastebasket")).ephemeral()
                .bindTo(buttonEvent -> {
                    buttonEvent.deferEdit().queue();
                    buttonEvent.getHook().deleteOriginal().queue();
                })
                .build();
        channel.sendMessage(content)
                .addActionRow(deleteButton)
                .queue();
    }
}

Live templates

IntelliJ IDEA users can use live templates provided in this zip file, helping you make commands and other handlers with predefined templates, for both Kotlin and Java, keeping a consistent naming scheme and acting as a cheatsheet.

For example, if you type slashCommand in your class, this will generate a slash command and guide you through the declaration.

A list of live template can be found in Settings > Editor > Live Templates, in the BotCommands 3.X - [Language] group.

For an installation guide, you can follow this guide from JetBrains.

Support

Don't hesitate to join the support server if you have any question!

Contributing

If you want to contribute, make sure to base your branch on 3.X, and create your PR from it.

It would be appreciated to focus on improving the documentation, such as the wiki, the library documentation, or by creating examples.
Maintainers will focus on bug reports and feature requests, which you can create issues for.

Read the contributing guide for more details.