1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2025-09-03 07:09:23 +00:00

CommonMessageFilterExcludeCommand and SimpleFilter.minus

This commit is contained in:
2024-10-13 18:23:45 +06:00
parent fbd53e1f2d
commit 5ee87ac78d
4 changed files with 53 additions and 3 deletions

View File

@@ -0,0 +1,33 @@
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.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
/**
* Use as initialFilter. Will exclude messages with [command] if it is not null, if null - all messages with commands.
* If [textBeginOnly] set to false, all commands inside of message will be taken in attention.
*
* **Command line starts with `/` or `!`**
*
* @param command Pass non-null value to search specific command or null to search any command
* @param textBeginOnly Pass true (default) to check only start of message. Pass false to search in whole text of
* content
*/
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
}
}

View File

@@ -59,3 +59,14 @@ infix operator fun <T> SimpleFilter<T>?.plus(other: SimpleFilter<T>?): SimpleFil
operator fun <T> SimpleFilter<T>.not() = SimpleFilter<T> {
!this(it)
}
/**
* Works as [not]
*/
operator fun <T> SimpleFilter<T>.unaryMinus() = not()
/**
* Making +! operation. In fact that means that [other] will be inversed with [not] and that added to [this] via
* [plus]
*/
operator fun <T> SimpleFilter<T>?.minus(other: SimpleFilter<T>?): SimpleFilter<T> = this + (other ?.not())