BehaviourContext.invoke

This commit is contained in:
InsanusMokrassar 2021-01-10 15:21:31 +06:00
parent bc5737d435
commit 595102fede
4 changed files with 21 additions and 20 deletions

View File

@ -5,6 +5,9 @@
* `Versions` * `Versions`
* `tgbotapi`: `0.30.10` -> `0.31.0` * `tgbotapi`: `0.30.10` -> `0.31.0`
* `microutils`: `0.4.11` -> `0.4.16` * `microutils`: `0.4.11` -> `0.4.16`
* `Plugin`
* New method `BehaviourContext#invoke`
* Old method `invoke` has been deprecated
## 0.0.5 ## 0.0.5

View File

@ -5,6 +5,7 @@ import dev.inmo.plagubot.config.Config
import dev.inmo.plagubot.config.configSerialFormat import dev.inmo.plagubot.config.configSerialFormat
import dev.inmo.tgbotapi.bot.Ktor.telegramBot import dev.inmo.tgbotapi.bot.Ktor.telegramBot
import dev.inmo.tgbotapi.extensions.api.bot.setMyCommands 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.extensions.utils.updates.retrieving.longPolling
import dev.inmo.tgbotapi.types.botCommandsLimit import dev.inmo.tgbotapi.types.botCommandsLimit
import kotlinx.coroutines.* import kotlinx.coroutines.*
@ -17,9 +18,9 @@ suspend inline fun initPlaguBot(
) { ) {
val bot = telegramBot(config.botToken) val bot = telegramBot(config.botToken)
bot.longPolling(scope = scope) { bot.buildBehaviour(scope) {
val commands = config.plugins.flatMap { val commands = config.plugins.flatMap {
it.invoke(bot, config.database.database, this, scope) it.apply { invoke(config.database.database) }
it.getCommands() it.getCommands()
}.let { }.let {
val futureUnavailable = it.drop(botCommandsLimit.last) val futureUnavailable = it.drop(botCommandsLimit.last)
@ -28,11 +29,7 @@ suspend inline fun initPlaguBot(
} }
it.take(botCommandsLimit.last) it.take(botCommandsLimit.last)
} }
scope.launch { safelyWithoutExceptions { setMyCommands(commands) }
safelyWithoutExceptions {
bot.setMyCommands(commands)
}
}
} }
} }

View File

@ -1,8 +1,6 @@
package dev.inmo.plagubot package dev.inmo.plagubot
import dev.inmo.tgbotapi.bot.TelegramBot import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext
import dev.inmo.tgbotapi.updateshandlers.FlowsUpdatesFilter
import kotlinx.coroutines.CoroutineScope
import kotlinx.serialization.SerialName import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
import org.jetbrains.exposed.sql.Database import org.jetbrains.exposed.sql.Database
@ -12,12 +10,7 @@ import org.jetbrains.exposed.sql.Database
data class HelloPlugin( data class HelloPlugin(
val parameter: String val parameter: String
) : Plugin { ) : Plugin {
override suspend fun invoke( override suspend fun BehaviourContext.invoke(database: Database) {
bot: TelegramBot,
database: Database,
updatesFilter: FlowsUpdatesFilter,
scope: CoroutineScope
) {
println(parameter) println(parameter)
} }
} }

View File

@ -1,6 +1,7 @@
package dev.inmo.plagubot package dev.inmo.plagubot
import dev.inmo.tgbotapi.bot.TelegramBot import dev.inmo.tgbotapi.bot.TelegramBot
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext
import dev.inmo.tgbotapi.types.BotCommand import dev.inmo.tgbotapi.types.BotCommand
import dev.inmo.tgbotapi.updateshandlers.FlowsUpdatesFilter import dev.inmo.tgbotapi.updateshandlers.FlowsUpdatesFilter
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
@ -19,13 +20,20 @@ interface Plugin {
*/ */
suspend fun getCommands(): List<BotCommand> = emptyList() suspend fun getCommands(): List<BotCommand> = emptyList()
/** @Deprecated("Override other method with receiver BehaviourContext")
* This method (usually) will be invoked just one time in the whole application.
*/
suspend operator fun invoke( suspend operator fun invoke(
bot: TelegramBot, bot: TelegramBot,
database: Database, database: Database,
updatesFilter: FlowsUpdatesFilter, updatesFilter: FlowsUpdatesFilter,
scope: CoroutineScope 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)
}
} }