mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2024-11-21 15:53:47 +00:00
CommonMessageFilterExcludeCommand and SimpleFilter.minus
This commit is contained in:
parent
fbd53e1f2d
commit
5ee87ac78d
@ -2,6 +2,10 @@
|
||||
|
||||
## 18.2.2
|
||||
|
||||
* `BehaviourBuilder`:
|
||||
* Add `CommonMessageFilterExcludeCommand` to filter commands in messages
|
||||
* Add `minus` operation for `SimpleFilter`s
|
||||
|
||||
## 18.2.1
|
||||
|
||||
* `Version`:
|
||||
|
@ -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
|
||||
}
|
||||
}
|
@ -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())
|
||||
|
@ -7,8 +7,6 @@ import dev.inmo.tgbotapi.utils.RiskFeature
|
||||
import dev.inmo.tgbotapi.utils.internal.*
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
private val commandRegex = Regex("[/!][^@\\s]*")
|
||||
|
||||
/**
|
||||
* @see botCommand
|
||||
*/
|
||||
@ -17,7 +15,7 @@ data class BotCommandTextSource @RiskFeature(DirectInvocationOfTextSourceConstru
|
||||
override val source: String
|
||||
) : TextSource {
|
||||
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 {
|
||||
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 markdownV2: String by lazy { source.commandMarkdownV2() }
|
||||
override val html: String by lazy { source.commandHTML() }
|
||||
|
||||
companion object {
|
||||
val CommandRegex = Regex("[/!][^@\\s]*")
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user