Skip to content

Commit 814c5d9

Browse files
authored
Merge pull request #95 from simple-robot/dev/support-msg-from-id
feat: 支持 OneBotMessageContent 和 OneBotBot 中与根据id或引用查询消息内容的API
2 parents b835e4c + 18e61db commit 814c5d9

File tree

6 files changed

+75
-7
lines changed

6 files changed

+75
-7
lines changed

simbot-component-onebot-v11/simbot-component-onebot-v11-core/api/simbot-component-onebot-v11-core.api

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1810,6 +1810,8 @@ public abstract interface class love/forte/simbot/component/onebot/v11/core/bot/
18101810
public abstract fun getName ()Ljava/lang/String;
18111811
public abstract fun getUserId ()Llove/forte/simbot/common/id/ID;
18121812
public abstract fun isMe (Llove/forte/simbot/common/id/ID;)Z
1813+
public synthetic fun messageFromIdBlocking (Llove/forte/simbot/common/id/ID;)Llove/forte/simbot/message/MessageContent;
1814+
public synthetic fun messageFromReferenceBlocking (Llove/forte/simbot/message/MessageReference;)Llove/forte/simbot/message/MessageContent;
18131815
public abstract fun push (Ljava/lang/String;)Lkotlinx/coroutines/flow/Flow;
18141816
public fun pushAndLaunch (Ljava/lang/String;)Lkotlinx/coroutines/Job;
18151817
public abstract synthetic fun queryLoginInfo (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;

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

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
package love.forte.simbot.component.onebot.v11.core.bot
1919

2020
import io.ktor.client.*
21-
import io.ktor.client.statement.*
2221
import io.ktor.http.*
2322
import kotlinx.coroutines.Job
2423
import kotlinx.coroutines.flow.Flow
@@ -42,6 +41,7 @@ import love.forte.simbot.component.onebot.v11.core.actor.OneBotStranger
4241
import love.forte.simbot.component.onebot.v11.core.api.*
4342
import love.forte.simbot.component.onebot.v11.message.OneBotMessageContent
4443
import love.forte.simbot.event.EventResult
44+
import love.forte.simbot.message.MessageReference
4545
import love.forte.simbot.suspendrunner.ST
4646
import kotlin.coroutines.CoroutineContext
4747
import kotlin.jvm.JvmSynthetic
@@ -241,13 +241,50 @@ public interface OneBotBot : Bot, OneBotApiExecutable {
241241
* 注意:此API是实验性的,未来可能会随时被变更或删除。
242242
*
243243
* @see GetMsgApi
244-
* @throws Throwable 任何API请求过程中可能产生的异常,
245-
* 例如消息不存在
244+
* @throws RuntimeException 任何API请求过程中可能产生的异常,
245+
* 例如消息不存在或反序列化错误。
246246
*/
247247
@ST
248248
@ExperimentalOneBotAPI
249249
public suspend fun getMessageContent(messageId: ID): OneBotMessageContent
250250

251+
/**
252+
* 根据 [id] 使用 [GetMsgApi] 查询消息内容,
253+
* 并得到对应的 [OneBotMessageContent]。
254+
*
255+
* 注意:此API是实验性的,未来可能会随时被变更或删除,
256+
* 同 [getMessageContent]。
257+
*
258+
* @see GetMsgApi
259+
* @see getMessageContent
260+
* @throws RuntimeException 任何API请求过程中可能产生的异常,
261+
* 例如消息不存在或反序列化错误。
262+
*
263+
* @since 1.3.0
264+
*/
265+
@ST
266+
@ExperimentalOneBotAPI
267+
override suspend fun messageFromId(id: ID): OneBotMessageContent = getMessageContent(id)
268+
269+
/**
270+
* 根据消息引用的id使用 [GetMsgApi] 查询消息内容,
271+
* 并得到对应的 [OneBotMessageContent]。
272+
*
273+
* 注意:此API是实验性的,未来可能会随时被变更或删除,
274+
* 同 [getMessageContent]。
275+
*
276+
* @see getMessageContent
277+
*
278+
* @see GetMsgApi
279+
* @throws RuntimeException 任何API请求过程中可能产生的异常,
280+
* 例如消息不存在或反序列化错误。
281+
*
282+
* @since 1.3.0
283+
*/
284+
@ST
285+
@ExperimentalOneBotAPI
286+
override suspend fun messageFromReference(reference: MessageReference): OneBotMessageContent =
287+
getMessageContent(id)
251288

252289
}
253290

simbot-component-onebot-v11/simbot-component-onebot-v11-core/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/core/internal/message/OneBotMessageContentImpl.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@ import love.forte.simbot.ability.DeleteOption
2121
import love.forte.simbot.ability.StandardDeleteOption
2222
import love.forte.simbot.common.id.ID
2323
import love.forte.simbot.component.onebot.v11.core.api.DeleteMsgApi
24+
import love.forte.simbot.component.onebot.v11.core.api.GetMsgApi
2425
import love.forte.simbot.component.onebot.v11.core.bot.internal.OneBotBotImpl
2526
import love.forte.simbot.component.onebot.v11.message.OneBotMessageContent
2627
import love.forte.simbot.component.onebot.v11.message.resolveToMessageElement
2728
import love.forte.simbot.component.onebot.v11.message.segment.OneBotMessageSegment
29+
import love.forte.simbot.component.onebot.v11.message.segment.OneBotReply
2830
import love.forte.simbot.component.onebot.v11.message.segment.OneBotText
2931
import love.forte.simbot.message.Messages
3032
import love.forte.simbot.message.toMessages
@@ -56,6 +58,13 @@ internal class OneBotMessageContentImpl(
5658
sb?.toString()
5759
}
5860

61+
override suspend fun referenceMessage(): OneBotMessageContent? {
62+
val ref = messages.firstNotNullOfOrNull { it as? OneBotReply }
63+
?: return null
64+
65+
return bot.getMessageContent(ref.id)
66+
}
67+
5968
override suspend fun delete(vararg options: DeleteOption) {
6069
runCatching {
6170
bot.executeData(DeleteMsgApi.create(id))

simbot-component-onebot-v11/simbot-component-onebot-v11-message/api/simbot-component-onebot-v11-message.api

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
public abstract interface class love/forte/simbot/component/onebot/v11/message/OneBotMessageContent : love/forte/simbot/message/MessageContent {
2-
public abstract fun delete ([Llove/forte/simbot/ability/DeleteOption;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
2+
public abstract synthetic fun delete ([Llove/forte/simbot/ability/DeleteOption;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
33
public abstract fun getId ()Llove/forte/simbot/common/id/ID;
44
public abstract fun getMessages ()Llove/forte/simbot/message/Messages;
55
public abstract fun getPlainText ()Ljava/lang/String;
6+
public fun getReference ()Llove/forte/simbot/component/onebot/v11/message/segment/OneBotReply;
7+
public synthetic fun getReference ()Llove/forte/simbot/message/MessageReference;
8+
public fun getReferenceAsync ()Ljava/util/concurrent/CompletableFuture;
9+
public fun getReferenceMessage ()Llove/forte/simbot/component/onebot/v11/message/OneBotMessageContent;
10+
public synthetic fun getReferenceMessage ()Llove/forte/simbot/message/MessageContent;
11+
public fun getReferenceMessageAsync ()Ljava/util/concurrent/CompletableFuture;
12+
public fun getReferenceMessageReserve ()Llove/forte/simbot/suspendrunner/reserve/SuspendReserve;
13+
public fun getReferenceReserve ()Llove/forte/simbot/suspendrunner/reserve/SuspendReserve;
614
public abstract fun getSourceSegments ()Ljava/util/List;
7-
public fun reference (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
15+
public synthetic fun reference (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
816
public static synthetic fun reference$suspendImpl (Llove/forte/simbot/component/onebot/v11/message/OneBotMessageContent;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
17+
public abstract synthetic fun referenceMessage (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
918
}
1019

1120
public abstract interface class love/forte/simbot/component/onebot/v11/message/OneBotMessageElement : love/forte/simbot/message/Message$Element {

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ plugins {
2828
kotlin("multiplatform")
2929
kotlin("plugin.serialization")
3030
`simbot-onebot-dokka-partial-configure`
31-
// 没用到
32-
// `simbot-onebot-suspend-transform-configure`
31+
`simbot-onebot-suspend-transform-configure`
3332

3433
alias(libs.plugins.ksp)
3534
}

simbot-component-onebot-v11/simbot-component-onebot-v11-message/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/message/OneBotMessageContent.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import love.forte.simbot.message.MessageContent
2828
import love.forte.simbot.message.Messages
2929
import love.forte.simbot.message.PlainText
3030
import love.forte.simbot.suspendrunner.STP
31+
import kotlin.jvm.JvmSynthetic
3132

3233

3334
/**
@@ -70,6 +71,16 @@ public interface OneBotMessageContent : MessageContent {
7071
override suspend fun reference(): OneBotReply? =
7172
messages.firstNotNullOfOrNull { it as? OneBotReply }
7273

74+
/**
75+
* 根据 [消息引用][reference] 信息通过API查询对应引用的消息内容。
76+
*
77+
* @throws RuntimeException 任何可能在请求API过程中产生的异常
78+
*
79+
* @since 1.3.0
80+
*/
81+
@STP
82+
override suspend fun referenceMessage(): OneBotMessageContent?
83+
7384
/**
7485
* 消息中所有的 [文本消息][PlainText]
7586
* (或者说 [sourceSegments] 中所有的 [OneBotText])
@@ -88,6 +99,7 @@ public interface OneBotMessageContent : MessageContent {
8899
* @throws Exception 任何请求API过程中可能会产生的异常,
89100
* 例如因权限不足或消息不存在得到的请求错误
90101
*/
102+
@JvmSynthetic
91103
override suspend fun delete(vararg options: DeleteOption)
92104
}
93105

0 commit comments

Comments
 (0)