Skip to content

Commit 88814d2

Browse files
committed
Refactor onebot11 session module
1 parent 8312fb6 commit 88814d2

18 files changed

Lines changed: 393 additions & 175 deletions

File tree

build.gradle.kts

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,9 @@ subprojects {
3333
apply(plugin = "signing")
3434
apply(plugin = "org.jetbrains.kotlin.multiplatform")
3535

36-
val wsAddress: String? by extra
37-
val wsPassword: String? by extra
38-
val qqGroupId: String? by extra
39-
40-
tasks.withType<KotlinNativeTest> {
41-
environment("WS_ADDRESS", wsAddress ?: "")
42-
environment("WS_PASSWORD", wsPassword ?: "")
43-
environment("QQ_GROUP_ID", qqGroupId ?: "")
44-
}
45-
46-
tasks.withType<KotlinJvmTest> {
47-
environment("WS_ADDRESS_PLAIN", wsAddress ?: "")
48-
environment("WS_PASSWORD", wsPassword ?: "")
49-
environment("QQ_GROUP_ID", qqGroupId ?: "")
36+
tasks.withType<JavaCompile>().configureEach {
37+
sourceCompatibility = "11"
38+
targetCompatibility = "11"
5039
}
5140

5241
mavenPublishing {

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ kotlin.incremental.native=true
66
org.gradle.test.skip=true
77
kotlin.native.enableKlibsCrossCompilation=true
88
SONATYPE_AUTOMATIC_RELEASE=true
9-
libVersion=3.1.1
9+
libVersion=3.2.0
1010
#systemProp.http.proxyHost=127.0.0.1
1111
#systemProp.http.proxyPort=12334
1212
#systemProp.https.proxyHost=127.0.0.1

ronebot-common/src/commonMain/kotlin/cn/rtast/rob/command/IBaseCommand.kt

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,14 @@ package cn.rtast.rob.command
99

1010
import cn.rtast.rob.entity.IGroupMessage
1111
import cn.rtast.rob.entity.IPrivateMessage
12-
import cn.rtast.rob.session.IGroupSession
13-
import cn.rtast.rob.session.IPrivateSession
12+
import cn.rtast.rob.session.GroupSessionStruct
13+
import cn.rtast.rob.session.PrivateSessionStruct
1414

1515
/**
1616
* 所有子模块的基本抽象命令父类
1717
* 都要继承此接口
1818
*/
19-
public interface IBaseCommand<
20-
G : IGroupMessage, P : IPrivateMessage,
21-
GS : IGroupSession<G>, PS : IPrivateSession<P>> {
19+
public interface IBaseCommand<G : IGroupMessage, P : IPrivateMessage> {
2220
/**
2321
* 定义指令别名
2422
*/
@@ -53,7 +51,6 @@ public interface IBaseCommand<
5351
*/
5452
public suspend fun handleGroup(message: G, matchedCommand: String)
5553

56-
public suspend fun <T> startGroupSession(init: T, block: GS)
57-
58-
public suspend fun <T> startPrivateSession(init: T, block: PS)
54+
public suspend fun startGroupSession(message: G, block: suspend (GroupSessionStruct<G>) -> Unit)
55+
public suspend fun startPrivateSession(message: P, block: suspend (PrivateSessionStruct<P>) -> Unit)
5956
}

ronebot-common/src/commonMain/kotlin/cn/rtast/rob/session/ISession.kt

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,30 @@ package cn.rtast.rob.session
1111
import cn.rtast.rob.entity.IGroupMessage
1212
import cn.rtast.rob.entity.IMessage
1313
import cn.rtast.rob.entity.IPrivateMessage
14+
import kotlinx.coroutines.CompletableDeferred
1415
import kotlin.uuid.ExperimentalUuidApi
1516

16-
public sealed interface ISession<T: IMessage> {
17-
public val message: T
18-
public val args: List<String>
17+
18+
public sealed interface ISession<T : ISessionStruct<K>, K : IMessage> {
19+
public val initMessage: K
20+
public val block: suspend (T) -> Unit
21+
22+
@Suppress("PropertyName")
23+
public val __finished: CompletableDeferred<Unit>
1924
}
2025

21-
public data class GroupSessionStruct<G: IGroupMessage>(
22-
override val args: List<String>,
23-
override val message: G
24-
) : ISession<G>
25-
26-
public data class PrivateSessionStruct<P: IPrivateMessage>(
27-
override val args: List<String>,
28-
override val message: P
29-
) : ISession<P>
30-
//public interface IPrivateSession<T> : ISession<T> {
31-
// override val message: IPrivateMessage
32-
// override val sender: IPrivateSender
33-
//}
34-
35-
//public interface IGroupSession<T> : ISession<T> {
36-
// override val message: IGroupMessage
37-
// override val sender: IGroupSender
38-
//}
39-
40-
public fun interface IGroupSession<T: IGroupMessage> {
41-
public fun consume(message: GroupSessionStruct<T>)
26+
public class GroupSession<T : IGroupMessage>(
27+
override val initMessage: T,
28+
override val block: suspend (GroupSessionStruct<T>) -> Unit,
29+
public val groupId: Long,
30+
) : ISession<GroupSessionStruct<T>, T> {
31+
override val __finished: CompletableDeferred<Unit> = CompletableDeferred()
4232
}
4333

44-
public fun interface IPrivateSession<T: IPrivateMessage> {
45-
public fun consume(arg: PrivateSessionStruct<T>)
46-
}
34+
35+
public class PrivateSession<T : IPrivateMessage>(
36+
override val block: suspend (PrivateSessionStruct<T>) -> Unit,
37+
override val initMessage: T,
38+
) : ISession<PrivateSessionStruct<T>, T> {
39+
override val __finished: CompletableDeferred<Unit> = CompletableDeferred()
40+
}

ronebot-common/src/commonMain/kotlin/cn/rtast/rob/session/ISessionManager.kt

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,32 @@
66

77
package cn.rtast.rob.session
88

9-
import cn.rtast.rob.command.IBaseCommand
10-
import cn.rtast.rob.entity.IGroupMessage
11-
import cn.rtast.rob.entity.IGroupSender
12-
import cn.rtast.rob.entity.IPrivateMessage
13-
import cn.rtast.rob.entity.IPrivateSender
9+
import cn.rtast.rob.entity.*
1410

1511
/**
1612
* 一个会话管理器的接口, 提供了一些基本的对会话的操作
1713
* 例如: 获取会话、开始会话、结束会话
1814
*/
19-
public interface ISessionManager<G: IGroupMessage, P: IPrivateMessage> {
20-
public val privateSessions: MutableMap<P, IPrivateSession<P>>
21-
public val groupSessions: MutableMap<G, IGroupSession<G>>
15+
public interface ISessionManager<
16+
GS : IGroupSender, PS : IPrivateSender,
17+
GM : IGroupMessage, PM : IPrivateMessage,
18+
> {
2219

23-
public suspend fun handleGroupSession(message: G, args: List<String>)
24-
public suspend fun handlePrivateSession(message: P, args: List<String>)
20+
public val privateSessions: MutableMap<PS, PrivateSession<PM>>
21+
public val groupSessions: MutableMap<GS, GroupSession<GM>>
22+
23+
public fun getGroupSession(message: GM): (GroupSession<GM>)?
24+
public fun getPrivateSession(message: PM): (PrivateSession<PM>)?
25+
26+
public suspend fun handleGroupSession(message: GM, args: List<String>): Unit?
27+
public suspend fun handlePrivateSession(message: PM, args: List<String>): Unit?
28+
29+
public suspend fun startGroupSession(message: GM, block: GroupSession<GM>): Unit?
30+
public suspend fun startPrivateSession(message: PM, block: PrivateSession<PM>): Unit?
31+
32+
public suspend fun endGroupSession(message: GM, feedback: IMessageChain?): Unit?
33+
public suspend fun endPrivateSession(message: PM, feedback: IMessageChain?): Unit?
34+
35+
public suspend fun rejectGroupSession(message: GM, feedback: IMessageChain): Unit?
36+
public suspend fun rejectPrivateSession(message: PM, feedback: IMessageChain): Unit?
2537
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright © 2025 RTAkland
3+
* Date: 10/6/25, 2:37 PM
4+
* Open Source Under Apache-2.0 License
5+
* https://www.apache.org/licenses/LICENSE-2.0
6+
*/
7+
8+
9+
package cn.rtast.rob.session
10+
11+
import cn.rtast.rob.entity.IGroupMessage
12+
import cn.rtast.rob.entity.IMessage
13+
import cn.rtast.rob.entity.IPrivateMessage
14+
15+
public sealed interface ISessionStruct<T : IMessage> {
16+
public val message: T
17+
public val args: List<String>
18+
public val manager: ISessionManager<*, *, *, *>
19+
}
20+
21+
public data class GroupSessionStruct<G : IGroupMessage>(
22+
override val args: List<String>,
23+
override val message: G,
24+
override val manager: ISessionManager<*, *, G, *>,
25+
) : ISessionStruct<G>
26+
27+
public data class PrivateSessionStruct<P : IPrivateMessage>(
28+
override val args: List<String>,
29+
override val message: P,
30+
override val manager: ISessionManager<*, *, *, P>,
31+
) : ISessionStruct<P>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright © 2025 RTAkland
3+
* Date: 10/6/25, 2:37 PM
4+
* Open Source Under Apache-2.0 License
5+
* https://www.apache.org/licenses/LICENSE-2.0
6+
*/
7+
8+
9+
package cn.rtast.rob.session
10+
11+
import cn.rtast.rob.entity.IGroupMessage
12+
import cn.rtast.rob.entity.IMessageChain
13+
import cn.rtast.rob.entity.IPrivateMessage
14+
15+
/**
16+
* 结束会话
17+
* 可选的对消息进行回复
18+
* 群聊
19+
*/
20+
public suspend fun <T : IGroupMessage> GroupSessionStruct<T>.accept(feedback: IMessageChain? = null): Unit? =
21+
manager.endGroupSession(message, feedback)
22+
23+
/**
24+
* 结束会话
25+
* 可选的对消息进行回复
26+
* 私聊
27+
*/
28+
public suspend fun <T : IPrivateMessage> PrivateSessionStruct<T>.accept(feedback: IMessageChain? = null): Unit? =
29+
manager.endPrivateSession(message, feedback)
30+
31+
/**
32+
* 拒绝本次输入, 不结束会话继续接收内容
33+
* 并回复内容
34+
* 群聊
35+
*/
36+
public suspend fun <T : IGroupMessage> GroupSessionStruct<T>.reject(feedback: IMessageChain): Unit? =
37+
manager.rejectGroupSession(message, feedback)
38+
39+
/**
40+
* 拒绝本次输入, 不结束会话继续接收内容
41+
* 并回复内容
42+
* 私聊
43+
*/
44+
public suspend fun <T : IPrivateMessage> PrivateSessionStruct<T>.reject(feedback: IMessageChain): Unit? =
45+
manager.rejectPrivateSession(message, feedback)

ronebot-milky/src/commonMain/kotlin/cn/rtast/rob/milky/command/BaseCommand.kt

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
package cn.rtast.rob.milky.command
99

1010
import cn.rtast.rob.command.IBaseCommand
11-
import cn.rtast.rob.entity.IMessageChain
1211
import cn.rtast.rob.milky.event.ws.raw.ReceiveMessage
1312

1413
public abstract class BaseCommand : IBaseCommand<ReceiveMessage, ReceiveMessage> {
@@ -22,46 +21,4 @@ public abstract class BaseCommand : IBaseCommand<ReceiveMessage, ReceiveMessage>
2221
public override suspend fun executeGroup(message: ReceiveMessage, args: List<String>) {}
2322
public override suspend fun executePrivate(message: ReceiveMessage, args: List<String>) {}
2423
public override suspend fun executeTemp(message: ReceiveMessage, args: List<String>) {}
25-
26-
// 下面都没用
27-
28-
@Deprecated("没用", level = DeprecationLevel.HIDDEN)
29-
public override suspend fun handleGroup(message: ReceiveMessage, matchedCommand: String) {
30-
}
31-
32-
@Deprecated("没用", level = DeprecationLevel.HIDDEN)
33-
public override suspend fun handlePrivate(message: ReceiveMessage, matchedCommand: String) {
34-
}
35-
36-
@Deprecated("没用", level = DeprecationLevel.HIDDEN)
37-
public override suspend fun onGroupSession(message: ReceiveMessage) {
38-
}
39-
40-
@Deprecated("没用", level = DeprecationLevel.HIDDEN)
41-
public override suspend fun onGroupSession(message: ReceiveMessage, initArg: Any) {
42-
}
43-
44-
@Deprecated("没用", level = DeprecationLevel.HIDDEN)
45-
public override suspend fun onPrivateSession(message: ReceiveMessage) {
46-
}
47-
48-
@Deprecated("没用", level = DeprecationLevel.HIDDEN)
49-
public override suspend fun onPrivateSession(message: ReceiveMessage, initArg: Any) {
50-
}
51-
52-
@Deprecated("没用", level = DeprecationLevel.HIDDEN)
53-
public override suspend fun ReceiveMessage.reject(reason: IMessageChain) {
54-
}
55-
56-
@Deprecated("没用", level = DeprecationLevel.HIDDEN)
57-
public override suspend fun ReceiveMessage.skipSession() {
58-
}
59-
60-
@Deprecated("没用", level = DeprecationLevel.HIDDEN)
61-
public override suspend fun <T : Any> ReceiveMessage.startSession(initArg: T) {
62-
}
63-
64-
@Deprecated("没用", level = DeprecationLevel.HIDDEN)
65-
public override suspend fun ReceiveMessage.startSession() {
66-
}
6724
}

ronebot-onebot-v11/src/commonMain/kotlin/cn/rtast/rob/OneBotFactory.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,10 @@ public class OneBotFactory {
7373
public val commandManager: CommandManagerImpl = CommandManagerImpl()
7474

7575
/**
76-
* 通过继承来实现命令的方式的会话管理器
76+
* 会话管理器
7777
*/
7878
@JvmStatic
79+
@ExperimentalROneBotApi
7980
public val sessionManager: SessionManager = SessionManager()
8081

8182
/**

ronebot-onebot-v11/src/commonMain/kotlin/cn/rtast/rob/command/BaseCommand.kt

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,37 @@ import cn.rtast.rob.event.raw.message.GroupMessage
1414
import cn.rtast.rob.event.raw.message.PrivateMessage
1515
import cn.rtast.rob.event.raw.message.first
1616
import cn.rtast.rob.interceptor.CommandInterceptor
17-
import cn.rtast.rob.session.IGroupSession
18-
import cn.rtast.rob.session.IPrivateSession
17+
import cn.rtast.rob.session.GroupSession
18+
import cn.rtast.rob.session.GroupSessionStruct
19+
import cn.rtast.rob.session.PrivateSession
20+
import cn.rtast.rob.session.PrivateSessionStruct
1921

2022

2123
public abstract class BaseCommand(
2224
public val interceptor: CommandInterceptor? = null,
23-
) : IBaseCommand<GroupMessage, PrivateMessage, IGroupSession, IPrivateSession> {
25+
) : IBaseCommand<GroupMessage, PrivateMessage> {
2426
abstract override val commandNames: List<String>
2527
override suspend fun executeGroup(message: GroupMessage, args: List<String>) {}
2628
override suspend fun executePrivate(message: PrivateMessage, args: List<String>) {}
27-
final override suspend fun <T> startGroupSession(init: T, block: IGroupSession) {
2829

30+
@ExperimentalROneBotApi
31+
final override suspend fun startGroupSession(
32+
message: GroupMessage,
33+
block: suspend (GroupSessionStruct<GroupMessage>) -> Unit,
34+
) {
35+
val session = GroupSession(message, block, message.groupId)
36+
OneBotFactory.sessionManager.startGroupSession(message, session)
37+
session.__finished.await()
2938
}
3039

31-
final override suspend fun <T> startPrivateSession(init: T, block: IPrivateSession) {
32-
40+
@ExperimentalROneBotApi
41+
final override suspend fun startPrivateSession(
42+
message: PrivateMessage,
43+
block: suspend (PrivateSessionStruct<PrivateMessage>) -> Unit,
44+
) {
45+
val session = PrivateSession(block, message)
46+
OneBotFactory.sessionManager.startPrivateSession(message, session)
47+
session.__finished.await()
3348
}
3449

3550
final override suspend fun handlePrivate(

0 commit comments

Comments
 (0)