1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-06-26 11:38:09 +00:00
tgbotapi/tgbotapi.extensions.steps/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/steps/triggers_handling/CommandHandling.kt

36 lines
1.5 KiB
Kotlin
Raw Normal View History

2021-01-06 17:06:48 +00:00
package dev.inmo.tgbotapi.extensions.steps.triggers_handling
import dev.inmo.tgbotapi.CommonAbstracts.textSources
import dev.inmo.tgbotapi.extensions.steps.*
import dev.inmo.tgbotapi.extensions.utils.*
import dev.inmo.tgbotapi.types.message.abstracts.ContentMessage
import dev.inmo.tgbotapi.types.message.content.TextContent
import kotlinx.coroutines.Job
2021-01-06 17:06:48 +00:00
2021-01-07 12:11:01 +00:00
suspend fun BehaviourContext.command(
2021-01-06 17:06:48 +00:00
commandRegex: Regex,
requireOnlyCommandInMessage: Boolean = true,
2021-01-07 12:11:01 +00:00
includeFilterByChatInBehaviourSubContext: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ContentMessage<TextContent>>
): Job = onText(
2021-01-07 12:11:01 +00:00
includeFilterByChatInBehaviourSubContext,
{ message ->
val content = message.content
val textSources = content.textSources
val sizeRequirement = if (requireOnlyCommandInMessage) {
textSources.size == 1
} else {
true
2021-01-06 17:06:48 +00:00
}
sizeRequirement && textSources.any { commandRegex.matches(it.asBotCommandTextSource() ?.command ?: return@any false) }
},
scenarioReceiver
)
2021-01-06 17:06:48 +00:00
2021-01-07 12:11:01 +00:00
suspend inline fun BehaviourContext.onCommand(
commandRegex: Regex,
requireOnlyCommandInMessage: Boolean = true,
includeFilterByChatInBehaviourSubContext: Boolean = true,
noinline scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ContentMessage<TextContent>>
): Job = command(commandRegex, requireOnlyCommandInMessage, includeFilterByChatInBehaviourSubContext, scenarioReceiver)