From 7488eb9d4b4e4355252ac62123800e2d63054710 Mon Sep 17 00:00:00 2001 From: d1snin Date: Thu, 13 Oct 2022 19:56:26 +0300 Subject: [PATCH] added `botCommand` text source factory function supporting `BotCommand` --- .../message/textsources/BotCommandTextSource.kt | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/textsources/BotCommandTextSource.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/textsources/BotCommandTextSource.kt index bb143c5fe0..8dbbfe804e 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/textsources/BotCommandTextSource.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/textsources/BotCommandTextSource.kt @@ -1,7 +1,8 @@ package dev.inmo.tgbotapi.types.message.textsources -import dev.inmo.tgbotapi.types.usernameRegex +import dev.inmo.tgbotapi.types.BotCommand import dev.inmo.tgbotapi.types.Username +import dev.inmo.tgbotapi.types.usernameRegex import dev.inmo.tgbotapi.utils.RiskFeature import dev.inmo.tgbotapi.utils.internal.* import kotlinx.serialization.Serializable @@ -12,14 +13,14 @@ private val commandRegex = Regex("[/!][^@\\s]*") * @see botCommand */ @Serializable -data class BotCommandTextSource @RiskFeature(DirectInvocationOfTextSourceConstructor) constructor ( +data class BotCommandTextSource @RiskFeature(DirectInvocationOfTextSourceConstructor) constructor( 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) + Username(usernameRegex.find(source)?.value ?: return@lazy null) } override val markdown: String by lazy { source.commandMarkdown() } @@ -32,3 +33,6 @@ data class BotCommandTextSource @RiskFeature(DirectInvocationOfTextSourceConstru */ @Suppress("NOTHING_TO_INLINE") inline fun botCommand(command: String) = BotCommandTextSource("/$command") + +@Suppress("NOTHING_TO_INLINE") +inline fun botCommand(botCommand: BotCommand) = botCommand(botCommand.command) \ No newline at end of file