From 135cbd31a5379b5df9573ca353d2e2be3fa56a4e Mon Sep 17 00:00:00 2001 From: Ahmad Ansori Palembani Date: Tue, 20 May 2025 05:45:48 +0700 Subject: [PATCH] refactor: Move stuff around and replace println with Logger --- .../tsukumogami/bot/core/module/GeneralModule.kt | 3 ++- .../null2264/tsukumogami/core/AbstractBot.kt | 16 +++++++++------- .../tsukumogami/core/module/BotModule.kt | 3 ++- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/bot/src/main/kotlin/io/github/null2264/tsukumogami/bot/core/module/GeneralModule.kt b/bot/src/main/kotlin/io/github/null2264/tsukumogami/bot/core/module/GeneralModule.kt index c1e774d..ce39762 100644 --- a/bot/src/main/kotlin/io/github/null2264/tsukumogami/bot/core/module/GeneralModule.kt +++ b/bot/src/main/kotlin/io/github/null2264/tsukumogami/bot/core/module/GeneralModule.kt @@ -1,5 +1,6 @@ package io.github.null2264.tsukumogami.bot.core.module +import dev.kord.core.entity.effectiveName import io.github.null2264.tsukumogami.core.module.annotation.Command import io.github.null2264.tsukumogami.core.Context import io.github.null2264.tsukumogami.core.module.BotModule @@ -17,6 +18,6 @@ class GeneralModule : BotModule("General", "idk") { @Command("test") private suspend fun differentName(ctx: Context) { - ctx.send("Hello World! ${ctx.author}") + ctx.send("Hello World! ${ctx.author?.effectiveName}") } } 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 4f0f895..604b054 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 @@ -38,9 +38,7 @@ abstract class AbstractBot(configurator: BotConfigurator.() -> Unit) { client = runBlocking { Kord(currentConfig.token).apply { - on { - Logger.i { "Online! ${self.username}" } - } + on { onReady() } on { onMessage(this.message, this) } } @@ -65,11 +63,11 @@ abstract class AbstractBot(configurator: BotConfigurator.() -> Unit) { return Context(this, message, candidate?.first, candidate?.second) } - suspend fun onCommandError(context: Context, error: CommandException) { + open suspend fun onCommandError(context: Context, error: CommandException) { context.send(error.message!!) } - suspend fun processCommand(message: Message) { + open suspend fun processCommand(message: Message) { val ctx = getContext(message) try { @@ -83,12 +81,16 @@ abstract class AbstractBot(configurator: BotConfigurator.() -> Unit) { } } - suspend fun onMessage(message: Message, event: MessageCreateEvent) { + open suspend fun onMessage(message: Message, event: MessageCreateEvent) { if (message.author?.isBot != false) return processCommand(message) } + open suspend fun onReady() { + Logger.i { "Online! ${client.getSelf().username}" } + } + fun String.hasPrefix(): Pair? { if (this.isBlank()) return null @@ -96,7 +98,7 @@ abstract class AbstractBot(configurator: BotConfigurator.() -> Unit) { prefixes.forEach { if (this.substring(0, it.length) == it) { - ret = Pair(this.substring(0, it.length), this.substring(it.length)) + ret = Pair(this.substring(0, it.length), this.substring(it.length).split(" ").first()) return@forEach } } 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 b533d50..26fe1d5 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 @@ -1,5 +1,6 @@ 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.BotConfigurator @@ -25,7 +26,7 @@ abstract class BotModule(val name: String, val description: String? = null) { configurator.apply { val kMethod = method.kotlinFunction if (isExists(kMethod?.name)) { - println("Command already exists") + Logger.e { "Command already exists" } return } kMethod?.let {