1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-24 11:08:43 +00:00

add CommonMessageFilterIncludeText

This commit is contained in:
InsanusMokrassar 2024-10-13 18:28:26 +06:00
parent 5ee87ac78d
commit be25f705ef
2 changed files with 24 additions and 12 deletions

View File

@ -1,10 +1,8 @@
package dev.inmo.tgbotapi.extensions.behaviour_builder.filters
import dev.inmo.tgbotapi.abstracts.TextedInput
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.CommonMessageFilter
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.not
import dev.inmo.tgbotapi.extensions.utils.textedContentOrNull
import dev.inmo.tgbotapi.types.message.abstracts.*
import dev.inmo.tgbotapi.types.message.content.MessageContent
import dev.inmo.tgbotapi.types.message.textsources.BotCommandTextSource
/**
@ -21,13 +19,12 @@ fun CommonMessageFilterExcludeCommand(
command: String?,
textBeginOnly: Boolean = true
): CommonMessageFilter<*> {
val regex = when {
command == null -> BotCommandTextSource.CommandRegex
textBeginOnly -> Regex("^[/!]$command(\\s|$)")
!textBeginOnly -> Regex("[/!]$command(\\s|$)")
else -> error("Unreachable code has been reached. It is error and must not happen")
}
return CommonMessageFilter {
it.content.textedContentOrNull() ?.text ?.contains(regex) == true
}
return !CommonMessageFilterIncludeText(
when {
command == null -> BotCommandTextSource.CommandRegex
textBeginOnly -> Regex("^[/!]$command(\\s|$)")
!textBeginOnly -> Regex("[/!]$command(\\s|$)")
else -> error("Unreachable code has been reached. It is error and must not happen")
}
)
}

View File

@ -0,0 +1,15 @@
package dev.inmo.tgbotapi.extensions.behaviour_builder.filters
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.CommonMessageFilter
import dev.inmo.tgbotapi.extensions.utils.textedContentOrNull
/**
* Includes messages only contains text with [textRegex]
*/
fun CommonMessageFilterIncludeText(
textRegex: Regex,
): CommonMessageFilter<*> {
return CommonMessageFilter {
it.content.textedContentOrNull() ?.text ?.contains(textRegex) == true
}
}