From 7d7658c64fa935239134aa7004d8ac364438073f Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 8 Nov 2022 22:11:44 +0600 Subject: [PATCH] add forumMessage/notForumMessage extensions --- .../utils/updates/MessageFilters.kt | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/updates/MessageFilters.kt b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/updates/MessageFilters.kt index 40d996bc6f..03963a88f4 100644 --- a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/updates/MessageFilters.kt +++ b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/updates/MessageFilters.kt @@ -2,8 +2,10 @@ package dev.inmo.tgbotapi.extensions.utils.updates import dev.inmo.tgbotapi.extensions.utils.* import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage +import dev.inmo.tgbotapi.types.message.abstracts.Message import dev.inmo.tgbotapi.types.message.content.TextContent import dev.inmo.tgbotapi.types.message.textsources.BotCommandTextSource +import dev.inmo.tgbotapi.utils.extensions.threadIdOrNull /** * A predicate to test whether a message contains any commands in its body. @@ -42,3 +44,39 @@ fun CommonMessage<*>.hasCommands(): Boolean = withContentOrNull() ? * @see hasCommands */ fun CommonMessage<*>.hasNoCommands(): Boolean = !this.hasCommands() + +/** + * A predicate to test that message has been sent in the forum. + * Use it as the `initialFilter` parameter in behaviour builder triggers. + * E.g. + * + * ```kotlin + * onContentMessage( + * initialFilter = Message::forumMessage + * ) { + * // The message here will be from forum + * } + * ``` + * + * @return true if this [Message] is from forum ([threadIdOrNull] is not null). False otherwise. + * @see notForumMessage + */ +fun Message.forumMessage(): Boolean = threadIdOrNull != null + +/** + * A predicate to test that message has not been sent in the forum. + * Use it as the `initialFilter` parameter in behaviour builder triggers. + * E.g. + * + * ```kotlin + * onContentMessage( + * initialFilter = Message::notForumMessage + * ) { + * // The message here will not be from forum + * } + * ``` + * + * @return true if this [Message] is not from forum ([threadIdOrNull] is not null). False otherwise. + * @see forumMessage + */ +fun Message.notForumMessage(): Boolean = !forumMessage()