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
|
2021-01-07 06:45:30 +00:00
|
|
|
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>>
|
2021-01-07 06:45:30 +00:00
|
|
|
): Job = onText(
|
2021-01-07 12:11:01 +00:00
|
|
|
includeFilterByChatInBehaviourSubContext,
|
2021-01-07 06:45:30 +00:00
|
|
|
{ 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
|
|
|
}
|
2021-01-07 06:45:30 +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)
|