Skip to content

Commit d39e8b8

Browse files
authored
Merge pull request #1019 from simple-robot/internal-interaction-event
增加若干内部事件、内部行为事件和相关的消息拦截事件
2 parents 4d20817 + 907bc6c commit d39e8b8

File tree

13 files changed

+1298
-8
lines changed

13 files changed

+1298
-8
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ fun Application.registerListeners() {
142142
## 协助我们
143143
为我们点亮一个 **✨star🌟** 便是能够给予我们继续走下去的最大动力与支持!
144144

145-
- 阅读 [贡献指南](docs/CONTRIBUTING_CN.md) 来了解如何贡献你的力量!
145+
- 阅读 [**贡献指南**](docs/CONTRIBUTING_CN.md) 来了解如何贡献你的力量!
146146
- 你可以通过 [**讨论区**][discussions] 与其他人或者simbot开发团队相互友好交流。
147147
- 如果你通过此项目创建了一个很酷的开源项目,欢迎通过 [ISSUES][issues][讨论区][discussions]
148148
等方式留下你的开源项目信息,并将你酷酷的项目展示在作品展示区。

docs/CONTRIBUTING.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,7 @@ and the [Simple Robot Library](https://github.com/simple-robot-library)!
1616

1717
### Pull Request
1818

19+
> [!info]
20+
> If you want to contribute code, please submit a Pull Request to the `v4-dev` development branch.
21+
1922
TODO

docs/CONTRIBUTING_CN.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ override val developers: List<Developer> = developers {
5757
为其他来者留下一盏指路的明灯~
5858

5959
## 代码贡献
60+
61+
> [!info]
62+
> 如果您要提交代码贡献,请向开发分支 `v4-dev` 提交 PR 。
63+
6064
### 注释风格
6165

6266
simbot核心库对源代码的注释有着一些约定。

simbot-api/api/simbot-api.api

Lines changed: 243 additions & 1 deletion
Large diffs are not rendered by default.

simbot-api/src/commonMain/kotlin/love/forte/simbot/ability/ReplySupport.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/*
2-
* Copyright (c) 2024. ForteScarlet.
2+
* Copyright (c) 2024-2025. ForteScarlet.
33
*
44
* Project https://github.com/simple-robot/simpler-robot
55
66
*
7-
* This file is part of the Simple Robot Library.
7+
* This file is part of the Simple Robot Library (Alias: simple-robot, simbot, etc.).
88
*
99
* This program is free software: you can redistribute it and/or modify
1010
* it under the terms of the GNU Lesser General Public License as published by
@@ -23,6 +23,7 @@
2323

2424
package love.forte.simbot.ability
2525

26+
import love.forte.simbot.event.InternalInterceptionException
2627
import love.forte.simbot.event.MessageEvent
2728
import love.forte.simbot.message.Message
2829
import love.forte.simbot.message.MessageContent
@@ -45,6 +46,8 @@ public interface ReplySupport {
4546
* 发送一段纯文本消息。
4647
*
4748
* @return 消息发送成功后的回执
49+
* @throws InternalInterceptionException 在拦截事件处理过程中产生的异常,
50+
* 每一个具体的异常都会被收集在 [InternalInterceptionException.suppressedExceptions] 中。
4851
* @throws Exception 可能产生任何异常
4952
*/
5053
public suspend fun reply(text: String): MessageReceipt
@@ -53,6 +56,8 @@ public interface ReplySupport {
5356
* 发送一个消息 [Message]。
5457
*
5558
* @return 消息发送成功后的回执
59+
* @throws InternalInterceptionException 在拦截事件处理过程中产生的异常,
60+
* 每一个具体的异常都会被收集在 [InternalInterceptionException.suppressedExceptions] 中。
5661
* @throws Exception 可能产生任何异常
5762
*/
5863
public suspend fun reply(message: Message): MessageReceipt
@@ -63,6 +68,8 @@ public interface ReplySupport {
6368
* 并在不支持的情况下降级为使用 [MessageContent.messages]。
6469
*
6570
* @return 消息发送成功后的回执
71+
* @throws InternalInterceptionException 在拦截事件处理过程中产生的异常,
72+
* 每一个具体的异常都会被收集在 [InternalInterceptionException.suppressedExceptions] 中。
6673
* @throws Exception 可能产生任何异常
6774
*/
6875
public suspend fun reply(messageContent: MessageContent): MessageReceipt

simbot-api/src/commonMain/kotlin/love/forte/simbot/ability/SendSupport.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/*
2-
* Copyright (c) 2024. ForteScarlet.
2+
* Copyright (c) 2024-2025. ForteScarlet.
33
*
44
* Project https://github.com/simple-robot/simpler-robot
55
66
*
7-
* This file is part of the Simple Robot Library.
7+
* This file is part of the Simple Robot Library (Alias: simple-robot, simbot, etc.).
88
*
99
* This program is free software: you can redistribute it and/or modify
1010
* it under the terms of the GNU Lesser General Public License as published by
@@ -25,6 +25,7 @@ package love.forte.simbot.ability
2525

2626
import love.forte.simbot.definition.Actor
2727
import love.forte.simbot.definition.Contact
28+
import love.forte.simbot.event.InternalInterceptionException
2829
import love.forte.simbot.message.Message
2930
import love.forte.simbot.message.MessageContent
3031
import love.forte.simbot.message.MessageReceipt
@@ -42,6 +43,8 @@ public interface SendSupport {
4243
* 发送一段纯文本消息。
4344
*
4445
* @return 消息发送成功后的回执
46+
* @throws InternalInterceptionException 在拦截事件处理过程中产生的异常,
47+
* 每一个具体的异常都会被收集在 [InternalInterceptionException.suppressedExceptions] 中。
4548
* @throws Exception 可能产生任何异常
4649
*/
4750
public suspend fun send(text: String): MessageReceipt
@@ -50,6 +53,8 @@ public interface SendSupport {
5053
* 发送一个消息 [Message]。
5154
*
5255
* @return 消息发送成功后的回执
56+
* @throws InternalInterceptionException 在拦截事件处理过程中产生的异常,
57+
* 每一个具体的异常都会被收集在 [InternalInterceptionException.suppressedExceptions] 中。
5358
* @throws Exception 可能产生任何异常
5459
*/
5560
public suspend fun send(message: Message): MessageReceipt
@@ -60,6 +65,8 @@ public interface SendSupport {
6065
* 并在不支持的情况下降级为使用 [MessageContent.messages]。
6166
*
6267
* @return 消息发送成功后的回执
68+
* @throws InternalInterceptionException 在拦截事件处理过程中产生的异常,
69+
* 每一个具体的异常都会被收集在 [InternalInterceptionException.suppressedExceptions] 中。
6370
* @throws Exception 可能产生任何异常
6471
*/
6572
public suspend fun send(messageContent: MessageContent): MessageReceipt

simbot-api/src/commonMain/kotlin/love/forte/simbot/event/BotStageEvent.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2024. ForteScarlet.
2+
* Copyright (c) 2024-2025. ForteScarlet.
33
*
44
* Project https://github.com/simple-robot/simpler-robot
55
@@ -33,7 +33,7 @@ import love.forte.simbot.bot.BotManager
3333
*
3434
* @author ForteScarlet
3535
*/
36-
public interface BotStageEvent : BotEvent {
36+
public interface BotStageEvent : InternalNotificationEvent, BotEvent {
3737
/**
3838
* 相关的 bot.
3939
*/
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/*
2+
* Copyright (c) 2025. ForteScarlet.
3+
*
4+
* Project https://github.com/simple-robot/simpler-robot
5+
6+
*
7+
* This file is part of the Simple Robot Library (Alias: simple-robot, simbot, etc.).
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Lesser General Public License as published by
11+
* the Free Software Foundation, either version 3 of the License, or
12+
* (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* Lesser GNU General Public License for more details.
18+
*
19+
* You should have received a copy of the Lesser GNU General Public License
20+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
21+
*
22+
*/
23+
24+
package love.forte.simbot.event
25+
26+
import love.forte.simbot.ability.ReplySupport
27+
import love.forte.simbot.ability.SendSupport
28+
import kotlin.jvm.JvmStatic
29+
30+
/**
31+
* 拦截或通知中 [SendSupport.send] 或 [ReplySupport.reply] 的消息内容。
32+
*/
33+
public sealed class InteractionMessage {
34+
/**
35+
* 当参数类型为 [String] 时,表示发送的文本消息。
36+
*/
37+
public class Text internal constructor(public val text: String) : InteractionMessage() {
38+
override fun equals(other: Any?): Boolean {
39+
if (this === other) return true
40+
if (other !is Text) return false
41+
42+
if (text != other.text) return false
43+
44+
return true
45+
}
46+
47+
override fun hashCode(): Int {
48+
return text.hashCode()
49+
}
50+
51+
override fun toString(): String {
52+
return "Text(text='$text')"
53+
}
54+
}
55+
56+
/**
57+
* 当参数类型为 [love.forte.simbot.message.Message] 时,表示发送的消息。
58+
*/
59+
public class Message internal constructor(public val message: love.forte.simbot.message.Message) :
60+
InteractionMessage() {
61+
override fun equals(other: Any?): Boolean {
62+
if (this === other) return true
63+
if (other !is Message) return false
64+
65+
if (message != other.message) return false
66+
67+
return true
68+
}
69+
70+
override fun hashCode(): Int {
71+
return message.hashCode()
72+
}
73+
74+
override fun toString(): String {
75+
return "Message(message=$message)"
76+
}
77+
78+
}
79+
80+
/**
81+
* 当参数类型为 [love.forte.simbot.message.MessageContent] 时,表示发送的消息内容。
82+
*/
83+
public class MessageContent internal constructor(
84+
public val messageContent: love.forte.simbot.message.MessageContent
85+
) : InteractionMessage() {
86+
override fun equals(other: Any?): Boolean {
87+
if (this === other) return true
88+
if (other !is MessageContent) return false
89+
90+
if (messageContent != other.messageContent) return false
91+
92+
return true
93+
}
94+
95+
override fun hashCode(): Int {
96+
return messageContent.hashCode()
97+
}
98+
99+
override fun toString(): String {
100+
return "MessageContent(messageContent=$messageContent)"
101+
}
102+
}
103+
104+
/**
105+
* 如果组件或 [SendSupport] 的实现者提供了其他三个类型参数以外的参数,
106+
* 则需要通过 [Extension] 对其进行扩展。
107+
*/
108+
public abstract class Extension : InteractionMessage()
109+
110+
public companion object {
111+
/**
112+
* 创建一个文本消息。
113+
* @see InteractionMessage
114+
*/
115+
@JvmStatic
116+
public fun valueOf(text: String): Text = Text(text)
117+
118+
/**
119+
* 创建一个消息。
120+
* @see InteractionMessage
121+
*/
122+
@JvmStatic
123+
public fun valueOf(message: love.forte.simbot.message.Message): Message = Message(message)
124+
125+
/**
126+
* 创建一个消息内容。
127+
* @see InteractionMessage
128+
*/
129+
@JvmStatic
130+
public fun valueOf(messageContent: love.forte.simbot.message.MessageContent): MessageContent =
131+
MessageContent(messageContent)
132+
}
133+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright (c) 2025. ForteScarlet.
3+
*
4+
* Project https://github.com/simple-robot/simpler-robot
5+
6+
*
7+
* This file is part of the Simple Robot Library (Alias: simple-robot, simbot, etc.).
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Lesser General Public License as published by
11+
* the Free Software Foundation, either version 3 of the License, or
12+
* (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* Lesser GNU General Public License for more details.
18+
*
19+
* You should have received a copy of the Lesser GNU General Public License
20+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
21+
*
22+
*/
23+
24+
package love.forte.simbot.event
25+
26+
27+
/**
28+
* 一个内部事件。
29+
* 用于表示一个仅在内部流转、与外界无关的事件,
30+
* 通常用于一些内部的状态通知或功能拦截,例如 [BotStageEvent] 或 [SendSupportInteractionEvent] 等。
31+
*
32+
* 通常由组件实现进行扩展,不过也提供了一些默认的定义。
33+
*
34+
* @see InternalNotificationEvent
35+
* @see InternalInterceptionEvent
36+
* @see BotStageEvent
37+
* @see SendSupportInteractionEvent
38+
*
39+
* @since 4.11.0
40+
* @author ForteScarlet
41+
*/
42+
public interface InternalEvent : Event
43+
44+
/**
45+
* 一个内部通知事件。
46+
* 通知性质的内部事件通常仅用作“通知”,即它不会对某些行为造成影响。
47+
*
48+
* @since 4.11.0
49+
*/
50+
public interface InternalNotificationEvent : InternalEvent
51+
52+
/**
53+
* 一个内部拦截事件。
54+
* 拦截性质的内部事件通常用作“拦截”,即它会对某些行为进行拦截,并有可能会产生影响,
55+
* 例如改变原本行为的参数、或者通过抛出异常直接阻止某些行为的发生。
56+
* 例如针对 [SendSupport.send][love.forte.simbot.ability.SendSupport.send]
57+
* 进行拦截,并改变其入参。
58+
*
59+
* ## 异常处理
60+
*
61+
* 在拦截过程中,如果产生了异常,它们不会影响后续事件的继续调度,
62+
* 而所有产生的异常都会被收集到 [InternalInterceptionException.suppressedExceptions] 中,
63+
* 并在最终抛出并影响原函数的执行。
64+
*
65+
* @since 4.11.0
66+
*
67+
*/
68+
public interface InternalInterceptionEvent : InternalEvent
69+
70+
/**
71+
* [InternalInterceptionEvent] 中产生的异常的收集与包装。
72+
* 更多参考 [InternalInterceptionEvent] 中有关异常的说明。
73+
*
74+
* @see InternalInterceptionEvent
75+
* @since 4.11.0
76+
*/
77+
public open class InternalInterceptionException : RuntimeException {
78+
public constructor() : super()
79+
public constructor(message: String?) : super(message)
80+
public constructor(message: String?, cause: Throwable?) : super(message, cause)
81+
public constructor(cause: Throwable?) : super(cause)
82+
}

0 commit comments

Comments
 (0)