io.github.freya022.botcommands.internal.commands.application.ApplicationCommandsContextImpl.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.internal.commands.application
import gnu.trove.map.hash.TLongObjectHashMap
import io.github.freya022.botcommands.api.commands.CommandPath
import io.github.freya022.botcommands.api.commands.application.ApplicationCommandMap
import io.github.freya022.botcommands.api.commands.application.ApplicationCommandsContext
import io.github.freya022.botcommands.api.commands.application.CommandUpdateResult
import io.github.freya022.botcommands.api.core.service.getService
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 io.github.freya022.botcommands.internal.core.BContextImpl
import kotlinx.coroutines.async
import kotlinx.coroutines.future.asCompletableFuture
import net.dv8tion.jda.api.entities.Guild
import java.util.concurrent.CompletableFuture
import java.util.concurrent.locks.ReentrantLock
import kotlin.concurrent.withLock
internal class ApplicationCommandsContextImpl internal constructor(private val context: BContextImpl) : ApplicationCommandsContext {
private val writeLock = ReentrantLock()
private val liveApplicationCommandInfoMap = TLongObjectHashMap()
override fun findLiveSlashCommand(guild: Guild?, path: CommandPath): SlashCommandInfo? =
getLiveApplicationCommandsMap(guild)?.findSlashCommand(path)
?: getLiveApplicationCommandsMap(null)?.findSlashCommand(path)
override fun findLiveUserCommand(guild: Guild?, name: String): UserCommandInfo? =
getLiveApplicationCommandsMap(guild)?.findUserCommand(name)
?: getLiveApplicationCommandsMap(null)?.findUserCommand(name)
override fun findLiveMessageCommand(guild: Guild?, name: String): MessageCommandInfo? =
getLiveApplicationCommandsMap(guild)?.findMessageCommand(name)
?: getLiveApplicationCommandsMap(null)?.findMessageCommand(name)
override fun getLiveApplicationCommandsMap(guild: Guild?): ApplicationCommandMap? {
return liveApplicationCommandInfoMap[getGuildKey(guild)]
}
override fun getEffectiveApplicationCommandsMap(guild: Guild?): ApplicationCommandMap = when (guild) {
null -> getLiveApplicationCommandsMap(guild = null) ?: MutableApplicationCommandMap.EMPTY_MAP
else -> (getLiveApplicationCommandsMap(guild = null) ?: MutableApplicationCommandMap.EMPTY_MAP) +
(getLiveApplicationCommandsMap(guild = guild) ?: MutableApplicationCommandMap.EMPTY_MAP)
}
fun putLiveApplicationCommandsMap(guild: Guild?, map: ApplicationCommandMap): Unit = writeLock.withLock {
liveApplicationCommandInfoMap.put(getGuildKey(guild), map.toUnmodifiableMap())
}
override fun updateGlobalApplicationCommands(force: Boolean): CompletableFuture {
return context.coroutineScopesConfig.commandUpdateScope.async {
context.getService().updateGlobalCommands(force)
}.asCompletableFuture()
}
override fun updateGuildApplicationCommands(guild: Guild, force: Boolean): CompletableFuture {
return context.coroutineScopesConfig.commandUpdateScope.async {
context.getService().updateGuildCommands(guild, force)
}.asCompletableFuture()
}
private fun getGuildKey(guild: Guild?): Long {
return guild?.idLong ?: 0
}
}