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

com.freya02.botcommands.internal.commands.application.MutableApplicationCommandMap.kt Maven / Gradle / Ivy

package com.freya02.botcommands.internal.commands.application

import com.freya02.botcommands.api.commands.CommandPath
import com.freya02.botcommands.api.commands.application.ApplicationCommandMap
import com.freya02.botcommands.internal.commands.application.context.message.MessageCommandInfo
import com.freya02.botcommands.internal.commands.application.context.user.UserCommandInfo
import com.freya02.botcommands.internal.commands.application.slash.SlashCommandInfo
import com.freya02.botcommands.internal.commands.application.slash.TopLevelSlashCommandInfo
import com.freya02.botcommands.internal.enumMapOf
import net.dv8tion.jda.api.interactions.commands.Command
import java.util.*
import java.util.function.Function
import net.dv8tion.jda.api.interactions.commands.Command.Type as CommandType

class MutableApplicationCommandMap(
    private val rawTypeMap: Map> = Collections.synchronizedMap(enumMapOf())
) : ApplicationCommandMap() {
    override fun getRawTypeMap() = rawTypeMap

    override fun getSlashCommands(): MutableCommandMap {
        return getTypeMap(CommandType.SLASH)
    }

    override fun getUserCommands(): MutableCommandMap {
        return getTypeMap(CommandType.USER)
    }

    override fun getMessageCommands(): MutableCommandMap {
        return getTypeMap(CommandType.MESSAGE)
    }

    fun  computeIfAbsent(
        type: CommandType,
        path: CommandPath,
        mappingFunction: Function
    ): T = getTypeMap(type).computeIfAbsent(path, mappingFunction)

    fun  put(type: CommandType, path: CommandPath, value: T): T? = getTypeMap(type).put(path, value)

    override operator fun plus(map: ApplicationCommandMap): MutableApplicationCommandMap {
        val newMap: MutableMap> = enumMapOf>()
        Command.Type.values().forEach { commandType ->
            val commandMap = newMap.getOrPut(commandType) { MutableCommandMap() }

            listOf(this, map).forEach { sourceMap ->
                sourceMap.getTypeMap(commandType).forEach { (path, info) ->
                    commandMap[path] = info
                }
            }
        }

        return MutableApplicationCommandMap(newMap)
    }

    override fun  getTypeMap(type: CommandType): MutableCommandMap {
        return super.getTypeMap(type) as MutableCommandMap
    }

    companion object {
        fun fromCommandList(guildApplicationCommands: Collection) = MutableApplicationCommandMap().also { map ->
            for (info in guildApplicationCommands) {
                val type = when (info) {
                    is MessageCommandInfo -> CommandType.MESSAGE
                    is UserCommandInfo -> CommandType.USER
                    is TopLevelSlashCommandInfo -> CommandType.SLASH
                    else -> throw IllegalArgumentException("Unknown application command info type: " + info.javaClass.name)
                }

                val typeMap = map.getTypeMap(type)
                when (info) {
                    is UserCommandInfo, is MessageCommandInfo -> typeMap[info.path] = info
                    is TopLevelSlashCommandInfo -> {
                        if (info.isTopLevelCommandOnly()) {
                            typeMap[info.path] = info
                        } else {
                            info.subcommandGroups.values.forEach { groupInfo ->
                                groupInfo.subcommands.values.forEach {
                                    typeMap[it.path] = it
                                }
                            }

                            info.subcommands.values.forEach {
                                typeMap[it.path] = it
                            }
                        }
                    }
                    else -> throw IllegalArgumentException("Unknown application command info type: " + info.javaClass.name)
                }
            }
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy