io.github.freya022.botcommands.internal.commands.application.CommandMap.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 io.github.freya022.botcommands.api.commands.CommandPath
import io.github.freya022.botcommands.internal.utils.shortSignatureNoSrc
import io.github.freya022.botcommands.internal.utils.throwUser
import java.util.*
interface CommandMap : Map
class MutableCommandMap(
private val map: MutableMap = Collections.synchronizedMap(
mutableMapOf()
)
) : CommandMap, MutableMap by map {
override fun put(key: CommandPath, value: T): T? {
//Check if commands with the same name as their entire path are present
// For example, trying to insert /tag create while /tag already exists
for ((commandPath, mapInfo) in this) {
if (key.fullPath == commandPath.name) {
throwUser(
"Tried to add a command with path '%s' (at %s) but a equal/longer path already exists: '%s' (at %s)".format(
key, value.function.shortSignatureNoSrc,
commandPath, mapInfo.function.shortSignatureNoSrc
)
)
}
if (commandPath.fullPath == key.name) {
throwUser(
"Tried to add a command with path '%s' (at %s) but a top level command already exists: '%s' (at %s)".format(
key, value.function.shortSignatureNoSrc,
commandPath, mapInfo.function.shortSignatureNoSrc
)
)
}
}
val oldInfo = map.put(key, value)
if (oldInfo != null) {
throwUser(
"Tried to add a command with path '%s' (at %s) but an equal path already exists: '%s' (at %s)".format(
key,
value.function.shortSignatureNoSrc,
oldInfo.path,
oldInfo.function.shortSignatureNoSrc
)
)
}
return null //oldInfo is always null
}
}