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

com.freya02.botcommands.internal.commands.prefixed.TextCommandsContextImpl.kt Maven / Gradle / Ivy

package com.freya02.botcommands.internal.commands.prefixed

import com.freya02.botcommands.api.commands.prefixed.TextCommandsContext
import com.freya02.botcommands.internal.throwUser

internal class TextCommandsContextImpl internal constructor() : TextCommandsContext {
    private val textCommandMap: MutableMap = hashMapOf()

    fun addTextCommand(commandInfo: TopLevelTextCommandInfo) {
        (commandInfo.aliases + commandInfo.name).forEach { name ->
            textCommandMap.put(name, commandInfo)?.let {
                throwUser(commandInfo.variations.first().function, "Text command with path ${commandInfo.path} already exists")
            }
        }
    }

    fun findTextCommand(words: List): TextCommandInfo? {
        val initial: TextCommandInfo = textCommandMap[words.first()] ?: return null
        return words
            .drop(1) //First word is already resolved
            .fold(initial) { info, subname ->
                info.subcommands[subname] ?: return null
            }
    }

    fun findTextSubcommands(words: List): Collection {
        val command = findTextCommand(words) ?: return emptyList()
        return command.subcommands.values
    }

    override fun getRootCommands(): Collection {
        return textCommandMap.values
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy