mirror of
https://github.com/mamoe/mirai.git
synced 2025-02-10 22:34:52 +08:00
Extract SingleMessage and ConstrainSingle to separate files, update docs
This commit is contained in:
parent
3a8003057a
commit
3c38a3b9c0
@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Copyright 2019-2021 Mamoe Technologies and contributors.
|
||||
*
|
||||
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
|
||||
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
|
||||
*
|
||||
* https://github.com/mamoe/mirai/blob/master/LICENSE
|
||||
*/
|
||||
|
||||
@file:Suppress(
|
||||
"MemberVisibilityCanBePrivate", "unused", "EXPERIMENTAL_API_USAGE",
|
||||
"NOTHING_TO_INLINE", "INVISIBLE_MEMBER", "INVISIBLE_REFERENCE",
|
||||
"INAPPLICABLE_JVM_NAME"
|
||||
)
|
||||
@file:JvmMultifileClass
|
||||
@file:JvmName("MessageUtils")
|
||||
|
||||
package net.mamoe.mirai.message.data
|
||||
|
||||
/**
|
||||
* 约束一个 [MessageChain] 中只存在这一种类型的元素. 新元素将会替换旧元素, 保持原顺序.
|
||||
*
|
||||
* 实现此接口的元素将会在连接时自动处理替换.
|
||||
*
|
||||
* 要获取有关键的信息, 查看 [MessageKey].
|
||||
* 要获取有关约束的处理方式, 查看 [AbstractPolymorphicMessageKey].
|
||||
*/
|
||||
public interface ConstrainSingle : SingleMessage {
|
||||
/**
|
||||
* 用于判断是否为同一种元素的 [MessageKey]. 使用多态类型 [MessageKey] 最上层的 [MessageKey].
|
||||
* @see MessageKey 查看更多信息
|
||||
*/
|
||||
public val key: MessageKey<*>
|
||||
}
|
@ -21,9 +21,7 @@ import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.fold
|
||||
import kotlinx.serialization.*
|
||||
import net.mamoe.mirai.contact.Contact
|
||||
import net.mamoe.mirai.event.events.MessageEvent
|
||||
import net.mamoe.mirai.message.MessageReceipt
|
||||
import net.mamoe.mirai.utils.safeCast
|
||||
import kotlin.internal.LowPriorityInOverloadResolution
|
||||
|
||||
/**
|
||||
@ -31,7 +29,7 @@ import kotlin.internal.LowPriorityInOverloadResolution
|
||||
*
|
||||
* [消息][Message] 分为
|
||||
* - [SingleMessage]:
|
||||
* - [MessageMetadata] 消息元数据, 即消息的属性. 包括: [消息来源][MessageSource], [引用回复][QuoteReply].
|
||||
* - [MessageMetadata] 消息元数据, 即消息的属性. 包括: [消息来源][MessageSource], [引用回复][QuoteReply] 等.
|
||||
* - [MessageContent] 含内容的消息, 包括: [纯文本][PlainText], [@群员][At], [@全体成员][AtAll] 等.
|
||||
* - [MessageChain]: 不可变消息链, 链表形式链接的多个 [SingleMessage] 实例.
|
||||
*
|
||||
@ -39,6 +37,8 @@ import kotlin.internal.LowPriorityInOverloadResolution
|
||||
*
|
||||
* 请先根据实际需求确定需要的类型.
|
||||
*
|
||||
* 特别地, 要以字符串方式处理消息, 可使用 [contentToString] 或 [content] 得到内容字符串.
|
||||
*
|
||||
*
|
||||
* - [PlainText]: 纯文本
|
||||
* - [Image]: 图片
|
||||
@ -56,7 +56,7 @@ import kotlin.internal.LowPriorityInOverloadResolution
|
||||
* ## 使用 [Message]
|
||||
*
|
||||
* ### 在 Kotlin 使用 [Message]:
|
||||
* 这与使用 [String] 的使用非常类似.
|
||||
* 与使用 [String] 的使用类似.
|
||||
*
|
||||
* - 比较 [SingleMessage] 与 [String]:
|
||||
* `if(message.content == "你好") friend.sendMessage(event)`
|
||||
@ -69,11 +69,12 @@ import kotlin.internal.LowPriorityInOverloadResolution
|
||||
* 但注意: 不能 `String + Message`. 只能 `Message + String`
|
||||
*
|
||||
*
|
||||
* ### 在 Java 使用 [Message]:
|
||||
*
|
||||
*
|
||||
* ### 发送消息
|
||||
* - 通过 [Contact] 中的成员函数: [Contact.sendMessage]
|
||||
* - 通过 [Message] 的扩展函数: [Message.sendTo]
|
||||
* - 在 [MessageEvent] 中使用 [MessageEvent.reply] 等捷径
|
||||
*
|
||||
* @see PlainText 纯文本
|
||||
* @see Image 图片
|
||||
@ -89,6 +90,8 @@ import kotlin.internal.LowPriorityInOverloadResolution
|
||||
* @see buildMessageChain 构造一个 [MessageChain]
|
||||
*
|
||||
* @see Contact.sendMessage 发送消息
|
||||
*
|
||||
* @suppress **注意:** [Message] 类型大多有隐藏的协议实现, 不能被第三方应用继承,
|
||||
*/
|
||||
public interface Message { // TODO: 2021/1/10 Make sealed interface in Kotlin 1.5
|
||||
|
||||
@ -240,71 +243,6 @@ public suspend inline operator fun Message.plus(another: Flow<Message>): Message
|
||||
another.fold(this) { acc, it -> acc + it }.toMessageChain()
|
||||
|
||||
|
||||
/**
|
||||
* 单个消息元素. 与之相对的是 [MessageChain], 是多个 [SingleMessage] 的集合.
|
||||
*/
|
||||
@Serializable(SingleMessage.Serializer::class)
|
||||
public interface SingleMessage : Message { // TODO: 2021/1/10 Make sealed interface in Kotlin 1.5
|
||||
public object Serializer : KSerializer<SingleMessage> by PolymorphicSerializer(SingleMessage::class)
|
||||
}
|
||||
|
||||
/**
|
||||
* 消息元数据, 即不含内容的元素.
|
||||
*
|
||||
* 这种类型的 [Message] 只表示一条消息的属性. 其子类为 [MessageSource], [QuoteReply] 和 [CustomMessageMetadata]
|
||||
*
|
||||
* 所有子类的 [contentToString] 都应该返回空字符串.
|
||||
*
|
||||
* 要获取详细信息, 查看 [MessageChain].
|
||||
*
|
||||
* @see MessageSource 消息源
|
||||
* @see QuoteReply 引用回复
|
||||
* @see CustomMessageMetadata 自定义元数据
|
||||
*
|
||||
* @see ConstrainSingle 约束一个 [MessageChain] 中只存在这一种类型的元素
|
||||
*/
|
||||
public interface MessageMetadata : SingleMessage { // TODO: 2021/1/10 Make sealed interface in Kotlin 1.5
|
||||
/**
|
||||
* 返回空字符串
|
||||
*/
|
||||
override fun contentToString(): String = ""
|
||||
}
|
||||
|
||||
/**
|
||||
* 约束一个 [MessageChain] 中只存在这一种类型的元素. 新元素将会替换旧元素, 保持原顺序.
|
||||
*
|
||||
* 实现此接口的元素将会在连接时自动处理替换.
|
||||
*
|
||||
* 要获取有关键的信息, 查看 [MessageKey].
|
||||
* 要获取有关约束的处理方式, 查看 [AbstractPolymorphicMessageKey].
|
||||
*/
|
||||
public interface ConstrainSingle : SingleMessage {
|
||||
/**
|
||||
* 用于判断是否为同一种元素的 [MessageKey]. 使用多态类型 [MessageKey] 最上层的 [MessageKey].
|
||||
* @see MessageKey 查看更多信息
|
||||
*/
|
||||
public val key: MessageKey<*>
|
||||
}
|
||||
|
||||
/**
|
||||
* 带内容的消息.
|
||||
*
|
||||
* @see PlainText 纯文本
|
||||
* @see At At 一个群成员.
|
||||
* @see AtAll At 全体成员
|
||||
* @see HummerMessage 一些特殊消息: [戳一戳][PokeMessage], [闪照][FlashImage]
|
||||
* @see Image 图片
|
||||
* @see RichMessage 富文本
|
||||
* @see ServiceMessage 服务消息, 如 JSON/XML
|
||||
* @see Face 原生表情
|
||||
* @see ForwardMessage 合并转发
|
||||
* @see Voice 语音
|
||||
*/
|
||||
public interface MessageContent : SingleMessage { // TODO: 2021/1/10 Make sealed interface in Kotlin 1.5
|
||||
public companion object Key : AbstractMessageKey<MessageContent>({ it.safeCast() })
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* [Message.contentToString] 的捷径
|
||||
*/
|
||||
|
@ -12,10 +12,19 @@ package net.mamoe.mirai.message.data
|
||||
import net.mamoe.mirai.utils.safeCast
|
||||
|
||||
/**
|
||||
* [MessageChain] 中包含秀图时的标记
|
||||
* [MessageChain] 中包含秀图时的标记.
|
||||
*
|
||||
* 秀图已被 QQ 弃用, 仅作识别处理
|
||||
*
|
||||
*
|
||||
* ```
|
||||
* MessageEvent event
|
||||
*
|
||||
* if (event.message.contains(ShowImageFlag)) {
|
||||
* // event.message 包含的图片是作为 '秀图' 发送
|
||||
* }
|
||||
* ```
|
||||
*
|
||||
* @since 2.2
|
||||
*/
|
||||
public object ShowImageFlag : MessageMetadata, ConstrainSingle, AbstractMessageKey<ShowImageFlag>({ it.safeCast() }) {
|
@ -0,0 +1,78 @@
|
||||
/*
|
||||
* Copyright 2019-2021 Mamoe Technologies and contributors.
|
||||
*
|
||||
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
|
||||
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
|
||||
*
|
||||
* https://github.com/mamoe/mirai/blob/master/LICENSE
|
||||
*/
|
||||
|
||||
@file:Suppress(
|
||||
"MemberVisibilityCanBePrivate", "unused", "EXPERIMENTAL_API_USAGE",
|
||||
"NOTHING_TO_INLINE", "INVISIBLE_MEMBER", "INVISIBLE_REFERENCE",
|
||||
"INAPPLICABLE_JVM_NAME"
|
||||
)
|
||||
@file:JvmMultifileClass
|
||||
@file:JvmName("MessageUtils")
|
||||
|
||||
package net.mamoe.mirai.message.data
|
||||
|
||||
import kotlinx.serialization.KSerializer
|
||||
import kotlinx.serialization.PolymorphicSerializer
|
||||
import kotlinx.serialization.Serializable
|
||||
import net.mamoe.mirai.utils.safeCast
|
||||
|
||||
/**
|
||||
* 单个消息元素. 与之相对的是 [MessageChain], 是多个 [SingleMessage] 的集合.
|
||||
*/
|
||||
@Serializable(SingleMessage.Serializer::class)
|
||||
public interface SingleMessage : Message { // TODO: 2021/1/10 Make sealed interface in Kotlin 1.5
|
||||
public object Serializer : KSerializer<SingleMessage> by PolymorphicSerializer(SingleMessage::class)
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 消息元数据, 即不含内容的元素.
|
||||
*
|
||||
* 这种类型的 [Message] 只表示一条消息的属性. 其子类为 [MessageSource], [QuoteReply] 和 [CustomMessageMetadata]
|
||||
*
|
||||
* 所有子类的 [contentToString] 都应该返回空字符串.
|
||||
*
|
||||
* 要获取详细信息, 查看 [MessageChain].
|
||||
*
|
||||
* @see MessageSource 消息源
|
||||
* @see QuoteReply 引用回复
|
||||
* @see CustomMessageMetadata 自定义元数据
|
||||
* @see ShowImageFlag 秀图标识
|
||||
*
|
||||
* @see ConstrainSingle 约束一个 [MessageChain] 中只存在这一种类型的元素
|
||||
*/
|
||||
public interface MessageMetadata : SingleMessage { // TODO: 2021/1/10 Make sealed interface in Kotlin 1.5
|
||||
/**
|
||||
* 返回空字符串
|
||||
*/
|
||||
override fun contentToString(): String = ""
|
||||
}
|
||||
|
||||
/**
|
||||
* 带内容的消息.
|
||||
*
|
||||
* @see PlainText 纯文本
|
||||
* @see At At 一个群成员.
|
||||
* @see AtAll At 全体成员
|
||||
* @see HummerMessage 一些特殊消息: [戳一戳][PokeMessage], [闪照][FlashImage]
|
||||
* @see Image 图片
|
||||
* @see RichMessage 富文本
|
||||
* @see ServiceMessage 服务消息, 如 JSON/XML
|
||||
* @see Face 原生表情
|
||||
* @see ForwardMessage 合并转发
|
||||
* @see Voice 语音
|
||||
* @see MarketFace 商城表情
|
||||
* @see MusicShare 音乐分享
|
||||
*/
|
||||
public interface MessageContent : SingleMessage { // TODO: 2021/1/10 Make sealed interface in Kotlin 1.5
|
||||
public companion object Key : AbstractMessageKey<MessageContent>({ it.safeCast() })
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user