Contact.kt 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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.contact
  10. import io.ktor.utils.io.core.*
  11. import kotlinx.coroutines.CoroutineScope
  12. import net.mamoe.mirai.Bot
  13. import net.mamoe.mirai.event.events.*
  14. import net.mamoe.mirai.message.MessageReceipt
  15. import net.mamoe.mirai.message.data.*
  16. import net.mamoe.mirai.utils.ExternalResource
  17. import net.mamoe.mirai.utils.ExternalResource.Companion.uploadAsImage
  18. import net.mamoe.mirai.utils.NotStableForInheritance
  19. import net.mamoe.mirai.utils.OverFileSizeMaxException
  20. import kotlin.coroutines.cancellation.CancellationException
  21. /**
  22. * 联系对象, 即可以与 [Bot] 互动的对象. 包含 [用户][User], 和 [群][Group].
  23. */
  24. @NotStableForInheritance
  25. public actual interface Contact : ContactOrBot, CoroutineScope {
  26. /**
  27. * 这个联系对象所属 [Bot].
  28. */
  29. public actual override val bot: Bot
  30. /**
  31. * 可以是 QQ 号码或者群号码.
  32. *
  33. * @see User.id
  34. * @see Group.id
  35. */
  36. public actual override val id: Long
  37. /**
  38. * 向这个对象发送消息.
  39. *
  40. * 单条消息最大可发送 4500 字符或 50 张图片.
  41. *
  42. * @see MessagePreSendEvent 发送消息前事件
  43. * @see MessagePostSendEvent 发送消息后事件
  44. *
  45. * @throws EventCancelledException 当发送消息事件被取消时抛出
  46. * @throws BotIsBeingMutedException 发送群消息时若 [Bot] 被禁言抛出
  47. * @throws MessageTooLargeException 当消息过长时抛出
  48. * @throws IllegalArgumentException 当消息内容为空时抛出 (详见 [Message.isContentEmpty])
  49. *
  50. * @return 消息回执. 可 [引用][MessageReceipt.quote] 或 [撤回][MessageReceipt.recall] 这条消息.
  51. */
  52. public actual suspend fun sendMessage(message: Message): MessageReceipt<Contact>
  53. /**
  54. * 发送纯文本消息
  55. * @see sendMessage
  56. */
  57. public actual suspend fun sendMessage(message: String): MessageReceipt<Contact> = sendMessage(message.toPlainText())
  58. /**
  59. * 上传一个 [资源][ExternalResource] 作为图片以备发送.
  60. *
  61. * **无论上传是否成功都不会关闭 [resource]. 需要调用方手动关闭资源**
  62. *
  63. * 也可以使用其他扩展: [ExternalResource.uploadAsImage] 使用 [Input] 等上传.
  64. *
  65. * @see Image 查看有关图片的更多信息, 如上传图片
  66. *
  67. * @see BeforeImageUploadEvent 图片发送前事件, 可拦截.
  68. * @see ImageUploadEvent 图片发送完成事件, 不可拦截.
  69. *
  70. * @see ExternalResource
  71. *
  72. * @throws EventCancelledException 当发送消息事件被取消时抛出
  73. * @throws OverFileSizeMaxException 当图片文件过大而被服务器拒绝上传时抛出. (最大大小约为 20 MB, 但 mirai 限制的大小为 30 MB)
  74. */
  75. public actual suspend fun uploadImage(resource: ExternalResource): Image
  76. public actual companion object {
  77. /**
  78. * 将资源作为单独的图片消息发送给 [this]
  79. *
  80. * @see Contact.sendMessage 最终调用, 发送消息.
  81. */
  82. public actual suspend fun <C : Contact> C.sendImage(resource: ExternalResource): MessageReceipt<C> {
  83. return this.uploadImage(resource).sendTo(this)
  84. }
  85. /**
  86. * 将文件作为图片上传, 但不发送
  87. * @throws OverFileSizeMaxException
  88. */
  89. @kotlin.internal.LowPriorityInOverloadResolution
  90. @Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE", "EXTENSION_SHADOWED_BY_MEMBER")
  91. @Throws(OverFileSizeMaxException::class, CancellationException::class)
  92. public actual suspend fun Contact.uploadImage(resource: ExternalResource): Image {
  93. return uploadImage(resource)
  94. }
  95. }
  96. }