mirror of
https://github.com/InsanusMokrassar/PlaguBot.git
synced 2024-12-22 05:57:11 +00:00
integrate startup launcher
This commit is contained in:
parent
37b5b8e867
commit
1b1b069d93
@ -2,6 +2,8 @@
|
||||
|
||||
## 8.1.0
|
||||
|
||||
* Integrate `dev.inmo:micro_utils.startup` into project
|
||||
|
||||
## 8.0.0
|
||||
|
||||
* `Versions`:
|
||||
|
@ -20,6 +20,7 @@ dependencies {
|
||||
api libs.tgbotapi.behaviourBuilder.fsm
|
||||
api libs.microutils.repos.exposed
|
||||
api libs.microutils.koin
|
||||
api libs.microutils.startup.launcher
|
||||
|
||||
api libs.sqlite
|
||||
|
||||
|
@ -32,8 +32,6 @@ object HelloPlugin : Plugin {
|
||||
override fun Module.setupDI(database: Database, params: JsonObject) {
|
||||
single {
|
||||
get<Json>().decodeFromJsonElement(HelloPluginConfig.serializer(), params["helloPlugin"] ?: return@single null)
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -43,6 +41,11 @@ object HelloPlugin : Plugin {
|
||||
data class SaidHelloOnce(override val context: IdChatIdentifier) : InternalFSMState
|
||||
}
|
||||
|
||||
override suspend fun startPlugin(koin: Koin) {
|
||||
super.startPlugin(koin)
|
||||
logger.i { "This logic called BEFORE the bot will be started and setup" }
|
||||
}
|
||||
|
||||
override suspend fun BehaviourContextWithFSM<State>.setupBotPlugin(koin: Koin) {
|
||||
val toPrint = koin.getOrNull<HelloPluginConfig>() ?.print ?: "Hello :)"
|
||||
logger.d { toPrint }
|
||||
|
@ -46,24 +46,28 @@ data class PlaguBot(
|
||||
}
|
||||
|
||||
override fun KtorRequestsExecutorBuilder.setupBotClient() {
|
||||
config.plugins.forEach {
|
||||
config.botPlugins.forEach {
|
||||
with(it) {
|
||||
setupBotClient()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun Module.setupDI(database: Database, params: JsonObject) {
|
||||
single { config }
|
||||
single { config.plugins }
|
||||
single { config.databaseConfig }
|
||||
single { config.databaseConfig.database }
|
||||
override fun Module.setupDI(config: JsonObject) {
|
||||
single { this@PlaguBot.config }
|
||||
single { this@PlaguBot.config.plugins }
|
||||
single { this@PlaguBot.config.databaseConfig }
|
||||
single { this@PlaguBot.config.databaseConfig.database }
|
||||
single { defaultJsonFormat }
|
||||
single { this@PlaguBot }
|
||||
single { bot }
|
||||
}
|
||||
|
||||
override fun Module.setupDI(database: Database, params: JsonObject) {
|
||||
setupDI(params)
|
||||
|
||||
includes(
|
||||
config.plugins.mapNotNull {
|
||||
config.botPlugins.mapNotNull {
|
||||
runCatching {
|
||||
module {
|
||||
with(it) {
|
||||
@ -71,14 +75,31 @@ data class PlaguBot(
|
||||
}
|
||||
}
|
||||
}.onFailure { e ->
|
||||
logger.w("Unable to load DI part of $it", e)
|
||||
logger.w(e) { "Unable to load DI part of $it" }
|
||||
}.getOrNull()
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
override suspend fun BehaviourContextWithFSM<State>.setupBotPlugin(koin: Koin) {
|
||||
override suspend fun startPlugin(koin: Koin) {
|
||||
super.startPlugin(koin)
|
||||
|
||||
config.plugins.forEach { plugin ->
|
||||
runCatchingSafely {
|
||||
logger.i { "Starting of $plugin common logic" }
|
||||
with(plugin) {
|
||||
startPlugin(koin)
|
||||
}
|
||||
}.onFailure { e ->
|
||||
logger.w(e) { "Unable to load common logic of $plugin" }
|
||||
}.onSuccess {
|
||||
logger.i { "Complete loading of $plugin common logic" }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun BehaviourContextWithFSM<State>.setupBotPlugin(koin: Koin) {
|
||||
config.botPlugins.forEach { plugin ->
|
||||
runCatchingSafely {
|
||||
logger.i("Start loading of $plugin")
|
||||
with(plugin) {
|
||||
@ -105,9 +126,11 @@ data class PlaguBot(
|
||||
setupDI(config.databaseConfig.database, json)
|
||||
}
|
||||
)
|
||||
logger.i("Modules loaded")
|
||||
logger.i("Modules loaded. Starting koin")
|
||||
GlobalContext.startKoin(koinApp)
|
||||
logger.i("Koin started")
|
||||
logger.i("Koin started. Starting plugins common logic")
|
||||
startPlugin(koinApp.koin)
|
||||
logger.i("Plugins common logic started. Starting setup of bot logic part")
|
||||
lateinit var behaviourContext: BehaviourContext
|
||||
val onStartContextsConflictResolver by lazy { koinApp.koin.getAllDistinct<OnStartContextsConflictResolver>() }
|
||||
val onUpdateContextsConflictResolver by lazy { koinApp.koin.getAllDistinct<OnUpdateContextsConflictResolver>() }
|
||||
|
@ -1,17 +1,20 @@
|
||||
package dev.inmo.plagubot.config
|
||||
|
||||
import dev.inmo.micro_utils.common.Warning
|
||||
import dev.inmo.micro_utils.startup.plugin.StartPlugin
|
||||
import dev.inmo.plagubot.Plugin
|
||||
import dev.inmo.tgbotapi.utils.telegramBotAPIDefaultUrl
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Warning("This API is internal and can be changed without notifications of mentions of changes")
|
||||
@Warning("This API is internal and can be changed without notifications or mentions of changes")
|
||||
@Serializable
|
||||
data class Config(
|
||||
val botToken: String,
|
||||
val plugins: List<Plugin>,
|
||||
val plugins: List<StartPlugin>,
|
||||
@SerialName("database")
|
||||
val databaseConfig: DatabaseConfig = DatabaseConfig(),
|
||||
val botApiServer: String = telegramBotAPIDefaultUrl
|
||||
)
|
||||
) {
|
||||
val botPlugins = plugins.filterIsInstance<Plugin>()
|
||||
}
|
||||
|
@ -30,6 +30,8 @@ tgbotapi-behaviourBuilder-fsm = { module = "dev.inmo:tgbotapi.behaviour_builder.
|
||||
microutils-repos-exposed = { module = "dev.inmo:micro_utils.repos.exposed", version.ref = "microutils" }
|
||||
microutils-koin = { module = "dev.inmo:micro_utils.koin", version.ref = "microutils" }
|
||||
microutils-koin-generator = { module = "dev.inmo:micro_utils.koin.generator", version.ref = "microutils" }
|
||||
microutils-startup-launcher = { module = "dev.inmo:micro_utils.startup.launcher", version.ref = "microutils" }
|
||||
microutils-startup-plugin = { module = "dev.inmo:micro_utils.startup.plugin", version.ref = "microutils" }
|
||||
|
||||
koin = { module = "io.insert-koin:koin-core", version.ref = "koin" }
|
||||
|
||||
|
@ -14,6 +14,7 @@ dependencies {
|
||||
|
||||
api libs.tgbotapi
|
||||
api libs.microutils.repos.exposed
|
||||
api libs.microutils.startup.plugin
|
||||
|
||||
api libs.koin
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package dev.inmo.plagubot
|
||||
|
||||
import dev.inmo.micro_utils.fsm.common.State
|
||||
import dev.inmo.micro_utils.startup.plugin.StartPlugin
|
||||
import dev.inmo.tgbotapi.bot.ktor.KtorRequestsExecutorBuilder
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContextWithFSM
|
||||
@ -18,7 +19,7 @@ import org.koin.core.module.Module
|
||||
* too.
|
||||
*/
|
||||
@Serializable(PluginSerializer::class)
|
||||
interface Plugin {
|
||||
interface Plugin : StartPlugin {
|
||||
fun KtorRequestsExecutorBuilder.setupBotClient() {}
|
||||
|
||||
/**
|
||||
@ -27,7 +28,9 @@ interface Plugin {
|
||||
fun Module.setupDI(
|
||||
database: Database,
|
||||
params: JsonObject
|
||||
) {}
|
||||
) {
|
||||
setupDI(params)
|
||||
}
|
||||
|
||||
/**
|
||||
* Override this method in cases when you want to declare common bot behaviour. In case you wish to use FSM, you
|
||||
|
Loading…
Reference in New Issue
Block a user