Skip to content

Commit

Permalink
pref: UnknownEvent 增加 rawJson 来直接获取解析后的 JsonObject
Browse files Browse the repository at this point in the history
  • Loading branch information
ForteScarlet committed Jun 20, 2024
1 parent fc7ece1 commit cfd43e4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ internal class OneBotBotImpl(
fun toUnknown(reason: Throwable? = null): UnknownEvent {
val time = obj["time"]?.jsonPrimitive?.long ?: -1L
val selfId = obj["self_id"]?.jsonPrimitive?.long?.ID ?: 0L.ID
return UnknownEvent(time, selfId, postType, text, reason)
return UnknownEvent(time, selfId, postType, text, obj, reason)
}

if (subType == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ kotlin {
commonMain.dependencies {
implementation(libs.simbot.api)
implementation(libs.simbot.common.annotations)
implementation(libs.kotlinx.serialization.json)
implementation(project(":simbot-component-onebot-common"))

api(project(":simbot-component-onebot-v11:simbot-component-onebot-v11-common"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package love.forte.simbot.component.onebot.v11.event

import kotlinx.serialization.SerializationException
import kotlinx.serialization.json.JsonObject
import love.forte.simbot.annotations.FragileSimbotAPI
import love.forte.simbot.common.id.LongID
import love.forte.simbot.component.onebot.common.annotations.InternalOneBotAPI
Expand All @@ -32,6 +33,12 @@ import love.forte.simbot.component.onebot.common.annotations.InternalOneBotAPI
* 如果 [UnknownEvent] 是由于某些异常而产生(例如原本事件进行序列化但是失败了),
* 那么失败的原因则会通过 [reason] 提供。
*
* ### 内部构造
*
* [UnknownEvent] 应当始终由内部使用、构造,
* 不要在其他地方自行构造 [UnknownEvent],
* 它的构造函数不保证任何源码或二进制兼容。
*
* ### FragileAPI
*
* 这是一个具有**特殊规则**的事件类型。
Expand All @@ -51,11 +58,17 @@ public class UnknownEvent @InternalOneBotAPI constructor(
*/
public val raw: String,

/**
* [raw] 对应解析的 [JsonObject] 对象。
*/
public val rawJson: JsonObject,

/**
* 如果是由于异常而产生,则此处为异常的原因。
* 通常会是 [SerializationException]。
*/
public val reason: Throwable? = null
public val reason: Throwable? = null,

) : RawEvent {
override fun toString(): String =
"UnknownEvent(time=$time, selfId=$selfId, postType='$postType')"
Expand Down

0 comments on commit cfd43e4

Please sign in to comment.