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

io.github.freya022.botcommands.internal.commands.application.CommandMap.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.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
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy