At.kt 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. @file:JvmMultifileClass
  10. @file:JvmName("MessageUtils")
  11. @file:Suppress("EXPERIMENTAL_API_USAGE", "NOTHING_TO_INLINE")
  12. package net.mamoe.mirai.message.data
  13. import kotlinx.serialization.SerialName
  14. import kotlinx.serialization.Serializable
  15. import net.mamoe.mirai.contact.Group
  16. import net.mamoe.mirai.contact.Member
  17. import net.mamoe.mirai.contact.UserOrBot
  18. import net.mamoe.mirai.contact.nameCardOrNick
  19. import net.mamoe.mirai.message.code.CodableMessage
  20. import net.mamoe.mirai.message.data.visitor.MessageVisitor
  21. import net.mamoe.mirai.utils.MiraiExperimentalApi
  22. import net.mamoe.mirai.utils.MiraiInternalApi
  23. import kotlin.jvm.JvmMultifileClass
  24. import kotlin.jvm.JvmName
  25. import kotlin.jvm.JvmSynthetic
  26. /**
  27. * At 一个群成员. 只能发送给一个群.
  28. *
  29. * 使用时直接构造 At 实例即可.
  30. *
  31. * ## mirai 码支持
  32. * 格式: [mirai:at:*[target]*]
  33. *
  34. * @see AtAll 全体成员
  35. */
  36. @SerialName(At.SERIAL_NAME)
  37. @Serializable
  38. public data class At(
  39. public val target: Long,
  40. ) : MessageContent, CodableMessage {
  41. public override fun toString(): String = "[mirai:at:$target]"
  42. public override fun contentToString(): String = "@$target"
  43. /**
  44. * 获取 [At] 发送于指定 [Group] 时会显示的内容.
  45. *
  46. * 若 [group] 非 `null` 且包含成员 [target], 返回 `"@成员名片或昵称"`. 否则返回 `"@123456"` 其中 123456 表示 [target]
  47. */
  48. public fun getDisplay(group: Group?): String {
  49. val member = group?.get(this.target) ?: return "@$target"
  50. return "@${member.nameCardOrNick}"
  51. }
  52. @MiraiExperimentalApi
  53. override fun appendMiraiCodeTo(builder: StringBuilder) {
  54. builder.append("[mirai:at:").append(target).append(']')
  55. }
  56. public companion object {
  57. public const val SERIAL_NAME: String = "At"
  58. }
  59. // 自动为消息补充 " "
  60. public override fun followedBy(tail: Message): MessageChain {
  61. if (tail is PlainText && tail.content.startsWith(' ')) {
  62. return super<MessageContent>.followedBy(tail)
  63. }
  64. return super<MessageContent>.followedBy(PlainText(" ")) + tail
  65. }
  66. @MiraiInternalApi
  67. override fun <D, R> accept(visitor: MessageVisitor<D, R>, data: D): R {
  68. return visitor.visitAt(this, data)
  69. }
  70. }
  71. /**
  72. * 构造 [At]
  73. *
  74. * @see At
  75. * @see Member.at
  76. */
  77. @JvmSynthetic
  78. public inline fun At(user: UserOrBot): At = At(user.id)
  79. /**
  80. * At 这个成员
  81. */
  82. @JvmSynthetic
  83. public inline fun Member.at(): At = At(this)