io.github.freya022.botcommands.api.commands.application.ApplicationCommandMap.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of BotCommands Show documentation
Show all versions of BotCommands Show documentation
A Kotlin-first (and Java) framework that makes creating Discord bots a piece of cake, using the JDA library.
package io.github.freya022.botcommands.api.commands.application
import io.github.freya022.botcommands.api.commands.CommandPath
import io.github.freya022.botcommands.api.core.utils.enumMapOf
import io.github.freya022.botcommands.internal.commands.application.*
import io.github.freya022.botcommands.internal.commands.application.context.message.MessageCommandInfo
import io.github.freya022.botcommands.internal.commands.application.context.user.UserCommandInfo
import io.github.freya022.botcommands.internal.commands.application.slash.SlashCommandInfo
import net.dv8tion.jda.api.interactions.commands.Command
import org.jetbrains.annotations.UnmodifiableView
import java.util.*
abstract class ApplicationCommandMap {
val allApplicationCommands: @UnmodifiableView List
get() = Collections.unmodifiableList(Command.Type.entries.flatMap { getTypeMap(it).values })
operator fun get(type: Command.Type, path: CommandPath): ApplicationCommandInfo? {
return getTypeMap(type)[path]
}
val slashCommands: @UnmodifiableView CommandMap get() = getTypeMap(Command.Type.SLASH)
val userCommands: @UnmodifiableView CommandMap get() = getTypeMap(Command.Type.USER)
val messageCommands: @UnmodifiableView CommandMap get() = getTypeMap(Command.Type.MESSAGE)
fun findSlashCommand(path: CommandPath): SlashCommandInfo? = slashCommands[path]
fun findUserCommand(name: String): UserCommandInfo? = userCommands[CommandPath.ofName(name)]
fun findMessageCommand(name: String): MessageCommandInfo? = messageCommands[CommandPath.ofName(name)]
abstract fun getTypeMap(type: Command.Type): CommandMap
operator fun plus(liveApplicationCommandsMap: ApplicationCommandMap): ApplicationCommandMap {
val newMap: MutableMap> = enumMapOf>()
Command.Type.entries.forEach { commandType ->
val commandMap = newMap.getOrPut(commandType) { MutableCommandMap() }
listOf(this, liveApplicationCommandsMap).forEach { sourceMap ->
sourceMap.getTypeMap(commandType).forEach { (path, info) ->
commandMap[path] = info
}
}
}
return MutableApplicationCommandMap(newMap).toUnmodifiableMap()
}
}