2021-04-03 16:04:18 +00:00
|
|
|
package dev.inmo.plagubot
|
|
|
|
|
2021-05-09 06:37:28 +00:00
|
|
|
import dev.inmo.plagubot.config.*
|
2022-05-13 07:04:49 +00:00
|
|
|
import dev.inmo.tgbotapi.bot.ktor.telegramBot
|
2021-11-12 08:05:42 +00:00
|
|
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.*
|
2022-05-16 14:07:57 +00:00
|
|
|
import dev.inmo.tgbotapi.extensions.utils.updates.retrieving.startGettingOfUpdatesByLongPolling
|
2021-04-03 16:04:18 +00:00
|
|
|
import kotlinx.coroutines.*
|
|
|
|
import kotlinx.serialization.Serializable
|
|
|
|
import kotlinx.serialization.Transient
|
2022-05-16 14:07:57 +00:00
|
|
|
import kotlinx.serialization.json.JsonObject
|
2021-04-03 16:04:18 +00:00
|
|
|
import org.jetbrains.exposed.sql.Database
|
2022-05-16 14:07:57 +00:00
|
|
|
import org.koin.core.KoinApplication
|
|
|
|
import org.koin.core.component.get
|
|
|
|
import org.koin.core.context.GlobalContext
|
|
|
|
import org.koin.core.qualifier.named
|
|
|
|
import org.koin.dsl.module
|
2021-04-03 16:04:18 +00:00
|
|
|
|
|
|
|
const val DefaultPlaguBotParamsKey = "plagubot"
|
2022-05-16 14:07:57 +00:00
|
|
|
val Plugin.plagubot: PlaguBot
|
|
|
|
get() = get()
|
2021-04-03 16:04:18 +00:00
|
|
|
|
|
|
|
@Serializable
|
|
|
|
data class PlaguBot(
|
2022-05-16 14:07:57 +00:00
|
|
|
private val json: JsonObject,
|
2021-04-03 16:04:18 +00:00
|
|
|
private val config: Config
|
|
|
|
) : Plugin {
|
|
|
|
@Transient
|
|
|
|
private val bot = telegramBot(config.botToken)
|
|
|
|
|
2022-05-16 14:07:57 +00:00
|
|
|
override suspend fun BehaviourContext.invoke(
|
|
|
|
database: Database,
|
|
|
|
params: JsonObject
|
|
|
|
) {
|
2021-04-03 16:04:18 +00:00
|
|
|
config.plugins.forEach {
|
|
|
|
it.apply { invoke(database, params) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method will create an [Job] which will be the main [Job] of ran instance
|
|
|
|
*/
|
|
|
|
suspend fun start(
|
|
|
|
scope: CoroutineScope = CoroutineScope(Dispatchers.Default)
|
2022-05-16 14:07:57 +00:00
|
|
|
): Job {
|
|
|
|
val koinApp = KoinApplication.init()
|
|
|
|
koinApp.modules(
|
|
|
|
module {
|
|
|
|
single { config }
|
|
|
|
single { config.plugins }
|
|
|
|
single { config.database }
|
|
|
|
single(named(defaultDatabaseParamsName)) { config.database.database }
|
|
|
|
single { defaultJsonFormat }
|
|
|
|
single(named(DefaultPlaguBotParamsKey)) { this@PlaguBot }
|
|
|
|
}
|
|
|
|
)
|
|
|
|
lateinit var behaviourContext: BehaviourContext
|
|
|
|
bot.buildBehaviour(scope = scope) {
|
|
|
|
invoke(config.database.database, json)
|
|
|
|
behaviourContext = this
|
|
|
|
}
|
|
|
|
GlobalContext.startKoin(koinApp)
|
|
|
|
return bot.startGettingOfUpdatesByLongPolling(scope = behaviourContext, updatesFilter = behaviourContext)
|
2021-04-03 16:04:18 +00:00
|
|
|
}
|
|
|
|
}
|