QuoteReply.kt 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * Copyright 2019-2021 Mamoe Technologies and contributors.
  3. *
  4. * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
  5. * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
  6. *
  7. * https://github.com/mamoe/mirai/blob/master/LICENSE
  8. */
  9. @file:JvmMultifileClass
  10. @file:JvmName("MessageUtils")
  11. @file:Suppress("NOTHING_TO_INLINE", "unused")
  12. package net.mamoe.mirai.message.data
  13. import kotlinx.serialization.SerialName
  14. import kotlinx.serialization.Serializable
  15. import net.mamoe.mirai.message.data.MessageSource.Key.quote
  16. import net.mamoe.mirai.message.data.MessageSource.Key.recall
  17. import net.mamoe.mirai.utils.safeCast
  18. /**
  19. * 引用回复. [QuoteReply] 被作为 [MessageMetadata], 因为它不包含实际的消息内容.
  20. *
  21. * 支持引用任何一条消息发送给任何人.
  22. *
  23. * 引用回复的原消息内容完全由 [source] 中 [MessageSource.originalMessage] 控制, 客户端不会自行寻找原消息.
  24. * 可通过 [MessageSource.copyAmend] 修改引用的消息内容.
  25. *
  26. * 客户端通过 [MessageSource.ids] 等数据定位源消息, 在修改时使用 [MessageSourceBuilder.metadata] 可以修改定位结果.
  27. *
  28. * ## 创建引用回复
  29. * - 直接构造 [QuoteReply]: `new QuoteReply(source)`
  30. * - 在 Kotlin 使用扩展 [MessageSource.quote]
  31. *
  32. * @see MessageSource 获取有关消息源的更多信息
  33. */
  34. @Serializable
  35. @SerialName(QuoteReply.SERIAL_NAME)
  36. public data class QuoteReply(
  37. /**
  38. * 指代被引用的消息. 其中 [MessageSource.originalMessage] 可以控制客户端显示的消息内容.
  39. */
  40. public val source: MessageSource
  41. ) : Message, MessageMetadata, ConstrainSingle {
  42. /**
  43. * 从消息链中获取 [MessageSource] 并构造.
  44. */
  45. public constructor(sourceMessage: MessageChain) : this(sourceMessage.getOrFail(MessageSource))
  46. public override val key: MessageKey<QuoteReply> get() = Key
  47. public override fun toString(): String =
  48. "[mirai:quote:${source.ids.contentToString()},${source.internalIds.contentToString()}]"
  49. public override fun equals(other: Any?): Boolean = other is QuoteReply && other.source == this.source
  50. public override fun hashCode(): Int = source.hashCode()
  51. public companion object Key : AbstractMessageKey<QuoteReply>({ it.safeCast() }) {
  52. public const val SERIAL_NAME: String = "QuoteReply"
  53. }
  54. }
  55. /**
  56. * 撤回引用的源消息
  57. */
  58. @JvmSynthetic
  59. public suspend inline fun QuoteReply.recallSource(): Unit = this.source.recall()