2020-10-04 11:06:30 +00:00
|
|
|
package dev.inmo.tgbotapi.types.MessageEntity.textsources
|
2019-12-11 08:06:09 +00:00
|
|
|
|
2021-01-09 14:59:37 +00:00
|
|
|
import dev.inmo.tgbotapi.CommonAbstracts.DirectInvocationOfTextSourceConstructor
|
|
|
|
import dev.inmo.tgbotapi.utils.RiskFeature
|
2020-11-07 07:42:10 +00:00
|
|
|
import dev.inmo.tgbotapi.utils.internal.*
|
2021-04-12 17:23:18 +00:00
|
|
|
import kotlinx.serialization.Serializable
|
2019-12-11 08:06:09 +00:00
|
|
|
|
|
|
|
private val commandRegex = Regex("[/!][^@\\s]*")
|
|
|
|
|
2020-11-06 12:52:59 +00:00
|
|
|
/**
|
|
|
|
* @see botCommand
|
|
|
|
*/
|
2021-04-12 17:23:18 +00:00
|
|
|
@Serializable
|
2020-11-06 12:52:59 +00:00
|
|
|
data class BotCommandTextSource @RiskFeature(DirectInvocationOfTextSourceConstructor) constructor (
|
|
|
|
override val source: String
|
|
|
|
) : TextSource {
|
2019-12-11 08:06:09 +00:00
|
|
|
val command: String by lazy {
|
2020-01-05 13:24:17 +00:00
|
|
|
commandRegex.find(source) ?.value ?.substring(1) ?: source.substring(1)// skip first symbol like "/" or "!"
|
|
|
|
}
|
|
|
|
|
2020-11-16 07:32:20 +00:00
|
|
|
override val markdown: String by lazy { source.commandMarkdown() }
|
|
|
|
override val markdownV2: String by lazy { source.commandMarkdownV2() }
|
|
|
|
override val html: String by lazy { source.commandHTML() }
|
2019-12-11 08:06:09 +00:00
|
|
|
}
|
2020-11-06 12:52:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param command Without leading "/"
|
|
|
|
*/
|
|
|
|
@Suppress("NOTHING_TO_INLINE")
|
|
|
|
inline fun botCommand(command: String) = BotCommandTextSource("/$command")
|