Skip to content

Commit 48f762a

Browse files
committed
feat: 支持注册自定义事件解析器
1 parent 497cfec commit 48f762a

File tree

16 files changed

+587
-48
lines changed

16 files changed

+587
-48
lines changed

build.gradle.kts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@ apiValidation {
138138
"love.forte.simbot.annotations.InternalSimbotAPI",
139139
"love.forte.simbot.component.onebot.common.annotations.ApiResultConstructor",
140140
"love.forte.simbot.component.onebot.common.annotations.SourceEventConstructor",
141+
142+
// CustomEventResolver
143+
"love.forte.simbot.component.onebot.v11.core.event.ExperimentalCustomEventResolverApi"
141144
),
142145
)
143146

buildSrc/src/main/kotlin/P.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ object P {
3737
override val description: String get() = DESCRIPTION
3838
override val homepage: String get() = HOMEPAGE
3939

40-
const val VERSION = "1.7.0"
41-
const val NEXT_VERSION = "1.7.1"
40+
const val VERSION = "1.8.0"
41+
const val NEXT_VERSION = "1.8.0"
4242

4343
override val snapshotVersion = "$NEXT_VERSION-SNAPSHOT"
4444
override val version = if (isSnapshot()) snapshotVersion else VERSION

simbot-component-onebot-v11/simbot-component-onebot-v11-core/build.gradle.kts

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,21 @@ kotlin {
5959
applyTier3(supportKtorClient = true)
6060

6161
sourceSets {
62+
configureEach {
63+
if (this.name == "jvmMain") {
64+
dependencies {
65+
compileOnly(libs.simbot.api)
66+
}
67+
} else {
68+
dependencies {
69+
implementation(libs.simbot.api)
70+
}
71+
}
72+
}
6273
commonMain.dependencies {
6374
// JVM compileOnly
64-
implementation(libs.simbot.api)
65-
implementation(libs.jetbrains.annotations)
75+
// implementation(libs.simbot.api)
76+
api(libs.jetbrains.annotations)
6677

6778
api(libs.simbot.common.annotations)
6879
implementation(libs.simbot.common.atomic)
@@ -92,9 +103,11 @@ kotlin {
92103

93104
jvmMain {
94105
dependencies {
95-
compileOnly(libs.simbot.api)
96-
compileOnly(libs.ktor.client.contentNegotiation)
97-
compileOnly(libs.jetbrains.annotations)
106+
// api(libs.simbot.api)
107+
// api(libs.jetbrains.annotations)
108+
// compileOnly(libs.simbot.api)
109+
// compileOnly(libs.ktor.client.contentNegotiation)
110+
// compileOnly(libs.jetbrains.annotations)
98111
}
99112
}
100113

@@ -105,10 +118,14 @@ kotlin {
105118
implementation(libs.log4j.slf4j2)
106119
implementation(libs.kotlinPoet)
107120
implementation(libs.kotlinx.coroutines.reactor)
108-
api(libs.ktor.client.java)
121+
implementation(libs.ktor.client.java)
109122
implementation(libs.ktor.server.netty)
110123
implementation(libs.ktor.server.ws)
111124
}
125+
126+
appleTest.dependencies {
127+
implementation(libs.ktor.client.darwin)
128+
}
112129
}
113130
}
114131

simbot-component-onebot-v11/simbot-component-onebot-v11-core/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/core/api/OneBotApi.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public interface OneBotApi<T : Any> {
5252
public val resultDeserializer: DeserializationStrategy<T>
5353

5454
/**
55-
* 预期结果 [OneBotApi] 类型的反序列化器。
55+
* 预期结果 [OneBotApiResult] 类型的反序列化器。
5656
*/
5757
public val apiResultDeserializer: DeserializationStrategy<OneBotApiResult<T>>
5858

simbot-component-onebot-v11/simbot-component-onebot-v11-core/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/core/bot/OneBotBot.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import love.forte.simbot.component.onebot.v11.message.OneBotMessageContent
4343
import love.forte.simbot.event.EventResult
4444
import love.forte.simbot.message.MessageReference
4545
import love.forte.simbot.suspendrunner.ST
46+
import org.intellij.lang.annotations.Language
4647
import kotlin.coroutines.CoroutineContext
4748
import kotlin.jvm.JvmSynthetic
4849

@@ -200,14 +201,14 @@ public interface OneBotBot : Bot, OneBotApiExecutable {
200201
*
201202
* @throws IllegalArgumentException 如果事件解析失败
202203
*/
203-
public fun push(rawEvent: String): Flow<EventResult>
204+
public fun push(@Language("json") rawEvent: String): Flow<EventResult>
204205

205206
/**
206207
* 直接推送一个外部的原始事件字符串,并在异步任务中处理事件。
207208
*
208209
* @throws IllegalArgumentException 如果事件解析失败
209210
*/
210-
public fun pushAndLaunch(rawEvent: String): Job =
211+
public fun pushAndLaunch(@Language("json") rawEvent: String): Job =
211212
push(rawEvent).launchIn(this)
212213

213214
/**

simbot-component-onebot-v11/simbot-component-onebot-v11-core/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/core/bot/OneBotBotConfiguration.kt

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,17 @@ import io.ktor.client.*
2121
import io.ktor.client.engine.*
2222
import io.ktor.client.plugins.*
2323
import io.ktor.http.*
24+
import kotlinx.serialization.DeserializationStrategy
2425
import kotlinx.serialization.modules.SerializersModule
2526
import kotlinx.serialization.modules.overwriteWith
2627
import love.forte.simbot.common.function.ConfigurerFunction
2728
import love.forte.simbot.common.function.invokeBy
2829
import love.forte.simbot.component.onebot.common.annotations.ExperimentalOneBotAPI
30+
import love.forte.simbot.component.onebot.v11.core.event.CustomEventResolver
31+
import love.forte.simbot.component.onebot.v11.core.event.CustomKotlinSerializationEventResolver
32+
import love.forte.simbot.component.onebot.v11.core.event.ExperimentalCustomEventResolverApi
2933
import love.forte.simbot.component.onebot.v11.message.segment.OneBotImage
34+
import love.forte.simbot.event.Event
3035
import love.forte.simbot.resource.Resource
3136
import kotlin.coroutines.CoroutineContext
3237
import kotlin.coroutines.EmptyCoroutineContext
@@ -252,4 +257,54 @@ public class OneBotBotConfiguration {
252257
public fun defaultImageAdditionalParams(params: OneBotImage.AdditionalParams?) {
253258
defaultImageAdditionalParamsProvider = { params }
254259
}
260+
261+
/**
262+
*
263+
* @since 1.8.0
264+
*/
265+
@ExperimentalCustomEventResolverApi
266+
internal val customEventResolvers: MutableList<CustomEventResolver> = mutableListOf()
267+
268+
/**
269+
* 注册一个 [CustomEventResolver]。
270+
* @since 1.8.0
271+
*/
272+
@ExperimentalCustomEventResolverApi
273+
public fun addCustomEventResolver(customEventResolver: CustomEventResolver) {
274+
customEventResolvers.add(customEventResolver)
275+
}
276+
}
277+
278+
/**
279+
* 添加一个 [CustomKotlinSerializationEventResolver]。
280+
*
281+
* @see OneBotBotConfiguration.addCustomEventResolver
282+
* @since 1.8.0
283+
*/
284+
@ExperimentalCustomEventResolverApi
285+
public fun OneBotBotConfiguration.addCustomKotlinSerializationEventResolver(resolver: CustomKotlinSerializationEventResolver) {
286+
addCustomEventResolver(resolver)
287+
}
288+
289+
/**
290+
* 添加一个 [CustomKotlinSerializationEventResolver]。
291+
* 原则上通过 `postType` 和 `subType` 可以定位唯一一个事件类型。
292+
*
293+
* @see OneBotBotConfiguration.addCustomEventResolver
294+
* @since 1.8.0
295+
*/
296+
@ExperimentalCustomEventResolverApi
297+
public inline fun OneBotBotConfiguration.addCustomKotlinSerializationEventResolver(
298+
postType: String,
299+
subType: String,
300+
crossinline deserializationStrategy: () -> DeserializationStrategy<Event>
301+
) {
302+
addCustomKotlinSerializationEventResolver { context ->
303+
val rawEventResolveResult = context.rawEventResolveResult
304+
if (rawEventResolveResult.postType == postType && rawEventResolveResult.subType == subType) {
305+
deserializationStrategy()
306+
} else {
307+
null
308+
}
309+
}
255310
}

0 commit comments

Comments
 (0)