From 15109391b16e5336156d7f1171bbebd42dd57dd2 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 3 Apr 2021 16:04:18 +0600 Subject: [PATCH 1/4] start 0.1.7 --- CHANGELOG.md | 2 ++ gradle.properties | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3411c8b..0bd5d32 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ # Changelog +## 0.1.7 + ## 0.1.6 * `Versions` diff --git a/gradle.properties b/gradle.properties index cb494a7..2fcf1bb 100644 --- a/gradle.properties +++ b/gradle.properties @@ -19,4 +19,4 @@ sqlite_version=3.30.1 github_release_plugin_version=2.2.12 group=dev.inmo -version=0.1.6 +version=0.1.7 From 8444172e445974bc08be557996cf7eaa16ccda2c Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 3 Apr 2021 22:04:18 +0600 Subject: [PATCH 2/4] PlaguBot class --- CHANGELOG.md | 6 ++ bot/src/main/kotlin/dev/inmo/plagubot/App.kt | 35 +++--------- .../main/kotlin/dev/inmo/plagubot/PlaguBot.kt | 56 +++++++++++++++++++ 3 files changed, 71 insertions(+), 26 deletions(-) create mode 100644 bot/src/main/kotlin/dev/inmo/plagubot/PlaguBot.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 0bd5d32..4118b17 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ ## 0.1.7 +* `PlaguBot` + * New class `PlaguBot` (😊) + * `initPlaguBot` is deprecated + * New shortcut for params - `plagubot`. `PlaguBot` class can be put inside other plagubot + for additional opportunities + ## 0.1.6 * `Versions` diff --git a/bot/src/main/kotlin/dev/inmo/plagubot/App.kt b/bot/src/main/kotlin/dev/inmo/plagubot/App.kt index 78ee034..3676fc5 100644 --- a/bot/src/main/kotlin/dev/inmo/plagubot/App.kt +++ b/bot/src/main/kotlin/dev/inmo/plagubot/App.kt @@ -1,38 +1,22 @@ package dev.inmo.plagubot -import dev.inmo.micro_utils.coroutines.safelyWithoutExceptions import dev.inmo.plagubot.config.* import dev.inmo.plagubot.config.configJsonFormat -import dev.inmo.tgbotapi.bot.Ktor.telegramBot -import dev.inmo.tgbotapi.extensions.api.bot.setMyCommands -import dev.inmo.tgbotapi.extensions.behaviour_builder.buildBehaviour -import dev.inmo.tgbotapi.types.botCommandsLimit import kotlinx.coroutines.* import kotlinx.serialization.InternalSerializationApi import java.io.File +@Deprecated( + "This method is redundant due to new class PlaguBot", + ReplaceWith( + "PlaguBot(config).start(scope)", + "dev.inmo.plagubot.PlaguBot" + ) +) suspend inline fun initPlaguBot( config: Config, scope: CoroutineScope = CoroutineScope(Dispatchers.Default) -): Job { - val bot = telegramBot(config.botToken) - - val paramsMap = config.params ?.toMap() ?: emptyMap() - val database = config.params ?.database ?: config.database.database - return bot.buildBehaviour(scope) { - val commands = config.plugins.flatMap { - it.apply { invoke(database, paramsMap) } - it.getCommands() - }.let { - val futureUnavailable = it.drop(botCommandsLimit.last) - if (futureUnavailable.isNotEmpty()) { - println("Next commands are out of range in setting command request and will be unavailable from autocompleting: ${futureUnavailable}") - } - it.take(botCommandsLimit.last) - } - safelyWithoutExceptions { setMyCommands(commands) } - } -} +): Job = PlaguBot(config).start(scope) /** * This method by default expects one argument in [args] field: path to config @@ -43,6 +27,5 @@ suspend fun main(args: Array) { val file = File(configPath) val config = configJsonFormat.decodeFromString(ConfigSerializer, file.readText()) - val scope = CoroutineScope(Dispatchers.Default) - initPlaguBot(config, scope).join() + PlaguBot(config).start().join() } diff --git a/bot/src/main/kotlin/dev/inmo/plagubot/PlaguBot.kt b/bot/src/main/kotlin/dev/inmo/plagubot/PlaguBot.kt new file mode 100644 index 0000000..ca7e3b5 --- /dev/null +++ b/bot/src/main/kotlin/dev/inmo/plagubot/PlaguBot.kt @@ -0,0 +1,56 @@ +package dev.inmo.plagubot + +import dev.inmo.micro_utils.coroutines.safelyWithoutExceptions +import dev.inmo.plagubot.config.Config +import dev.inmo.plagubot.config.database +import dev.inmo.tgbotapi.bot.Ktor.telegramBot +import dev.inmo.tgbotapi.extensions.api.bot.setMyCommands +import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext +import dev.inmo.tgbotapi.extensions.behaviour_builder.buildBehaviour +import dev.inmo.tgbotapi.types.BotCommand +import dev.inmo.tgbotapi.types.botCommandsLimit +import kotlinx.coroutines.* +import kotlinx.serialization.Serializable +import kotlinx.serialization.Transient +import org.jetbrains.exposed.sql.Database + +const val DefaultPlaguBotParamsKey = "plagubot" +val Map.plagubot + get() = get(DefaultPlaguBotParamsKey) as? PlaguBot + +@Serializable +data class PlaguBot( + private val config: Config +) : Plugin { + @Transient + private val bot = telegramBot(config.botToken) + @Transient + private val paramsMap = config.params ?.toMap() ?: emptyMap() + @Transient + private val database = config.params ?.database ?: config.database.database + + override suspend fun getCommands(): List = config.plugins.flatMap { + it.getCommands() + } + + override suspend fun BehaviourContext.invoke(database: Database, params: Map) { + config.plugins.forEach { + it.apply { invoke(database, params) } + } + val commands = getCommands() + val futureUnavailable = commands.drop(botCommandsLimit.last) + if (futureUnavailable.isNotEmpty()) { + println("Next commands are out of range in setting command request and will be unavailable from autocompleting: $futureUnavailable") + } + safelyWithoutExceptions { setMyCommands(commands.take(botCommandsLimit.last)) } + } + + /** + * This method will create an [Job] which will be the main [Job] of ran instance + */ + suspend fun start( + scope: CoroutineScope = CoroutineScope(Dispatchers.Default) + ): Job = bot.buildBehaviour(scope) { + invoke(database, paramsMap) + } +} From 1ffb7a1999d121db4304db4fef6f765545f88034 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 3 Apr 2021 22:05:18 +0600 Subject: [PATCH 3/4] update of versions --- CHANGELOG.md | 4 ++++ gradle.properties | 6 +++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4118b17..23dc068 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## 0.1.7 +* `Versions` + * `exposed`: `0.29.1` -> `0.30.1` + * `tgbotapi`: `0.33.1` -> `0.33.2` + * `microutils`: `0.4.31` -> `0.4.32` * `PlaguBot` * New class `PlaguBot` (😊) * `initPlaguBot` is deprecated diff --git a/gradle.properties b/gradle.properties index 2fcf1bb..3c88aed 100644 --- a/gradle.properties +++ b/gradle.properties @@ -7,11 +7,11 @@ kotlin.incremental=true kotlin_version=1.4.32 kotlin_coroutines_version=1.4.3 kotlin_serialisation_runtime_version=1.1.0 -kotlin_exposed_version=0.29.1 +kotlin_exposed_version=0.30.1 sdi_version=0.4.1 -tgbotapi_version=0.33.1 -microutils_version=0.4.31 +tgbotapi_version=0.33.2 +microutils_version=0.4.32 klassindex_version=4.1.0-rc.1 sqlite_version=3.30.1 From d94df497956ea95c5f3c3db1381e79f19efe63e2 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 3 Apr 2021 22:07:43 +0600 Subject: [PATCH 4/4] optimize imports --- bot/src/main/kotlin/dev/inmo/plagubot/App.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/bot/src/main/kotlin/dev/inmo/plagubot/App.kt b/bot/src/main/kotlin/dev/inmo/plagubot/App.kt index 3676fc5..cfa0bbe 100644 --- a/bot/src/main/kotlin/dev/inmo/plagubot/App.kt +++ b/bot/src/main/kotlin/dev/inmo/plagubot/App.kt @@ -1,7 +1,6 @@ package dev.inmo.plagubot import dev.inmo.plagubot.config.* -import dev.inmo.plagubot.config.configJsonFormat import kotlinx.coroutines.* import kotlinx.serialization.InternalSerializationApi import java.io.File