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

io.github.freya022.botcommands.internal.commands.application.SimpleCommandMap.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.18
Show newest version
package io.github.freya022.botcommands.internal.commands.application

import io.github.freya022.botcommands.api.commands.builder.IBuilderFunctionHolder
import io.github.freya022.botcommands.internal.IExecutableInteractionInfo
import io.github.freya022.botcommands.internal.commands.mixins.INamedCommand
import io.github.freya022.botcommands.internal.utils.shortSignature
import io.github.freya022.botcommands.internal.utils.throwUser
import java.util.*
import kotlin.reflect.KFunction

internal class SimpleCommandMap(private val functionSupplier: ((T) -> KFunction<*>)?) {
    private val mutableMap: MutableMap = hashMapOf()
    val map: Map
        get() = Collections.unmodifiableMap(mutableMap)

    fun putNewCommand(newCommand: T) {
        mutableMap.putIfAbsent(newCommand.name, newCommand)?.let { oldCommand ->
            throwUser(
                """
                Command '${newCommand.path.fullPath}' is already defined
                Existing command: ${functionSupplier?.invoke(oldCommand)?.shortSignature}
                Current command: ${functionSupplier?.invoke(newCommand)?.shortSignature}
                """.trimIndent()
            )
        }
    }

    fun isEmpty(): Boolean = mutableMap.isEmpty()

    companion object {
        fun  ofBuilders(): SimpleCommandMap where T : INamedCommand,
                                                        T : IBuilderFunctionHolder<*> {
            return SimpleCommandMap(IBuilderFunctionHolder<*>::function)
        }

        fun  ofInfos(): SimpleCommandMap where T : INamedCommand,
                                                     T : IExecutableInteractionInfo {
            return SimpleCommandMap(IExecutableInteractionInfo::function)
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy