refactor: Move stuff around and replace println with Logger

This commit is contained in:
Ahmad Ansori Palembani 2025-05-20 05:45:48 +07:00
parent acde0d3ee5
commit 135cbd31a5
3 changed files with 13 additions and 9 deletions

View file

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

View file

@ -38,9 +38,7 @@ abstract class AbstractBot(configurator: BotConfigurator.() -> Unit) {
client = runBlocking {
Kord(currentConfig.token).apply {
on<ReadyEvent> {
Logger.i { "Online! ${self.username}" }
}
on<ReadyEvent> { onReady() }
on<MessageCreateEvent> { 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<String, String>? {
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
}
}

View file

@ -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 {