Skip to content

Commit 92752e8

Browse files
authored
Merge pull request #206 from simple-robot/dev/custom-event-type
支持注册自定义事件解析器
2 parents 497cfec + bd1ff6d commit 92752e8

File tree

18 files changed

+613
-43
lines changed

18 files changed

+613
-43
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/api/simbot-component-onebot-v11-core.api

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2125,6 +2125,29 @@ public final class love/forte/simbot/component/onebot/v11/core/component/OneBot1
21252125
public static synthetic fun useOneBot11Component$default (Llove/forte/simbot/application/ApplicationFactoryConfigurer;Llove/forte/simbot/common/function/ConfigurerFunction;ILjava/lang/Object;)V
21262126
}
21272127

2128+
public class love/forte/simbot/component/onebot/v11/core/event/CustomEventResolveException : java/lang/RuntimeException {
2129+
public fun <init> ()V
2130+
public fun <init> (Ljava/lang/String;)V
2131+
public fun <init> (Ljava/lang/String;Ljava/lang/Throwable;)V
2132+
public fun <init> (Ljava/lang/Throwable;)V
2133+
}
2134+
2135+
public abstract interface class love/forte/simbot/component/onebot/v11/core/event/CustomEventResolver$Context {
2136+
public abstract fun getBot ()Llove/forte/simbot/component/onebot/v11/core/bot/OneBotBot;
2137+
public abstract fun getJson ()Lkotlinx/serialization/json/Json;
2138+
public abstract fun getRawEventResolveResult ()Llove/forte/simbot/component/onebot/v11/core/event/RawEventResolveResult;
2139+
}
2140+
2141+
public class love/forte/simbot/component/onebot/v11/core/event/EventResolveException : java/lang/RuntimeException {
2142+
public fun <init> ()V
2143+
public fun <init> (Ljava/lang/String;)V
2144+
public fun <init> (Ljava/lang/String;Ljava/lang/Throwable;)V
2145+
public fun <init> (Ljava/lang/Throwable;)V
2146+
}
2147+
2148+
public abstract interface annotation class love/forte/simbot/component/onebot/v11/core/event/ExperimentalCustomEventResolverApi : java/lang/annotation/Annotation {
2149+
}
2150+
21282151
public abstract interface class love/forte/simbot/component/onebot/v11/core/event/OneBotBotEvent : love/forte/simbot/component/onebot/v11/core/event/OneBotEvent, love/forte/simbot/event/BotEvent {
21292152
public abstract fun getBot ()Llove/forte/simbot/component/onebot/v11/core/bot/OneBotBot;
21302153
}

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,20 @@ kotlin {
105105
implementation(libs.log4j.slf4j2)
106106
implementation(libs.kotlinPoet)
107107
implementation(libs.kotlinx.coroutines.reactor)
108-
api(libs.ktor.client.java)
108+
implementation(libs.ktor.client.java)
109109
implementation(libs.ktor.server.netty)
110110
implementation(libs.ktor.server.ws)
111111
}
112+
113+
appleTest.dependencies {
114+
implementation(libs.ktor.client.darwin)
115+
}
116+
mingwTest.dependencies {
117+
implementation(libs.ktor.client.winhttp)
118+
}
119+
linuxTest.dependencies {
120+
implementation(libs.ktor.client.cio)
121+
}
112122
}
113123
}
114124

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: 57 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,56 @@ 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(
286+
resolver: CustomKotlinSerializationEventResolver
287+
) {
288+
addCustomEventResolver(resolver)
289+
}
290+
291+
/**
292+
* 添加一个 [CustomKotlinSerializationEventResolver]。
293+
* 原则上通过 `postType` 和 `subType` 可以定位唯一一个事件类型。
294+
*
295+
* @see OneBotBotConfiguration.addCustomEventResolver
296+
* @since 1.8.0
297+
*/
298+
@ExperimentalCustomEventResolverApi
299+
public inline fun OneBotBotConfiguration.addCustomKotlinSerializationEventResolver(
300+
postType: String,
301+
subType: String,
302+
crossinline deserializationStrategy: () -> DeserializationStrategy<Event>
303+
) {
304+
addCustomKotlinSerializationEventResolver { context ->
305+
val rawEventResolveResult = context.rawEventResolveResult
306+
if (rawEventResolveResult.postType == postType && rawEventResolveResult.subType == subType) {
307+
deserializationStrategy()
308+
} else {
309+
null
310+
}
311+
}
255312
}

0 commit comments

Comments
 (0)