AtAll.kt 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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("MemberVisibilityCanBePrivate")
  12. package net.mamoe.mirai.message.data
  13. import kotlinx.serialization.SerialName
  14. import kotlinx.serialization.Serializable
  15. import net.mamoe.mirai.message.code.CodableMessage
  16. import net.mamoe.mirai.utils.MiraiExperimentalApi
  17. /**
  18. * "@全体成员".
  19. *
  20. * 非会员每天只能发送 10 次 [AtAll]. 超出部分会被以普通文字看待.
  21. *
  22. * ## 使用 [AtAll]
  23. *
  24. * [AtAll] 是单例, 将 [AtAll] 实例[添加][Message.plus]到消息链中即可.
  25. * ```
  26. * // Kotlin
  27. * contact.sendMessage(AtAll + "test")
  28. *
  29. * // Java
  30. * contact.sendMessage(MessageUtils.newChain(AtAll.INSTANCE, new PlainText("test")));
  31. * ```
  32. *
  33. * ## mirai 码支持
  34. * 格式: [mirai:atall]
  35. *
  36. * @see At at 单个群成员
  37. */
  38. @SerialName(AtAll.SERIAL_NAME)
  39. @Serializable
  40. public object AtAll :
  41. MessageContent, CodableMessage {
  42. public const val display: String = "@全体成员"
  43. public const val SERIAL_NAME: String = "AtAll"
  44. @Suppress("SpellCheckingInspection")
  45. override fun contentToString(): String = display
  46. override fun toString(): String = "[mirai:atall]"
  47. override fun serializeToMiraiCode(): String = toString()
  48. override fun hashCode(): Int = display.hashCode()
  49. override fun equals(other: Any?): Boolean = other === this
  50. @MiraiExperimentalApi
  51. override fun appendMiraiCodeTo(builder: StringBuilder) {
  52. builder.append(toString())
  53. }
  54. // 自动为消息补充 " "
  55. @JvmSynthetic
  56. public override fun followedBy(tail: Message): MessageChain {
  57. if (tail is PlainText && tail.content.startsWith(' ')) {
  58. return super<MessageContent>.followedBy(tail)
  59. }
  60. return super<MessageContent>.followedBy(PlainText(" ")) + tail
  61. }
  62. }