All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.freya022.botcommands.api.commands.text.provider.TextCommandManager.kt Maven / Gradle / Ivy

package io.github.freya022.botcommands.api.commands.text.provider

import io.github.freya022.botcommands.api.commands.annotations.Command
import io.github.freya022.botcommands.api.commands.text.BaseCommandEvent
import io.github.freya022.botcommands.api.commands.text.CommandEvent
import io.github.freya022.botcommands.api.commands.text.IHelpCommand
import io.github.freya022.botcommands.api.commands.text.annotations.JDATextCommandVariation
import io.github.freya022.botcommands.api.commands.text.builder.TopLevelTextCommandBuilder
import io.github.freya022.botcommands.api.core.BContext
import io.github.freya022.botcommands.api.core.setCallerAsDeclarationSite
import io.github.freya022.botcommands.internal.commands.application.NamedCommandMap
import io.github.freya022.botcommands.internal.commands.text.TopLevelTextCommandInfoImpl
import io.github.freya022.botcommands.internal.commands.text.builder.TopLevelTextCommandBuilderImpl

class TextCommandManager internal constructor(private val context: BContext) {
    internal val textCommands: NamedCommandMap = NamedCommandMap()

    /**
     * Declares the supplied function as a text command.
     *
     * ### Text command variations
     * A given text command path (such as `ban temp`) is composed of at least one variation;
     * Each variation has different parameters, and will display separately in the built-in help content.
     *
     * Each variation runs based off its declaration order,
     * the first variation that has a full match against the user input gets executed.
     *
     * If no regex-based variation (using a [BaseCommandEvent]) matches,
     * the fallback variation is executed (if a variation using [CommandEvent] exists).
     *
     * If no variation matches and there is no fallback,
     * then the [help content][IHelpCommand.onInvalidCommand] is invoked for the command.
     *
     * ### Requirements
     *  - The declaring class must be annotated with [@Command][Command].
     *  - First parameter must be [BaseCommandEvent], or, [CommandEvent] for fallback commands/manual token consumption
     *
     *  @see JDATextCommandVariation @JDATextCommandVariation
     */
    fun textCommand(name: String, builder: TopLevelTextCommandBuilder.() -> Unit) {
        TopLevelTextCommandBuilderImpl(context, name)
            .setCallerAsDeclarationSite()
            .apply(builder)
            .build()
            .also(textCommands::putNewCommand)
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy