MessageReceipt.kt 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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/dev/LICENSE
  8. */
  9. @file:Suppress("NOTHING_TO_INLINE", "FunctionName", "unused")
  10. @file:JvmBlockingBridge
  11. package net.mamoe.mirai.message
  12. import me.him188.kotlin.jvm.blocking.bridge.JvmBlockingBridge
  13. import net.mamoe.mirai.Bot
  14. import net.mamoe.mirai.IMirai
  15. import net.mamoe.mirai.Mirai
  16. import net.mamoe.mirai.contact.*
  17. import net.mamoe.mirai.message.action.AsyncRecallResult
  18. import net.mamoe.mirai.message.data.*
  19. import net.mamoe.mirai.message.data.MessageSource.Key.quote
  20. import net.mamoe.mirai.message.data.MessageSource.Key.recallIn
  21. import net.mamoe.mirai.utils.MiraiInternalApi
  22. import net.mamoe.mirai.utils.NotStableForInheritance
  23. /**
  24. * 发送消息后得到的回执. 可用于撤回, 引用回复等.
  25. *
  26. * @param source 指代发送出去的消息
  27. * @param target 消息发送对象
  28. *
  29. * @see quote 引用这条消息. 即引用机器人自己发出去的消息
  30. * @see quoteReply 引用并回复这条消息.
  31. * @see recallMessage 撤回这条消息
  32. *
  33. * @see Group.sendMessage 发送群消息, 返回回执(此对象)
  34. * @see User.sendMessage 发送群消息, 返回回执(此对象)
  35. * @see Member.sendMessage 发送临时消息, 返回回执(此对象)
  36. *
  37. * @see MessageReceipt.sourceIds 源 ids
  38. * @see MessageReceipt.sourceTime 源时间
  39. */
  40. @NotStableForInheritance
  41. public open class MessageReceipt<out C : Contact>
  42. @MiraiInternalApi
  43. @Deprecated("Do not call it directly", level = DeprecationLevel.ERROR)
  44. constructor(
  45. /**
  46. * 指代发送出去的消息.
  47. */
  48. public val source: OnlineMessageSource.Outgoing,
  49. /**
  50. * 发送目标, 为 [Group] 或 [Friend] 或 [Member]
  51. */
  52. public val target: C,
  53. ) {
  54. /**
  55. * 是否为发送给群的消息的回执
  56. */
  57. public val isToGroup: Boolean get() = target is Group
  58. /**
  59. * 撤回这条消息.
  60. *
  61. * @see IMirai.recallMessage
  62. */
  63. public suspend inline fun recall() {
  64. return Mirai.recallMessage(target.bot, source)
  65. }
  66. /**
  67. * 在一段时间后撤回这条消息.
  68. *
  69. * @see IMirai.recallMessage
  70. */
  71. @Suppress("DeferredIsResult")
  72. public fun recallIn(millis: Long): AsyncRecallResult = this.source.recallIn(millis)
  73. /**
  74. * 引用这条消息.
  75. * @see MessageSource.quote 引用一条消息
  76. */
  77. public fun quote(): QuoteReply = this.source.quote()
  78. /**
  79. * 引用这条消息并回复.
  80. * @see MessageSource.quote 引用一条消息
  81. */
  82. public suspend inline fun quoteReply(message: Message): MessageReceipt<C> {
  83. @Suppress("UNCHECKED_CAST")
  84. return target.sendMessage(this.quote() + message) as MessageReceipt<C>
  85. }
  86. /**
  87. * 引用这条消息并回复.
  88. * @see MessageSource.quote 引用一条消息
  89. */
  90. public suspend inline fun quoteReply(message: String): MessageReceipt<C> {
  91. return this.quoteReply(PlainText(message))
  92. }
  93. public companion object
  94. }
  95. /**
  96. * 获取相关 [Bot]
  97. */
  98. public inline val MessageReceipt<*>.bot: Bot
  99. get() = target.bot
  100. /**
  101. * 获取源消息 [MessageSource.ids]
  102. */
  103. public inline val MessageReceipt<*>.sourceIds: IntArray
  104. get() = this.source.ids
  105. /**
  106. * 获取源消息 [MessageSource.internalIds]
  107. */
  108. public inline val MessageReceipt<*>.sourceInternalIds: IntArray
  109. get() = this.source.internalIds
  110. /**
  111. * 获取源消息 [MessageSource.time]
  112. */
  113. public inline val MessageReceipt<*>.sourceTime: Int
  114. get() = this.source.time
  115. /**
  116. * 获取源消息 [MessageSource.originalMessage]
  117. */
  118. public inline val MessageReceipt<*>.sourceMessage: MessageChain
  119. get() = this.source.originalMessage