From 1c20e0506619b1707035fbbdfa1ce43d1491a3a4 Mon Sep 17 00:00:00 2001 From: Ahmad Ansori Palembani Date: Tue, 20 May 2025 05:54:37 +0700 Subject: [PATCH] refactor: Turn CommandHolder into internal data class Since we want commands to be registered from modules --- .../io/github/null2264/tsukumogami/core/AbstractBot.kt | 4 ++-- .../github/null2264/tsukumogami/core/BotConfigurator.kt | 6 +++--- .../github/null2264/tsukumogami/core/module/BotModule.kt | 8 ++++---- .../core/module/{Command.kt => CommandHolder.kt} | 4 ++-- 4 files changed, 11 insertions(+), 11 deletions(-) rename core/src/main/kotlin/io/github/null2264/tsukumogami/core/module/{Command.kt => CommandHolder.kt} (88%) diff --git a/core/src/main/kotlin/io/github/null2264/tsukumogami/core/AbstractBot.kt b/core/src/main/kotlin/io/github/null2264/tsukumogami/core/AbstractBot.kt index 604b054..594bd09 100644 --- a/core/src/main/kotlin/io/github/null2264/tsukumogami/core/AbstractBot.kt +++ b/core/src/main/kotlin/io/github/null2264/tsukumogami/core/AbstractBot.kt @@ -11,13 +11,13 @@ import dev.kord.gateway.PrivilegedIntent import io.github.null2264.tsukumogami.core.exceptions.CommandException import io.github.null2264.tsukumogami.core.exceptions.CommandNotFound import io.github.null2264.tsukumogami.core.module.BotModule -import io.github.null2264.tsukumogami.core.module.Command +import io.github.null2264.tsukumogami.core.module.CommandHolder import kotlin.reflect.full.callSuspend import kotlinx.coroutines.runBlocking abstract class AbstractBot(configurator: BotConfigurator.() -> Unit) { - private val commands: Map + private val commands: Map private val extensions: Map private val prefixes: List private val client: Kord diff --git a/core/src/main/kotlin/io/github/null2264/tsukumogami/core/BotConfigurator.kt b/core/src/main/kotlin/io/github/null2264/tsukumogami/core/BotConfigurator.kt index de98f03..dbdfc8a 100644 --- a/core/src/main/kotlin/io/github/null2264/tsukumogami/core/BotConfigurator.kt +++ b/core/src/main/kotlin/io/github/null2264/tsukumogami/core/BotConfigurator.kt @@ -1,21 +1,21 @@ package io.github.null2264.tsukumogami.core import io.github.null2264.tsukumogami.core.module.BotModule -import io.github.null2264.tsukumogami.core.module.Command +import io.github.null2264.tsukumogami.core.module.CommandHolder import kotlin.reflect.KClass import kotlin.reflect.KFunction import kotlin.reflect.full.isSubclassOf class BotConfigurator internal constructor() { - internal val commands = mutableMapOf() + internal val commands = mutableMapOf() internal val extensions = mutableListOf>() internal val prefixes = mutableListOf() var token: String = "" internal fun isExists(name: String?) = this.commands.containsKey(name) - fun commands(command: Command, name: String? = null) { + internal fun commands(command: CommandHolder, name: String? = null) { this.commands[if (name.isNullOrEmpty()) command.name else name] = command } diff --git a/core/src/main/kotlin/io/github/null2264/tsukumogami/core/module/BotModule.kt b/core/src/main/kotlin/io/github/null2264/tsukumogami/core/module/BotModule.kt index 26fe1d5..1cb21a6 100644 --- a/core/src/main/kotlin/io/github/null2264/tsukumogami/core/module/BotModule.kt +++ b/core/src/main/kotlin/io/github/null2264/tsukumogami/core/module/BotModule.kt @@ -2,7 +2,7 @@ package io.github.null2264.tsukumogami.core.module import co.touchlab.kermit.Logger import io.github.null2264.tsukumogami.core.AbstractBot -import io.github.null2264.tsukumogami.core.module.annotation.Command as CommandAnnotation +import io.github.null2264.tsukumogami.core.module.annotation.Command import io.github.null2264.tsukumogami.core.BotConfigurator import kotlin.reflect.jvm.isAccessible import kotlin.reflect.jvm.kotlinFunction @@ -20,7 +20,7 @@ abstract class BotModule(val name: String, val description: String? = null) { val methods = this::class.java.declaredMethods for (method in methods) { for (annotation in method.annotations) { - if (annotation !is CommandAnnotation) + if (annotation !is Command) continue configurator.apply { @@ -32,11 +32,11 @@ abstract class BotModule(val name: String, val description: String? = null) { kMethod?.let { it.isAccessible = true commands( - Command( + CommandHolder( annotation.name.ifEmpty { it.name }, name, - it, annotation.description.ifEmpty { description }, + it, ) ) } diff --git a/core/src/main/kotlin/io/github/null2264/tsukumogami/core/module/Command.kt b/core/src/main/kotlin/io/github/null2264/tsukumogami/core/module/CommandHolder.kt similarity index 88% rename from core/src/main/kotlin/io/github/null2264/tsukumogami/core/module/Command.kt rename to core/src/main/kotlin/io/github/null2264/tsukumogami/core/module/CommandHolder.kt index c5d64ed..f2a56dc 100644 --- a/core/src/main/kotlin/io/github/null2264/tsukumogami/core/module/Command.kt +++ b/core/src/main/kotlin/io/github/null2264/tsukumogami/core/module/CommandHolder.kt @@ -5,9 +5,9 @@ import kotlin.reflect.KFunction /** * Class holding information about a command */ -data class Command( +internal data class CommandHolder( val name: String, val extension: String, - val callback: KFunction<*>, val description: String? = null, + val callback: KFunction<*>, )