From 3de960b26a8e7a1145d4b28180cd77348f420e58 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 9 Jul 2022 13:41:59 +0600 Subject: [PATCH] add template module --- introduction/build.gradle | 4 -- template/build.gradle | 26 ++++++++++ template/src/main/kotlin/TemplatePlugin.kt | 60 ++++++++++++++++++++++ 3 files changed, 86 insertions(+), 4 deletions(-) create mode 100644 template/build.gradle create mode 100644 template/src/main/kotlin/TemplatePlugin.kt diff --git a/introduction/build.gradle b/introduction/build.gradle index 52328a3..3b4d2f0 100644 --- a/introduction/build.gradle +++ b/introduction/build.gradle @@ -24,7 +24,3 @@ dependencies { api libs.plagubot.plugin api libs.kslog } - -application { - mainClassName = 'dev.inmo.plagubot.AppKt' -} diff --git a/template/build.gradle b/template/build.gradle new file mode 100644 index 0000000..3b4d2f0 --- /dev/null +++ b/template/build.gradle @@ -0,0 +1,26 @@ +buildscript { + repositories { + mavenCentral() + } + + dependencies { + classpath libs.kotlin.gradle.plugin + classpath libs.kotlin.serialization.plugin + } +} + +plugins { + alias libs.plugins.kotlin.jvm + alias libs.plugins.kotlin.serialization +} + +repositories { + mavenCentral() + mavenLocal() +} + +dependencies { + implementation libs.kotlin + api libs.plagubot.plugin + api libs.kslog +} diff --git a/template/src/main/kotlin/TemplatePlugin.kt b/template/src/main/kotlin/TemplatePlugin.kt new file mode 100644 index 0000000..7a00169 --- /dev/null +++ b/template/src/main/kotlin/TemplatePlugin.kt @@ -0,0 +1,60 @@ +import dev.inmo.kslog.common.logger +import dev.inmo.kslog.common.w +import dev.inmo.plagubot.Plugin +import dev.inmo.tgbotapi.extensions.api.send.reply +import dev.inmo.tgbotapi.extensions.api.send.sendMessage +import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext +import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommand +import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onMyChatMemberUpdated +import dev.inmo.tgbotapi.types.chat.PrivateChat +import kotlinx.serialization.Serializable +import kotlinx.serialization.json.* +import org.jetbrains.exposed.sql.Database +import org.koin.core.Koin +import org.koin.core.module.Module + +/** + * This is template of plugin with preset [log]ger, [Config] and template configurations of [setupDI] and [setupBotPlugin]. + * Replace [pluginConfigSectionName] value with your one to customize configuration section name + */ +@Serializable +class TemplatePlugin : Plugin { + /** + * Default logger of [TemplatePlugin] got with [logger] + */ + private val log = logger + + /** + * Configuration class for current plugin + * + * See realization of [setupDI] to get know how this class will be deserialized from global config + * + * See realization of [setupBotPlugin] to get know how to get access to this class + */ + @Serializable + private class Config( + ) + + /** + * DI configuration of current plugin. Here we are decoding [Config] and put it into [Module] receiver + */ + override fun Module.setupDI(database: Database, params: JsonObject) { + single { get().decodeFromJsonElement(Config.serializer(), params[pluginConfigSectionName] ?: return@single null) } + } + + /** + * Final configuration of bot. Here we are getting [Config] from [koin] and configure reaction on `/start` command + */ + override suspend fun BehaviourContext.setupBotPlugin(koin: Koin) { + val config = koin.getOrNull() + + if (config == null) { + log.w("Plugin has been disabled due to absence of \"$pluginConfigSectionName\" field in config or some error during configuration loading") + return + } + } + + companion object { + private const val pluginConfigSectionName = "new" + } +}