Skip to content

Commit 8312fb6

Browse files
committed
Refactor session manager WIP
1 parent 12c0f36 commit 8312fb6

8 files changed

Lines changed: 117 additions & 258 deletions

File tree

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

Lines changed: 7 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,17 @@
88
package cn.rtast.rob.command
99

1010
import cn.rtast.rob.entity.IGroupMessage
11-
import cn.rtast.rob.entity.IMessageChain
1211
import cn.rtast.rob.entity.IPrivateMessage
12+
import cn.rtast.rob.session.IGroupSession
13+
import cn.rtast.rob.session.IPrivateSession
1314

1415
/**
1516
* 所有子模块的基本抽象命令父类
1617
* 都要继承此接口
1718
*/
18-
public interface IBaseCommand<G : IGroupMessage, P : IPrivateMessage> {
19+
public interface IBaseCommand<
20+
G : IGroupMessage, P : IPrivateMessage,
21+
GS : IGroupSession<G>, PS : IPrivateSession<P>> {
1922
/**
2023
* 定义指令别名
2124
*/
@@ -50,63 +53,7 @@ public interface IBaseCommand<G : IGroupMessage, P : IPrivateMessage> {
5053
*/
5154
public suspend fun handleGroup(message: G, matchedCommand: String)
5255

53-
/**
54-
* 私聊拒绝这次的会话内容
55-
*/
56-
public suspend fun P.reject(reason: IMessageChain)
57-
58-
/**
59-
* 群聊拒绝这次的会话内容
60-
*/
61-
public suspend fun G.reject(reason: IMessageChain)
62-
63-
/**
64-
* 群聊会话接收函数
65-
*/
66-
public suspend fun onGroupSession(message: G)
67-
68-
/**
69-
* 群聊会话接收函数, 但是还会附带初始参数
70-
*/
71-
public suspend fun onGroupSession(message: G, initArg: Any)
72-
73-
/**
74-
* 私聊会话接收函数
75-
*/
76-
public suspend fun onPrivateSession(message: P)
77-
78-
/**
79-
* 群聊会话接收函数, 但是还会附带初始参数
80-
*/
81-
public suspend fun onPrivateSession(message: P, initArg: Any)
56+
public suspend fun <T> startGroupSession(init: T, block: GS)
8257

83-
/**
84-
* 私聊结束会话
85-
*/
86-
public suspend fun P.skipSession()
87-
88-
/**
89-
* 群聊结束会话
90-
*/
91-
public suspend fun G.skipSession()
92-
93-
/**
94-
* 群聊开始会话
95-
*/
96-
public suspend fun G.startSession()
97-
98-
/**
99-
* 开始群聊会话但是附带初始参数
100-
*/
101-
public suspend fun <T : Any> G.startSession(initArg: T)
102-
103-
/**
104-
* 私聊开始会话
105-
*/
106-
public suspend fun P.startSession()
107-
108-
/**
109-
* 开始私聊会话但是附带初始参数
110-
*/
111-
public suspend fun <T : Any> P.startSession(initArg: T)
58+
public suspend fun <T> startPrivateSession(init: T, block: PS)
11259
}

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

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,39 @@
88

99
package cn.rtast.rob.session
1010

11-
import cn.rtast.rob.command.IBaseCommand
12-
import cn.rtast.rob.entity.*
11+
import cn.rtast.rob.entity.IGroupMessage
12+
import cn.rtast.rob.entity.IMessage
13+
import cn.rtast.rob.entity.IPrivateMessage
1314
import kotlin.uuid.ExperimentalUuidApi
14-
import kotlin.uuid.Uuid
15-
16-
public interface ISession<T> {
17-
public val id: Uuid
18-
public var active: Boolean
19-
public val message: IMessage
20-
public val command: IBaseCommand<out IGroupMessage, out IPrivateMessage>
21-
public val sender: ISender
22-
public val initArgType: T
23-
24-
public fun endSession() {
25-
active = false
26-
}
15+
16+
public sealed interface ISession<T: IMessage> {
17+
public val message: T
18+
public val args: List<String>
2719
}
2820

29-
public interface IPrivateSession<T> : ISession<T> {
30-
override val message: IPrivateMessage
31-
override val sender: IPrivateSender
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>)
3242
}
3343

34-
public interface IGroupSession<T> : ISession<T> {
35-
override val message: IGroupMessage
36-
override val sender: IGroupSender
44+
public fun interface IPrivateSession<T: IPrivateMessage> {
45+
public fun consume(arg: PrivateSessionStruct<T>)
3746
}

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

Lines changed: 5 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -16,77 +16,10 @@ import cn.rtast.rob.entity.IPrivateSender
1616
* 一个会话管理器的接口, 提供了一些基本的对会话的操作
1717
* 例如: 获取会话、开始会话、结束会话
1818
*/
19-
public interface ISessionManager<
20-
P : IPrivateMessage,
21-
G : IGroupMessage,
22-
PS : IPrivateSession<*>,
23-
GS : IGroupSession<*>,
24-
B : IBaseCommand<out IGroupMessage, out IPrivateMessage>,
25-
GSS : IGroupSender,
26-
PSS : IPrivateSender> {
27-
/**
28-
* 私聊正在进行的会话
29-
*/
30-
public val privateActiveSessions: MutableMap<PSS, out IPrivateSession<*>>
19+
public interface ISessionManager<G: IGroupMessage, P: IPrivateMessage> {
20+
public val privateSessions: MutableMap<P, IPrivateSession<P>>
21+
public val groupSessions: MutableMap<G, IGroupSession<G>>
3122

32-
/**
33-
* 群聊正在进行的会话
34-
*/
35-
public val groupActiveSessions: MutableMap<GSS, out IGroupSession<*>>
36-
37-
/**
38-
* 开始群聊会话
39-
*/
40-
public suspend fun startGroupSession(message: G, command: B): GS
41-
42-
/**
43-
* 开始群聊会话但是带上一个初始参数
44-
*/
45-
public suspend fun <T : Any> startGroupSession(message: G, command: B, initArg: T): GS
46-
47-
/**
48-
* 开始私聊会话
49-
*/
50-
public suspend fun startPrivateSession(message: P, command: B): PS
51-
52-
/**
53-
* 开启私聊会话但是带上一个初始参数
54-
*/
55-
public suspend fun <T : Any> startPrivateSession(message: P, command: B, initArg: T): PS
56-
57-
/**
58-
* 结束群聊会话
59-
*/
60-
public suspend fun endGroupSession(sender: GSS) {
61-
groupActiveSessions[sender]?.endSession()
62-
groupActiveSessions.remove(sender)
63-
}
64-
65-
/**
66-
* 结束私聊会话
67-
*/
68-
public suspend fun endPrivateSession(sender: PSS) {
69-
privateActiveSessions[sender]?.endSession()
70-
privateActiveSessions.remove(sender)
71-
}
72-
73-
/**
74-
* 获取私聊会话
75-
*/
76-
public suspend fun getPrivateSession(sender: PSS): PS?
77-
78-
/**
79-
* 获取私聊会话
80-
*/
81-
public suspend fun <T> getTypedPrivateSession(sender: PSS): IPrivateSession<T>?
82-
83-
/**
84-
* 获取群聊会话
85-
*/
86-
public suspend fun getGroupSession(sender: GSS): GS?
87-
88-
/**
89-
* 获取群聊会话
90-
*/
91-
public suspend fun <T> getTypedGroupSession(sender: GSS): IGroupSession<T>?
23+
public suspend fun handleGroupSession(message: G, args: List<String>)
24+
public suspend fun handlePrivateSession(message: P, args: List<String>)
9225
}

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

Lines changed: 11 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,27 @@ package cn.rtast.rob.command
1010

1111
import cn.rtast.rob.OneBotFactory
1212
import cn.rtast.rob.annotations.ExperimentalROneBotApi
13-
import cn.rtast.rob.entity.IMessageChain
1413
import cn.rtast.rob.event.raw.message.GroupMessage
1514
import cn.rtast.rob.event.raw.message.PrivateMessage
1615
import cn.rtast.rob.event.raw.message.first
1716
import cn.rtast.rob.interceptor.CommandInterceptor
18-
import cn.rtast.rob.onebot.MessageChain
17+
import cn.rtast.rob.session.IGroupSession
18+
import cn.rtast.rob.session.IPrivateSession
1919

2020

2121
public abstract class BaseCommand(
22-
public val interceptor: CommandInterceptor? = null
23-
) : IBaseCommand<GroupMessage, PrivateMessage> {
22+
public val interceptor: CommandInterceptor? = null,
23+
) : IBaseCommand<GroupMessage, PrivateMessage, IGroupSession, IPrivateSession> {
2424
abstract override val commandNames: List<String>
2525
override suspend fun executeGroup(message: GroupMessage, args: List<String>) {}
2626
override suspend fun executePrivate(message: PrivateMessage, args: List<String>) {}
27-
override suspend fun onGroupSession(message: GroupMessage) {}
28-
override suspend fun onGroupSession(message: GroupMessage, initArg: Any) {}
29-
override suspend fun onPrivateSession(message: PrivateMessage) {}
30-
override suspend fun onPrivateSession(message: PrivateMessage, initArg: Any) {}
27+
final override suspend fun <T> startGroupSession(init: T, block: IGroupSession) {
28+
29+
}
30+
31+
final override suspend fun <T> startPrivateSession(init: T, block: IPrivateSession) {
32+
33+
}
3134

3235
final override suspend fun handlePrivate(
3336
message: PrivateMessage,
@@ -48,38 +51,4 @@ public abstract class BaseCommand(
4851
val args = message.first.split(" ").drop(1)
4952
this.executeGroup(message, args)
5053
}
51-
52-
final override suspend fun GroupMessage.reject(reason: IMessageChain) {
53-
val chainReason = reason as MessageChain
54-
this.reply(chainReason)
55-
}
56-
57-
final override suspend fun PrivateMessage.reject(reason: IMessageChain) {
58-
val chainReason = reason as MessageChain
59-
this.reply(chainReason)
60-
}
61-
62-
final override suspend fun GroupMessage.skipSession() {
63-
OneBotFactory.sessionManager.endGroupSession(this.sender)
64-
}
65-
66-
final override suspend fun PrivateMessage.skipSession() {
67-
OneBotFactory.sessionManager.endPrivateSession(this.sender)
68-
}
69-
70-
final override suspend fun GroupMessage.startSession() {
71-
OneBotFactory.sessionManager.startGroupSession(this, this@BaseCommand)
72-
}
73-
74-
final override suspend fun <T : Any> GroupMessage.startSession(initArg: T) {
75-
OneBotFactory.sessionManager.startGroupSession(this, this@BaseCommand, initArg)
76-
}
77-
78-
final override suspend fun PrivateMessage.startSession() {
79-
OneBotFactory.sessionManager.startPrivateSession(this, this@BaseCommand)
80-
}
81-
82-
final override suspend fun <T : Any> PrivateMessage.startSession(initArg: T) {
83-
OneBotFactory.sessionManager.startPrivateSession(this, this@BaseCommand, initArg)
84-
}
8554
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ public class CommandManagerImpl internal constructor() : CommandManager<BaseComm
3838
val activeSession = OneBotFactory.sessionManager.getPrivateSession(message.sender)
3939
val (command, commandName) = this.getCommand(message)
4040
if (activeSession != null) {
41-
activeSession.command.onPrivateSession(message)
42-
activeSession.command.onPrivateSession(message, activeSession.initArgType)
41+
activeSession.command.startGroupSession(message)
42+
activeSession.command.startGroupSession(message, activeSession.initArgType)
4343
return
4444
}
4545
val commandString = commandRegex.find(message.text)?.value
@@ -65,8 +65,8 @@ public class CommandManagerImpl internal constructor() : CommandManager<BaseComm
6565
val activeSession = OneBotFactory.sessionManager.getGroupSession(message.sender)
6666
val (command, commandName) = this.getCommand(message)
6767
if (activeSession != null && activeSession.sender.groupId == message.groupId) {
68-
activeSession.command.onGroupSession(message)
69-
activeSession.command.onGroupSession(message, activeSession.initArgType)
68+
activeSession.command.startGroupSession(message)
69+
activeSession.command.startGroupSession(message, activeSession.initArgType)
7070
return
7171
}
7272
val commandString = commandRegex.find(message.text)?.value

0 commit comments

Comments
 (0)