1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-22 00:03:48 +00:00

CommonMessageFilterExcludeCommand and SimpleFilter.minus

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

View File

@ -2,6 +2,10 @@
## 18.2.2 ## 18.2.2
* `BehaviourBuilder`:
* Add `CommonMessageFilterExcludeCommand` to filter commands in messages
* Add `minus` operation for `SimpleFilter`s
## 18.2.1 ## 18.2.1
* `Version`: * `Version`:

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> { operator fun <T> SimpleFilter<T>.not() = SimpleFilter<T> {
!this(it) !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())

View File

@ -7,8 +7,6 @@ import dev.inmo.tgbotapi.utils.RiskFeature
import dev.inmo.tgbotapi.utils.internal.* import dev.inmo.tgbotapi.utils.internal.*
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
private val commandRegex = Regex("[/!][^@\\s]*")
/** /**
* @see botCommand * @see botCommand
*/ */
@ -17,7 +15,7 @@ data class BotCommandTextSource @RiskFeature(DirectInvocationOfTextSourceConstru
override val source: String override val source: String
) : TextSource { ) : TextSource {
val command: String by lazy { val command: String by lazy {
commandRegex.find(source) ?.value ?.substring(1) ?: source.substring(1)// skip first symbol like "/" or "!" CommandRegex.find(source) ?.value ?.substring(1) ?: source.substring(1)// skip first symbol like "/" or "!"
} }
val username: Username? by lazy { val username: Username? by lazy {
Username(usernameRegex.find(source) ?.value ?: return@lazy null) Username(usernameRegex.find(source) ?.value ?: return@lazy null)
@ -26,6 +24,10 @@ data class BotCommandTextSource @RiskFeature(DirectInvocationOfTextSourceConstru
override val markdown: String by lazy { source.commandMarkdown() } override val markdown: String by lazy { source.commandMarkdown() }
override val markdownV2: String by lazy { source.commandMarkdownV2() } override val markdownV2: String by lazy { source.commandMarkdownV2() }
override val html: String by lazy { source.commandHTML() } override val html: String by lazy { source.commandHTML() }
companion object {
val CommandRegex = Regex("[/!][^@\\s]*")
}
} }
/** /**