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`
* `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

View File

@ -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) }
}
}

View File

@ -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)
}
}

View File

@ -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<BotCommand> = 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)
}
}