refactor: Turn CommandHolder into internal data class

Since we want commands to be registered from modules
This commit is contained in:
Ahmad Ansori Palembani 2025-05-20 05:54:37 +07:00
parent 135cbd31a5
commit 1c20e05066
4 changed files with 11 additions and 11 deletions

View file

@ -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<String, Command>
private val commands: Map<String, CommandHolder>
private val extensions: Map<String, BotModule>
private val prefixes: List<String>
private val client: Kord

View file

@ -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<String, Command>()
internal val commands = mutableMapOf<String, CommandHolder>()
internal val extensions = mutableListOf<KFunction<BotModule>>()
internal val prefixes = mutableListOf<String>()
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
}

View file

@ -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,
)
)
}

View file

@ -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<*>,
)