From 5861b909cd87fbd71dd98fca6026e6e350dcf371 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 9 Jul 2022 13:44:12 +0600 Subject: [PATCH] add welcome plugin (currently uncompilable) and small fix in kdoc of template --- template/src/main/kotlin/TemplatePlugin.kt | 4 +- welcome/build.gradle | 26 ++++++++++ welcome/src/main/kotlin/WelcomePlugin.kt | 60 ++++++++++++++++++++++ 3 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 welcome/build.gradle create mode 100644 welcome/src/main/kotlin/WelcomePlugin.kt diff --git a/template/src/main/kotlin/TemplatePlugin.kt b/template/src/main/kotlin/TemplatePlugin.kt index 7a00169..0c984b9 100644 --- a/template/src/main/kotlin/TemplatePlugin.kt +++ b/template/src/main/kotlin/TemplatePlugin.kt @@ -20,7 +20,7 @@ import org.koin.core.module.Module @Serializable class TemplatePlugin : Plugin { /** - * Default logger of [TemplatePlugin] got with [logger] + * Default logger of [WelcomePlugin] got with [logger] */ private val log = logger @@ -43,7 +43,7 @@ class TemplatePlugin : Plugin { } /** - * Final configuration of bot. Here we are getting [Config] from [koin] and configure reaction on `/start` command + * Final configuration of bot. Here we are getting [Config] from [koin] */ override suspend fun BehaviourContext.setupBotPlugin(koin: Koin) { val config = koin.getOrNull() diff --git a/welcome/build.gradle b/welcome/build.gradle new file mode 100644 index 0000000..3b4d2f0 --- /dev/null +++ b/welcome/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/welcome/src/main/kotlin/WelcomePlugin.kt b/welcome/src/main/kotlin/WelcomePlugin.kt new file mode 100644 index 0000000..5998a2a --- /dev/null +++ b/welcome/src/main/kotlin/WelcomePlugin.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 WelcomePlugin : Plugin { + /** + * Default logger of [WelcomePlugin] 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] + */ + 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 = "welcome" + } +}