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

com.freya02.botcommands.api.commands.application.AbstractApplicationCommandManager.kt Maven / Gradle / Ivy

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

import com.freya02.botcommands.api.commands.application.context.builder.MessageCommandBuilder
import com.freya02.botcommands.api.commands.application.context.builder.UserCommandBuilder
import com.freya02.botcommands.api.commands.application.slash.builder.TopLevelSlashCommandBuilder
import com.freya02.botcommands.internal.BContextImpl
import com.freya02.botcommands.internal.commands.application.ApplicationCommandInfo
import com.freya02.botcommands.internal.commands.application.SimpleCommandMap
import kotlin.reflect.KFunction

sealed class AbstractApplicationCommandManager(private val context: BContextImpl) {
    private val commandMap: SimpleCommandMap = SimpleCommandMap.ofInfos()
    internal val applicationCommands: Map
        @JvmSynthetic get() = commandMap.map

    @JvmSynthetic
    internal abstract fun isValidScope(scope: CommandScope): Boolean

    protected abstract fun checkScope(scope: CommandScope)

    @JvmOverloads
    fun slashCommand(name: String, scope: CommandScope = CommandScope.GLOBAL_NO_DM, function: KFunction?, builder: TopLevelSlashCommandBuilder.() -> Unit) {
        checkScope(scope)

        TopLevelSlashCommandBuilder(context, name, function, scope)
            .apply(builder)
            .build()
            .also(::putNewCommand)
    }

    @JvmOverloads
    fun userCommand(name: String, scope: CommandScope = CommandScope.GLOBAL_NO_DM, function: KFunction, builder: UserCommandBuilder.() -> Unit) {
        checkScope(scope)

        UserCommandBuilder(context, name, function, scope)
            .apply(builder)
            .build()
            .also(::putNewCommand)
    }

    @JvmOverloads
    fun messageCommand(name: String, scope: CommandScope = CommandScope.GLOBAL_NO_DM, function: KFunction, builder: MessageCommandBuilder.() -> Unit) {
        checkScope(scope)

        MessageCommandBuilder(context, name, function, scope)
            .apply(builder)
            .build()
            .also(::putNewCommand)
    }

    private fun putNewCommand(newInfo: ApplicationCommandInfo) {
        commandMap.putNewCommand(newInfo)
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy