From 0ecf6cbf59b4d93f8eadc20bb3b795766f663ecb Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 17 Jun 2022 22:24:44 +0600 Subject: [PATCH] introduction plugin --- build.gradle | 2 + config.json | 6 +-- gradle/libs.versions.toml | 3 ++ introduction/build.gradle | 30 +++++++++++++ .../src/main/kotlin/IntroductionPlugin.kt | 44 +++++++++++++++++++ settings.gradle | 2 + 6 files changed, 84 insertions(+), 3 deletions(-) create mode 100644 introduction/build.gradle create mode 100644 introduction/src/main/kotlin/IntroductionPlugin.kt diff --git a/build.gradle b/build.gradle index 1080345..423b937 100644 --- a/build.gradle +++ b/build.gradle @@ -23,6 +23,8 @@ repositories { dependencies { implementation libs.kotlin implementation libs.plagubot + + implementation project(":introduction") } application { diff --git a/config.json b/config.json index 367f05b..6878d1e 100644 --- a/config.json +++ b/config.json @@ -1,10 +1,10 @@ { "botToken": "1234567890:ABCDEFGHIJKLMNOP_qrstuvwxyz12345678", "plugins": [ - "dev.inmo.plagubot.HelloPlugin" + "IntroductionPlugin" ], - "helloPlugin": { - "print": "Hello World" + "introduction": { + "onStartCommandMessage": "Hello World" }, "database": { "url": "jdbc:sqlite:file:local.db?mode=memory&cache=shared" diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 7bb663b..39c3a57 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -2,11 +2,14 @@ kotlin = "1.6.21" plagubot = "1.1.1" +kslog = "0.3.1" [libraries] kotlin = { module = "org.jetbrains.kotlin:kotlin-stdlib", version.ref = "kotlin" } plagubot = { module = "dev.inmo:plagubot.bot", version.ref = "plagubot" } +plagubot-plugin = { module = "dev.inmo:plagubot.plugin", version.ref = "plagubot" } +kslog = { module = "dev.inmo:kslog", version.ref = "kslog" } # Libs for classpath kotlin-gradle-plugin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" } diff --git a/introduction/build.gradle b/introduction/build.gradle new file mode 100644 index 0000000..52328a3 --- /dev/null +++ b/introduction/build.gradle @@ -0,0 +1,30 @@ +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 +} + +application { + mainClassName = 'dev.inmo.plagubot.AppKt' +} diff --git a/introduction/src/main/kotlin/IntroductionPlugin.kt b/introduction/src/main/kotlin/IntroductionPlugin.kt new file mode 100644 index 0000000..d4dd140 --- /dev/null +++ b/introduction/src/main/kotlin/IntroductionPlugin.kt @@ -0,0 +1,44 @@ +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 + +@Serializable +class IntroductionPlugin : Plugin { + private val log = logger + + @Serializable + private class Config( + val onStartCommandMessage: String + ) + + override fun Module.setupDI(database: Database, params: JsonObject) { + single { get().decodeFromJsonElement(Config.serializer(), params["introduction"] ?: return@single null) } + } + + override suspend fun BehaviourContext.setupBotPlugin(koin: Koin) { + val config = koin.getOrNull() + + if (config == null) { + log.w("Plugin has been disabled due to absence of \"introduction\" field in config or some error during configuration loading") + return + } + + onCommand("start", initialFilter = { it.chat is PrivateChat }) { + sendMessage( + it.chat, + config.onStartCommandMessage + ) + } + } +} diff --git a/settings.gradle b/settings.gradle index c9dee36..94d9c36 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1 +1,3 @@ rootProject.name = "bot" + +include ":introduction"