mirror of
https://github.com/InsanusMokrassar/PlaguBot.git
synced 2024-11-22 07:33:46 +00:00
BehaviourContext.invoke
This commit is contained in:
parent
bc5737d435
commit
595102fede
@ -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
|
||||
|
||||
|
@ -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) }
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user