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

com.freya02.botcommands.api.commands.application.slash.builder.TopLevelSlashCommandBuilder.kt Maven / Gradle / Ivy

package com.freya02.botcommands.api.commands.application.slash.builder

import com.freya02.botcommands.api.commands.application.CommandScope
import com.freya02.botcommands.api.commands.application.slash.builder.mixins.ITopLevelApplicationCommandBuilder
import com.freya02.botcommands.api.commands.application.slash.builder.mixins.ITopLevelSlashCommandBuilder
import com.freya02.botcommands.api.commands.application.slash.builder.mixins.TopLevelSlashCommandBuilderMixin
import com.freya02.botcommands.internal.BContextImpl
import com.freya02.botcommands.internal.commands.application.SimpleCommandMap
import com.freya02.botcommands.internal.commands.application.slash.SlashUtils.isFakeSlashFunction
import com.freya02.botcommands.internal.commands.application.slash.TopLevelSlashCommandInfo
import com.freya02.botcommands.internal.commands.mixins.INamedCommand
import com.freya02.botcommands.internal.throwUser
import kotlin.reflect.KFunction

class TopLevelSlashCommandBuilder internal constructor(
    context: BContextImpl,
    name: String,
    function: KFunction?,
    scope: CommandScope
) : SlashCommandBuilder(context, name, function), ITopLevelSlashCommandBuilder by TopLevelSlashCommandBuilderMixin(scope) {
    override val topLevelBuilder: ITopLevelApplicationCommandBuilder = this
    override val parentInstance: INamedCommand? = null

    @get:JvmSynthetic
    internal val subcommands: SimpleCommandMap = SimpleCommandMap.ofBuilders()
    @get:JvmSynthetic
    internal val subcommandGroups: SimpleCommandMap = SimpleCommandMap(null)

    override val allowOptions: Boolean
        get() = subcommands.isEmpty() && subcommandGroups.isEmpty()
    override val allowSubcommands: Boolean
        get() = optionAggregateBuilders.isEmpty()
    override val allowSubcommandGroups: Boolean
        get() = optionAggregateBuilders.isEmpty()

    @JvmOverloads
    fun subcommand(name: String, function: KFunction, block: SlashSubcommandBuilder.() -> Unit = {}) {
        if (isFunctionSet()) throwUser("Cannot add subcommands as this already contains a function")
        if (!allowSubcommands) throwUser("Cannot add subcommands as this already contains options")

        SlashSubcommandBuilder(context, name, function, this, this).apply(block).also(subcommands::putNewCommand)
    }

    fun subcommandGroup(name: String, block: SlashSubcommandGroupBuilder.() -> Unit) {
        if (isFunctionSet()) throwUser("Cannot add subcommand groups as this already contains a function")
        if (!allowSubcommandGroups) throwUser("Cannot add subcommand groups as this already contains options")

        SlashSubcommandGroupBuilder(context, name, this).apply(block).also(subcommandGroups::putNewCommand)
    }

    internal fun build(): TopLevelSlashCommandInfo {
        return TopLevelSlashCommandInfo(context, this)
    }

    private fun isFunctionSet() = !function.isFakeSlashFunction()
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy