refactor: Turn CommandHolder into internal data class
Since we want commands to be registered from modules
This commit is contained in:
parent
135cbd31a5
commit
1c20e05066
4 changed files with 11 additions and 11 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
@ -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,
|
||||
)
|
||||
)
|
||||
}
|
||||
|
|
|
@ -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<*>,
|
||||
)
|
Loading…
Add table
Add a link
Reference in a new issue