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

io.github.freya022.botcommands.internal.commands.text.TextCommandsContextImpl.kt Maven / Gradle / Ivy

Go to download

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

There is a newer version: 3.0.0-alpha.22
Show newest version
package io.github.freya022.botcommands.internal.commands.text

import io.github.freya022.botcommands.api.commands.text.TextCommandsContext
import io.github.freya022.botcommands.internal.utils.throwUser

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

    override val rootCommands: Collection
        get() = textCommandMap.values.toList()

    internal 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")
            }
        }
    }

    override 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
            }
    }

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




© 2015 - 2025 Weber Informatics LLC | Privacy Policy