refactor: Use Koin more and API to register module to Koin

This commit is contained in:
Ahmad Ansori Palembani 2025-05-26 07:12:33 +07:00
parent 26e5ba9e85
commit 96c85a351e
Signed by: null2264
GPG key ID: BA64F8B60AF3EFB6
6 changed files with 29 additions and 21 deletions

View file

@ -1,10 +1,8 @@
package io.github.null2264.tsukumogami.bot package io.github.null2264.tsukumogami.bot
import co.touchlab.kermit.Logger import co.touchlab.kermit.Logger
import io.github.null2264.tsukumogami.bot.core.di.appModule
import io.github.null2264.tsukumogami.bot.core.module.generalModule import io.github.null2264.tsukumogami.bot.core.module.generalModule
import io.github.null2264.tsukumogami.core.bot import io.github.null2264.tsukumogami.core.bot
import org.koin.core.context.GlobalContext.startKoin
suspend fun main() { suspend fun main() {

View file

@ -1,5 +1,6 @@
package io.github.null2264.tsukumogami.bot.core.module package io.github.null2264.tsukumogami.bot.core.module
import dev.kord.core.entity.effectiveName
import io.github.null2264.tsukumogami.bot.core.module.arguments.Test2Arguments import io.github.null2264.tsukumogami.bot.core.module.arguments.Test2Arguments
import io.github.null2264.tsukumogami.bot.core.module.arguments.TestArguments import io.github.null2264.tsukumogami.bot.core.module.arguments.TestArguments
import io.github.null2264.tsukumogami.core.module.api.botModules import io.github.null2264.tsukumogami.core.module.api.botModules
@ -18,6 +19,7 @@ val generalModule = botModules("General") {
groups("group") { groups("group") {
commands("test", alias = setOf("t")) { ctx -> commands("test", alias = setOf("t")) { ctx ->
ctx.reply("Hello world ${ctx.command?.module?.name}") ctx.reply("Hello world ${ctx.command?.module?.name}")
ctx.reply("Bot: ${ctx.bot.self().effectiveName}")
} }
} }
} }

View file

@ -3,6 +3,7 @@ package io.github.null2264.tsukumogami.core
import co.touchlab.kermit.Logger import co.touchlab.kermit.Logger
import dev.kord.core.Kord import dev.kord.core.Kord
import dev.kord.core.entity.Message import dev.kord.core.entity.Message
import dev.kord.core.entity.User
import dev.kord.core.event.gateway.ReadyEvent import dev.kord.core.event.gateway.ReadyEvent
import dev.kord.core.event.message.MessageCreateEvent import dev.kord.core.event.message.MessageCreateEvent
import dev.kord.core.on import dev.kord.core.on
@ -28,6 +29,8 @@ open class Bot internal constructor(): IGroup, TsukumogamiKoinComponent {
internal lateinit var token: String internal lateinit var token: String
override val allCommands: MutableMap<String, Command> = mutableMapOf() override val allCommands: MutableMap<String, Command> = mutableMapOf()
suspend fun self() = client.getSelf().asUser()
fun addModule(module: BotModule) { fun addModule(module: BotModule) {
modules[module.name] = module.install(this) modules[module.name] = module.install(this)
} }
@ -75,7 +78,7 @@ open class Bot internal constructor(): IGroup, TsukumogamiKoinComponent {
private fun getContext(message: Message): Context { private fun getContext(message: Message): Context {
val parsed = message.content.parsePrefixAndCommand() val parsed = message.content.parsePrefixAndCommand()
val context = Context(this, message, parsed?.first) val context = Context(message, parsed?.first)
context.command = getCommand(parsed?.second) context.command = getCommand(parsed?.second)
if (context.command != null) context.invokedWith = parsed?.second if (context.command != null) context.invokedWith = parsed?.second
return context return context

View file

@ -9,6 +9,7 @@ import io.github.null2264.tsukumogami.core.koin.TsukumogamiKoinContext
import io.github.null2264.tsukumogami.core.module.BotModule import io.github.null2264.tsukumogami.core.module.BotModule
import kotlin.reflect.KFunction import kotlin.reflect.KFunction
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import org.koin.core.module.Module as KoinModule
import org.koin.dsl.bind import org.koin.dsl.bind
class BotBuilder internal constructor( class BotBuilder internal constructor(
@ -24,6 +25,9 @@ class BotBuilder internal constructor(
} }
} }
internal var beforeKoinSetup: () -> Unit = {}
internal var afterKoinSetup: () -> Unit = {}
fun modules(vararg modules: BotModule) { fun modules(vararg modules: BotModule) {
modules.forEach(bot::addModule) modules.forEach(bot::addModule)
} }
@ -31,9 +35,17 @@ class BotBuilder internal constructor(
fun prefixes(vararg prefixes: String) { fun prefixes(vararg prefixes: String) {
prefixes.forEach(bot::addPrefix) prefixes.forEach(bot::addPrefix)
} }
fun beforeKoinSetup(declaration: () -> Unit) {
beforeKoinSetup = declaration
}
fun afterKoinSetup(declaration: () -> Unit) {
afterKoinSetup = declaration
}
} }
fun bot(clazz: KFunction<Bot> = ::Bot, declaration: BotBuilder.() -> Unit): Bot { suspend fun bot(clazz: KFunction<Bot> = ::Bot, declaration: BotBuilder.() -> Unit): Bot {
if (TsukumogamiKoinContext.getOrNull() == null) if (TsukumogamiKoinContext.getOrNull() == null)
TsukumogamiKoinContext.startKoin { TsukumogamiKoinContext.startKoin {
} }
@ -42,11 +54,13 @@ fun bot(clazz: KFunction<Bot> = ::Bot, declaration: BotBuilder.() -> Unit): Bot
val holder = BotBuilder(bot) val holder = BotBuilder(bot)
declaration(holder) declaration(holder)
val kord = runBlocking { holder.beforeKoinSetup.invoke()
holder.kordBuilder(holder.token)
} val kord = holder.kordBuilder(holder.token)
loadModule { single { kord } bind Kord::class } loadModule { single { kord } bind Kord::class }
loadModule { single { bot } bind Bot::class } loadModule { single { bot } bind Bot::class }
holder.afterKoinSetup.invoke()
return holder.bot return holder.bot
} }

View file

@ -5,15 +5,18 @@ import dev.kord.core.entity.Message
import dev.kord.rest.builder.message.AllowedMentionsBuilder import dev.kord.rest.builder.message.AllowedMentionsBuilder
import dev.kord.rest.builder.message.allowedMentions import dev.kord.rest.builder.message.allowedMentions
import io.github.null2264.tsukumogami.core.commands.Command import io.github.null2264.tsukumogami.core.commands.Command
import io.github.null2264.tsukumogami.core.koin.TsukumogamiKoinComponent
import org.koin.core.component.inject
class Context( class Context(
val bot: Bot,
val message: Message, val message: Message,
/** /**
* The prefix that used to invoke the command * The prefix that used to invoke the command
*/ */
val prefix: String?, val prefix: String?,
) { ) : TsukumogamiKoinComponent {
val bot: Bot by inject()
/** /**
* The user that invoked the command * The user that invoked the command

View file

@ -1,12 +0,0 @@
package io.github.null2264.tsukumogami.core.commands.annotation
/**
* Annotation to tag a function as command
*
* @param name the command's name
* @param description the command's description
* @param help the command's extended description (a more detailed description)
*/
@Retention(AnnotationRetention.RUNTIME)
@Target(AnnotationTarget.FUNCTION)
annotation class Command(val name: String = "", val description: String = "", val help: String = "")