mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-28 01:10:17 +08:00
Rename toMiraiCode to serializeToMiraiCode;
Rename parseFromMiraiCode to deserializeFromMiraiCode
This commit is contained in:
parent
78bc30ae5d
commit
b41a742ece
@ -38,11 +38,6 @@
|
||||
|
||||
Mirai 支持富文本消息。各类型消息元素如下文表格所示。
|
||||
|
||||
消息拥有三种转换到字符串的表示方式。
|
||||
- `toMiraiCode()`: 消息的一种序列化方式,格式为 `[mirai:TYPE:PROP]`,其中 `TYPE` 为消息类型, `PROP` 为属性。
|
||||
- `contentToSting()`: QQ 对话框中以纯文本方式会显示的消息内容。无法用纯文字表示的消息会丢失信息,如图片总是 `[图片]`。
|
||||
- `toString()`: Java 对象的 `toString()`,会尽可能包含多的信息用于调试作用,**行为可能不确定**。
|
||||
|
||||
[`PlainText`]: ../mirai-core-api/src/commonMain/kotlin/message/data/At.kt
|
||||
[`At`]: ../mirai-core-api/src/commonMain/kotlin/message/data/At.kt
|
||||
[`AtAll`]: ../mirai-core-api/src/commonMain/kotlin/message/data/AtAll.kt
|
||||
@ -60,23 +55,23 @@ Mirai 支持富文本消息。各类型消息元素如下文表格所示。
|
||||
[`Voice`]: ../mirai-core-api/src/commonMain/kotlin/message/data/Voice.kt
|
||||
[`ForwardMessage`]: ../mirai-core-api/src/commonMain/kotlin/message/data/ForwardMessage.kt
|
||||
|
||||
| 消息类型 | 属性 | 解释 | `contentToString()` |
|
||||
|:------------------------:|:--------------------------------------------|:--------------------|:-----------------------------------|
|
||||
| [`PlainText`] | `content: String` | 纯文本 | `$content` |
|
||||
| [`Image`] | `imageId: String` | 自定义图片 | `[图片]` |
|
||||
| [`At`] | `target: Int` | 提及某人 | `@$target` |
|
||||
| [`AtAll`] | | 提及全体成员 | `@全体成员` |
|
||||
| [`Face`] | `id: Int` | 原生表情 | `[表情对应的中文名]` |
|
||||
| [`FlashImage`] | `image: Image` | 闪照 | `[闪照]` |
|
||||
| [`PokeMessage`] | `name: String`, `pokeType: Int` , `id: Int` | 戳一戳消息(消息非动作) | `[戳一戳]` |
|
||||
| [`VipFace`] | `kind: VipFace.Kind`, `count: Int` | VIP 表情 | `[${kind.name}]x$count` |
|
||||
| [`LightApp`] | `content: String` | 小程序 | `$content` |
|
||||
| [`Voice`] | `content: String` | 语音 | `$content` |
|
||||
| [`MarketFace`] | `id: Int, name: String` | 商城表情 | `[表情对应的中文名]` |
|
||||
| [`MessageSource`] | ... | 消息来源元数据 | *空字符串* |
|
||||
| [`QuoteReply`] | `source: MessageSource` | 引用回复 | *空字符串* |
|
||||
| [`ForwardMessage`] | ... | 合并转发 | *`[mirai:forward:NOT_IMPLEMENTED]` |
|
||||
| [`SimpleServiceMessage`] | `serviceId: Int, content: String` | (不稳定)服务消息 | `$content` |
|
||||
| 消息类型 | 属性 | 解释 | `contentToString()` |
|
||||
|:------------------------:|:--------------------------------------------|:--------------------|:------------------------|
|
||||
| [`PlainText`] | `content: String` | 纯文本 | `$content` |
|
||||
| [`Image`] | `imageId: String` | 自定义图片 | `[图片]` |
|
||||
| [`At`] | `target: Int` | 提及某人 | `@$target` |
|
||||
| [`AtAll`] | | 提及全体成员 | `@全体成员` |
|
||||
| [`Face`] | `id: Int` | 原生表情 | `[表情对应的中文名]` |
|
||||
| [`FlashImage`] | `image: Image` | 闪照 | `[闪照]` |
|
||||
| [`PokeMessage`] | `name: String`, `pokeType: Int` , `id: Int` | 戳一戳消息(消息非动作) | `[戳一戳]` |
|
||||
| [`VipFace`] | `kind: VipFace.Kind`, `count: Int` | VIP 表情 | `[${kind.name}]x$count` |
|
||||
| [`LightApp`] | `content: String` | 小程序 | `$content` |
|
||||
| [`Voice`] | `content: String` | 语音 | `$content` |
|
||||
| [`MarketFace`] | `id: Int, name: String` | 商城表情 | `[表情对应的中文名]` |
|
||||
| [`MessageSource`] | ... | 消息来源元数据 | *空字符串* |
|
||||
| [`QuoteReply`] | `source: MessageSource` | 引用回复 | *空字符串* |
|
||||
| [`ForwardMessage`] | ... | 合并转发 | *`[转发消息]` |
|
||||
| [`SimpleServiceMessage`] | `serviceId: Int, content: String` | (不稳定)服务消息 | `$content` |
|
||||
|
||||
***注意:内容会首先被转义,详见 [转义规则](#转义规则)***
|
||||
|
||||
@ -84,6 +79,11 @@ Mirai 支持富文本消息。各类型消息元素如下文表格所示。
|
||||
|
||||
## Mirai 码
|
||||
|
||||
消息拥有三种转换到字符串的表示方式。
|
||||
- `serializeToMiraiCode()`: 消息的一种序列化方式,格式为 `[mirai:TYPE:PROP]`,其中 `TYPE` 为消息类型, `PROP` 为属性。
|
||||
- `contentToSting()`: QQ 对话框中以纯文本方式会显示的消息内容。无法用纯文字表示的消息会丢失信息,如图片总是 `[图片]`。
|
||||
- `toString()`: Java 对象的 `toString()`,会尽可能包含多的信息用于调试作用,**行为可能不确定**。
|
||||
|
||||
实现了接口 `CodableMessage` 的消息类型支持 mirai 码表示。
|
||||
|
||||
### 转义规则
|
||||
@ -106,20 +106,20 @@ mirai 码内的属性字符串会被转义。
|
||||
```
|
||||
val chain = messageChainOf(PlainText("plain"), At(123), AtAll)
|
||||
|
||||
chain.toMiraiCode() // "plain[mirai:at:123][mirai:atall]"
|
||||
chain.serializeToMiraiCode() // "plain[mirai:at:123][mirai:atall]"
|
||||
```
|
||||
|
||||
### 由 `CodableMessage` 取得 mirai 码字符串
|
||||
|
||||
通过 `CodableMessage.toMiraiCode()`。
|
||||
通过 `CodableMessage.serializeToMiraiCode()`。
|
||||
|
||||
```
|
||||
val at = At(123)
|
||||
|
||||
at.toMiraiCode() // 结果为 `[mirai:at:123]`
|
||||
at.serializeToMiraiCode() // 结果为 `[mirai:at:123]`
|
||||
```
|
||||
|
||||
| 消息类型 | `toMiraiCode()` |
|
||||
| 消息类型 | `serializeToMiraiCode()` |
|
||||
|:------------------------:|:-------------------------------------------------|
|
||||
| [`PlainText`] | `$content` |
|
||||
| [`Image`] | `[mirai:image:$imageId]` |
|
||||
|
@ -9,15 +9,16 @@
|
||||
|
||||
package net.mamoe.mirai.message.code
|
||||
|
||||
import net.mamoe.mirai.message.code.MiraiCode.parseMiraiCode
|
||||
import net.mamoe.mirai.message.code.MiraiCode.deserializeMiraiCode
|
||||
import net.mamoe.mirai.message.data.*
|
||||
import net.mamoe.mirai.utils.MiraiExperimentalApi
|
||||
import net.mamoe.mirai.utils.PlannedRemoval
|
||||
|
||||
|
||||
/**
|
||||
* 可以使用 mirai 码表示的 [Message] 类型.
|
||||
*
|
||||
* 从字符串解析 mirai 码:[MiraiCode.parseMiraiCode]
|
||||
* 从字符串解析 mirai 码:[MiraiCode.deserializeMiraiCode]
|
||||
*
|
||||
* @see At
|
||||
* @see AtAll
|
||||
@ -33,7 +34,11 @@ public interface CodableMessage : Message {
|
||||
/**
|
||||
* 转换为 mirai 码.
|
||||
*/
|
||||
public fun toMiraiCode(): String = buildString { appendMiraiCodeTo(this) }
|
||||
public fun serializeToMiraiCode(): String = buildString { appendMiraiCodeTo(this) }
|
||||
|
||||
@PlannedRemoval("2.0.0")
|
||||
@Deprecated("Use serializeToMiraiCode()", ReplaceWith("serializeToMiraiCode()"), DeprecationLevel.ERROR)
|
||||
public fun toMiraiCode(): String = this.serializeToMiraiCode()
|
||||
|
||||
// Using StringBuilder faster than direct plus objects
|
||||
@MiraiExperimentalApi
|
||||
|
@ -15,7 +15,7 @@ import net.mamoe.mirai.contact.Contact
|
||||
import net.mamoe.mirai.message.code.internal.parseMiraiCodeImpl
|
||||
import net.mamoe.mirai.message.data.Message
|
||||
import net.mamoe.mirai.message.data.MessageChain
|
||||
import net.mamoe.mirai.message.data.MessageChain.Companion.parseFromMiraiCode
|
||||
import net.mamoe.mirai.message.data.MessageChain.Companion.deserializeFromMiraiCode
|
||||
import net.mamoe.mirai.utils.safeCast
|
||||
|
||||
/**
|
||||
@ -23,54 +23,56 @@ import net.mamoe.mirai.utils.safeCast
|
||||
*/
|
||||
public object MiraiCode {
|
||||
/**
|
||||
* 解析形如 "[mirai:]" 的 mirai 码, 即 [CodableMessage.toMiraiCode] 返回的内容.
|
||||
* @see MessageChain.parseFromMiraiCode
|
||||
* 解析形如 "[mirai:]" 的 mirai 码, 即 [CodableMessage.serializeToMiraiCode] 返回的内容.
|
||||
* @see MessageChain.deserializeFromMiraiCode
|
||||
*/
|
||||
@JvmName("parseMiraiCode1")
|
||||
@JvmSynthetic
|
||||
public inline fun String.parseMiraiCode(contact: Contact? = null): MessageChain = parseMiraiCode(this, contact)
|
||||
public inline fun String.deserializeMiraiCode(contact: Contact? = null): MessageChain =
|
||||
deserializeMiraiCode(this, contact)
|
||||
|
||||
/**
|
||||
* 解析形如 "[mirai:]" 的 mirai 码, 即 [CodableMessage.toMiraiCode] 返回的内容.
|
||||
* @see MessageChain.parseFromMiraiCode
|
||||
* 解析形如 "[mirai:]" 的 mirai 码, 即 [CodableMessage.serializeToMiraiCode] 返回的内容.
|
||||
* @see MessageChain.deserializeFromMiraiCode
|
||||
*/
|
||||
@JvmOverloads
|
||||
@JvmStatic
|
||||
public fun parseMiraiCode(code: String, contact: Contact? = null): MessageChain = code.parseMiraiCodeImpl(contact)
|
||||
public fun deserializeMiraiCode(code: String, contact: Contact? = null): MessageChain =
|
||||
code.parseMiraiCodeImpl(contact)
|
||||
|
||||
/**
|
||||
* 转换得到 mirai 码.
|
||||
*/
|
||||
@JvmStatic
|
||||
public fun Iterable<Message>.toMiraiCode(): String = iterator().toMiraiCode()
|
||||
public fun Iterable<Message>.serializeToMiraiCode(): String = iterator().serializeToMiraiCode()
|
||||
|
||||
/**
|
||||
* 转换得到 mirai 码.
|
||||
*/
|
||||
@JvmStatic
|
||||
public fun Sequence<Message>.toMiraiCode(): String = iterator().toMiraiCode()
|
||||
public fun Sequence<Message>.serializeToMiraiCode(): String = iterator().serializeToMiraiCode()
|
||||
|
||||
/**
|
||||
* 转换得到 mirai 码.
|
||||
*/
|
||||
@JvmStatic
|
||||
public fun Array<out Message>.toMiraiCode(): String = iterator().toMiraiCode()
|
||||
public fun Array<out Message>.serializeToMiraiCode(): String = iterator().serializeToMiraiCode()
|
||||
|
||||
/**
|
||||
* 转换得到 mirai 码.
|
||||
*/
|
||||
@JvmStatic
|
||||
public fun Iterator<Message>.toMiraiCode(): String = buildString {
|
||||
this@toMiraiCode.forEach {
|
||||
public fun Iterator<Message>.serializeToMiraiCode(): String = buildString {
|
||||
this@serializeToMiraiCode.forEach {
|
||||
it.safeCast<CodableMessage>()?.appendMiraiCodeTo(this)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换得到 mirai 码.
|
||||
* @see CodableMessage.toMiraiCode
|
||||
* @see CodableMessage.serializeToMiraiCode
|
||||
*/
|
||||
@Suppress("EXTENSION_SHADOWED_BY_MEMBER")// for better Java API.
|
||||
@JvmStatic
|
||||
public fun CodableMessage.toMiraiCode(): String = this.toMiraiCode() // member function
|
||||
public fun CodableMessage.serializeToMiraiCode(): String = this.serializeToMiraiCode() // member function
|
||||
}
|
||||
|
@ -38,7 +38,7 @@ public object AtAll :
|
||||
@Suppress("SpellCheckingInspection")
|
||||
override fun contentToString(): String = display
|
||||
override fun toString(): String = "[mirai:atall]"
|
||||
override fun toMiraiCode(): String = toString()
|
||||
override fun serializeToMiraiCode(): String = toString()
|
||||
|
||||
override fun hashCode(): Int = display.hashCode()
|
||||
override fun equals(other: Any?): Boolean = other === this
|
||||
|
@ -31,7 +31,7 @@ public data class Face(public val id: Int) : // used in delegation
|
||||
|
||||
public val name: String get() = contentToString().let { it.substring(1, it.length - 1) }
|
||||
|
||||
override fun toString(): String = toMiraiCode()
|
||||
override fun toString(): String = serializeToMiraiCode()
|
||||
override fun contentToString(): String = names.getOrElse(id) { "[表情]" }
|
||||
|
||||
@MiraiExperimentalApi
|
||||
|
@ -52,7 +52,7 @@ public data class FlashImage(
|
||||
builder.append(stringValue)
|
||||
}
|
||||
|
||||
override fun toMiraiCode(): String = stringValue
|
||||
override fun serializeToMiraiCode(): String = stringValue
|
||||
override fun toString(): String = stringValue
|
||||
override fun contentToString(): String = "[闪照]"
|
||||
|
||||
|
@ -26,8 +26,8 @@ import net.mamoe.mirai.event.events.MessageEvent
|
||||
import net.mamoe.mirai.message.MessageSerializers
|
||||
import net.mamoe.mirai.message.code.CodableMessage
|
||||
import net.mamoe.mirai.message.code.MiraiCode
|
||||
import net.mamoe.mirai.message.code.MiraiCode.parseMiraiCode
|
||||
import net.mamoe.mirai.message.data.MessageChain.Companion.parseFromMiraiCode
|
||||
import net.mamoe.mirai.message.code.MiraiCode.deserializeMiraiCode
|
||||
import net.mamoe.mirai.message.data.MessageChain.Companion.deserializeFromMiraiCode
|
||||
import net.mamoe.mirai.message.data.MessageChain.Companion.serializeToJsonString
|
||||
import net.mamoe.mirai.message.data.MessageSource.Key.quote
|
||||
import net.mamoe.mirai.message.data.MessageSource.Key.recall
|
||||
@ -169,8 +169,8 @@ import kotlin.streams.asSequence
|
||||
*
|
||||
* 详见 [MiraiCode]
|
||||
*
|
||||
* - 使用 [MessageChain.toMiraiCode] 将 [MessageChain] 序列化为 Mirai Code [String].
|
||||
* - 使用 [MessageChain.parseFromMiraiCode] 将 Mirai Code [String] 反序列化为 [MessageChain].
|
||||
* - 使用 [MessageChain.serializeToMiraiCode] 将 [MessageChain] 序列化为 Mirai Code [String].
|
||||
* - 使用 [MessageChain.deserializeFromMiraiCode] 将 Mirai Code [String] 反序列化为 [MessageChain].
|
||||
*
|
||||
*/
|
||||
@Serializable(MessageChain.Serializer::class)
|
||||
@ -315,12 +315,12 @@ public interface MessageChain :
|
||||
format.encodeToString(Serializer, this)
|
||||
|
||||
/**
|
||||
* 解析形如 "[mirai:]" 的 mirai 码, 即 [CodableMessage.toMiraiCode] 返回的内容.
|
||||
* @see MiraiCode.parseMiraiCode
|
||||
* 解析形如 "[mirai:]" 的 mirai 码, 即 [CodableMessage.serializeToMiraiCode] 返回的内容.
|
||||
* @see MiraiCode.deserializeMiraiCode
|
||||
*/
|
||||
@JvmStatic
|
||||
public fun MessageChain.parseFromMiraiCode(miraiCode: String, contact: Contact? = null): MessageChain =
|
||||
miraiCode.parseMiraiCode(contact)
|
||||
public fun MessageChain.deserializeFromMiraiCode(miraiCode: String, contact: Contact? = null): MessageChain =
|
||||
miraiCode.deserializeMiraiCode(contact)
|
||||
}
|
||||
}
|
||||
|
||||
@ -333,7 +333,7 @@ public object EmptyMessageChain : MessageChain, List<SingleMessage> by emptyList
|
||||
|
||||
override fun toString(): String = ""
|
||||
override fun contentToString(): String = ""
|
||||
override fun toMiraiCode(): String = ""
|
||||
override fun serializeToMiraiCode(): String = ""
|
||||
|
||||
@MiraiExperimentalApi
|
||||
override fun appendMiraiCodeTo(builder: StringBuilder) {
|
||||
|
@ -9,7 +9,7 @@
|
||||
|
||||
package net.mamoe.mirai.message.code
|
||||
|
||||
import net.mamoe.mirai.message.code.MiraiCode.parseMiraiCode
|
||||
import net.mamoe.mirai.message.code.MiraiCode.deserializeMiraiCode
|
||||
import net.mamoe.mirai.message.data.*
|
||||
import org.junit.jupiter.api.Test
|
||||
import kotlin.test.assertEquals
|
||||
@ -17,31 +17,31 @@ import kotlin.test.assertEquals
|
||||
class TestMiraiCode {
|
||||
@Test
|
||||
fun testCodes() {
|
||||
assertEquals(AtAll.toMessageChain(), "[mirai:atall]".parseMiraiCode())
|
||||
assertEquals(PlainText("[Hello").toMessageChain(), "\\[Hello".parseMiraiCode())
|
||||
assertEquals(AtAll.toMessageChain(), "[mirai:atall]".deserializeMiraiCode())
|
||||
assertEquals(PlainText("[Hello").toMessageChain(), "\\[Hello".deserializeMiraiCode())
|
||||
assertEquals(buildMessageChain {
|
||||
+PlainText("1")
|
||||
+AtAll
|
||||
+PlainText("2345")
|
||||
+AtAll
|
||||
}, "1[mirai:atall]2345[mirai:atall]".parseMiraiCode())
|
||||
}, "1[mirai:atall]2345[mirai:atall]".deserializeMiraiCode())
|
||||
assertEquals(buildMessageChain {
|
||||
+PlainText("1")
|
||||
+AtAll
|
||||
+PlainText("2345[mirai:atall")
|
||||
}, "1[mirai:atall]2345[mirai:atall".parseMiraiCode())
|
||||
}, "1[mirai:atall]2345[mirai:atall".deserializeMiraiCode())
|
||||
assertEquals(buildMessageChain {
|
||||
+PlainText("[mirai:atall]")
|
||||
}, "\\[mirai:atall]".parseMiraiCode())
|
||||
}, "\\[mirai:atall]".deserializeMiraiCode())
|
||||
assertEquals(buildMessageChain {
|
||||
+PlainText("[mirai:atall]")
|
||||
}, "[mirai:atall\\]".parseMiraiCode())
|
||||
}, "[mirai:atall\\]".deserializeMiraiCode())
|
||||
assertEquals(buildMessageChain {
|
||||
+PlainText("[mirai:atall]")
|
||||
}, "[mirai\\:atall]".parseMiraiCode())
|
||||
}, "[mirai\\:atall]".deserializeMiraiCode())
|
||||
assertEquals(buildMessageChain {
|
||||
+SimpleServiceMessage(1, "[HiHi!!!\\]")
|
||||
+PlainText(" XE")
|
||||
}, "[mirai:service:1,\\[HiHi!!!\\\\\\]] XE".parseMiraiCode())
|
||||
}, "[mirai:service:1,\\[HiHi!!!\\\\\\]] XE".deserializeMiraiCode())
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user