Nudge.kt 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright 2019-2022 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. package net.mamoe.mirai.message.action
  10. import me.him188.kotlin.jvm.blocking.bridge.JvmBlockingBridge
  11. import net.mamoe.mirai.Bot
  12. import net.mamoe.mirai.Mirai
  13. import net.mamoe.mirai.contact.*
  14. import net.mamoe.mirai.event.events.NudgeEvent
  15. import net.mamoe.mirai.message.data.PokeMessage
  16. import net.mamoe.mirai.utils.BotConfiguration
  17. import net.mamoe.mirai.utils.BotConfiguration.MiraiProtocol
  18. import kotlin.jvm.JvmStatic
  19. import kotlin.jvm.JvmSynthetic
  20. /**
  21. * 一个 "戳一戳" 动作.
  22. *
  23. * 备注: 这类似微信拍一拍. 消息对话框中显示的 "一个手指" 的戳一戳是 [PokeMessage]
  24. *
  25. * 仅在手机 QQ 8.4.0 左右版本才受支持. 其他客户端会忽略这些消息.
  26. *
  27. * 示例,要机器人戳一个群员并发送到群里,使用 `member.nudge().sendTo(group)`.
  28. * 要机器人戳一个好友并发送给该好友,使用 `friend.nudge().sendTo(friend)`.
  29. *
  30. * @see UserOrBot.nudge 创建 [Nudge] 对象
  31. */
  32. public sealed class Nudge {
  33. /**
  34. * 戳的对象. 即 "A 戳了 B" 中的 "B".
  35. */
  36. public abstract val target: UserOrBot
  37. /**
  38. * 发送戳一戳消息到 [receiver].
  39. *
  40. * 需要使用支持的[协议][BotConfiguration.protocol] [MiraiProtocol.ANDROID_PHONE] 或 [MiraiProtocol.IPAD]. 自 2.10 起才支持使用 IPAD 协议发送.
  41. *
  42. * @param receiver 这条 "戳一戳" 消息的接收对象. (不是 "戳" 动作的对象, 而是接收 "A 戳了 B" 这条消息的对象)
  43. * @return 成功发送时为 `true`. 若对方禁用 "戳一戳" 功能, 返回 `false`.
  44. * @throws UnsupportedOperationException 当未使用 [ANDROID_PHONE 协议][MiraiProtocol.ANDROID_PHONE] 或 [IPAD 协议][MiraiProtocol.IPAD] 时抛出
  45. *
  46. * @see NudgeEvent 事件
  47. * @see Contact.sendNudge
  48. */
  49. @JvmBlockingBridge
  50. public suspend fun sendTo(receiver: Contact): Boolean {
  51. @Suppress("DEPRECATION_ERROR")
  52. return Mirai.sendNudge(receiver.bot, this, receiver)
  53. }
  54. public companion object {
  55. /**
  56. * 发送戳一戳消息.
  57. *
  58. * 需要使用支持的[协议][BotConfiguration.protocol] [MiraiProtocol.ANDROID_PHONE] 或 [MiraiProtocol.IPAD]. 自 2.10 起才支持使用 IPAD 协议发送.
  59. *
  60. * @return 成功发送时为 `true`. 若对方禁用 "戳一戳" 功能或今日 "戳一戳" 次数已达到上限, 返回 `false`.
  61. *
  62. * @throws UnsupportedOperationException 当未使用 [ANDROID_PHONE 协议][MiraiProtocol.ANDROID_PHONE] 或 [IPAD 协议][MiraiProtocol.IPAD] 时抛出
  63. *
  64. * @see NudgeEvent 事件
  65. */
  66. @JvmSynthetic
  67. @JvmStatic
  68. public suspend fun Contact.sendNudge(nudge: Nudge): Boolean = nudge.sendTo(this)
  69. }
  70. }
  71. /**
  72. * @see Bot.nudge
  73. * @see Nudge
  74. */
  75. public data class BotNudge(
  76. public override val target: Bot
  77. ) : Nudge()
  78. /**
  79. * @see User.nudge
  80. * @see Nudge
  81. */
  82. public sealed class UserNudge : Nudge() {
  83. public abstract override val target: UserOrBot
  84. }
  85. /**
  86. * @see Member.nudge
  87. * @see Nudge
  88. */
  89. public data class MemberNudge(
  90. public override val target: NormalMember
  91. ) : UserNudge()
  92. /**
  93. * @see Friend.nudge
  94. * @see Nudge
  95. */
  96. public data class FriendNudge(
  97. public override val target: Friend
  98. ) : UserNudge()
  99. /**
  100. * @see Stranger.nudge
  101. * @see Nudge
  102. */
  103. public data class StrangerNudge(
  104. public override val target: Stranger
  105. ) : UserNudge()