From 595102fede897a164b0b4be47c853b0aef7b9d03 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sun, 10 Jan 2021 15:21:31 +0600 Subject: [PATCH] BehaviourContext.invoke --- CHANGELOG.md | 3 +++ bot/src/main/kotlin/dev/inmo/plagubot/App.kt | 11 ++++------- .../main/kotlin/dev/inmo/plagubot/HelloPlugin.kt | 11 ++--------- .../src/main/kotlin/dev/inmo/plagubot/Plugin.kt | 16 ++++++++++++---- 4 files changed, 21 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0cc245b..5fd2c79 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,9 @@ * `Versions` * `tgbotapi`: `0.30.10` -> `0.31.0` * `microutils`: `0.4.11` -> `0.4.16` +* `Plugin` + * New method `BehaviourContext#invoke` + * Old method `invoke` has been deprecated ## 0.0.5 diff --git a/bot/src/main/kotlin/dev/inmo/plagubot/App.kt b/bot/src/main/kotlin/dev/inmo/plagubot/App.kt index c32a5fc..de2672b 100644 --- a/bot/src/main/kotlin/dev/inmo/plagubot/App.kt +++ b/bot/src/main/kotlin/dev/inmo/plagubot/App.kt @@ -5,6 +5,7 @@ import dev.inmo.plagubot.config.Config import dev.inmo.plagubot.config.configSerialFormat 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.extensions.utils.updates.retrieving.longPolling import dev.inmo.tgbotapi.types.botCommandsLimit import kotlinx.coroutines.* @@ -17,9 +18,9 @@ suspend inline fun initPlaguBot( ) { val bot = telegramBot(config.botToken) - bot.longPolling(scope = scope) { + bot.buildBehaviour(scope) { val commands = config.plugins.flatMap { - it.invoke(bot, config.database.database, this, scope) + it.apply { invoke(config.database.database) } it.getCommands() }.let { val futureUnavailable = it.drop(botCommandsLimit.last) @@ -28,11 +29,7 @@ suspend inline fun initPlaguBot( } it.take(botCommandsLimit.last) } - scope.launch { - safelyWithoutExceptions { - bot.setMyCommands(commands) - } - } + safelyWithoutExceptions { setMyCommands(commands) } } } diff --git a/bot/src/main/kotlin/dev/inmo/plagubot/HelloPlugin.kt b/bot/src/main/kotlin/dev/inmo/plagubot/HelloPlugin.kt index 19ef11e..1b66d96 100644 --- a/bot/src/main/kotlin/dev/inmo/plagubot/HelloPlugin.kt +++ b/bot/src/main/kotlin/dev/inmo/plagubot/HelloPlugin.kt @@ -1,8 +1,6 @@ package dev.inmo.plagubot -import dev.inmo.tgbotapi.bot.TelegramBot -import dev.inmo.tgbotapi.updateshandlers.FlowsUpdatesFilter -import kotlinx.coroutines.CoroutineScope +import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable import org.jetbrains.exposed.sql.Database @@ -12,12 +10,7 @@ import org.jetbrains.exposed.sql.Database data class HelloPlugin( val parameter: String ) : Plugin { - override suspend fun invoke( - bot: TelegramBot, - database: Database, - updatesFilter: FlowsUpdatesFilter, - scope: CoroutineScope - ) { + override suspend fun BehaviourContext.invoke(database: Database) { println(parameter) } } diff --git a/plugin/src/main/kotlin/dev/inmo/plagubot/Plugin.kt b/plugin/src/main/kotlin/dev/inmo/plagubot/Plugin.kt index b37fb75..dbd17b1 100644 --- a/plugin/src/main/kotlin/dev/inmo/plagubot/Plugin.kt +++ b/plugin/src/main/kotlin/dev/inmo/plagubot/Plugin.kt @@ -1,6 +1,7 @@ package dev.inmo.plagubot import dev.inmo.tgbotapi.bot.TelegramBot +import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext import dev.inmo.tgbotapi.types.BotCommand import dev.inmo.tgbotapi.updateshandlers.FlowsUpdatesFilter import kotlinx.coroutines.CoroutineScope @@ -19,13 +20,20 @@ interface Plugin { */ suspend fun getCommands(): List = emptyList() - /** - * This method (usually) will be invoked just one time in the whole application. - */ + @Deprecated("Override other method with receiver BehaviourContext") suspend operator fun invoke( bot: TelegramBot, database: Database, updatesFilter: FlowsUpdatesFilter, scope: CoroutineScope - ) + ) {} + + /** + * This method (usually) will be invoked just one time in the whole application. + */ + suspend operator fun BehaviourContext.invoke( + database: Database + ) { + invoke(bot, database, flowsUpdatesFilter, scope) + } }