Skip to content

Commit

Permalink
Merge pull request #95 from simple-robot/dev/support-msg-from-id
Browse files Browse the repository at this point in the history
feat: 支持 OneBotMessageContent 和 OneBotBot 中与根据id或引用查询消息内容的API
  • Loading branch information
ForteScarlet authored Aug 17, 2024
2 parents b835e4c + 18e61db commit 814c5d9
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1810,6 +1810,8 @@ public abstract interface class love/forte/simbot/component/onebot/v11/core/bot/
public abstract fun getName ()Ljava/lang/String;
public abstract fun getUserId ()Llove/forte/simbot/common/id/ID;
public abstract fun isMe (Llove/forte/simbot/common/id/ID;)Z
public synthetic fun messageFromIdBlocking (Llove/forte/simbot/common/id/ID;)Llove/forte/simbot/message/MessageContent;
public synthetic fun messageFromReferenceBlocking (Llove/forte/simbot/message/MessageReference;)Llove/forte/simbot/message/MessageContent;
public abstract fun push (Ljava/lang/String;)Lkotlinx/coroutines/flow/Flow;
public fun pushAndLaunch (Ljava/lang/String;)Lkotlinx/coroutines/Job;
public abstract synthetic fun queryLoginInfo (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package love.forte.simbot.component.onebot.v11.core.bot

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

/**
* 根据 [id] 使用 [GetMsgApi] 查询消息内容,
* 并得到对应的 [OneBotMessageContent]。
*
* 注意:此API是实验性的,未来可能会随时被变更或删除,
* 同 [getMessageContent]。
*
* @see GetMsgApi
* @see getMessageContent
* @throws RuntimeException 任何API请求过程中可能产生的异常,
* 例如消息不存在或反序列化错误。
*
* @since 1.3.0
*/
@ST
@ExperimentalOneBotAPI
override suspend fun messageFromId(id: ID): OneBotMessageContent = getMessageContent(id)

/**
* 根据消息引用的id使用 [GetMsgApi] 查询消息内容,
* 并得到对应的 [OneBotMessageContent]。
*
* 注意:此API是实验性的,未来可能会随时被变更或删除,
* 同 [getMessageContent]。
*
* @see getMessageContent
*
* @see GetMsgApi
* @throws RuntimeException 任何API请求过程中可能产生的异常,
* 例如消息不存在或反序列化错误。
*
* @since 1.3.0
*/
@ST
@ExperimentalOneBotAPI
override suspend fun messageFromReference(reference: MessageReference): OneBotMessageContent =
getMessageContent(id)

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ import love.forte.simbot.ability.DeleteOption
import love.forte.simbot.ability.StandardDeleteOption
import love.forte.simbot.common.id.ID
import love.forte.simbot.component.onebot.v11.core.api.DeleteMsgApi
import love.forte.simbot.component.onebot.v11.core.api.GetMsgApi
import love.forte.simbot.component.onebot.v11.core.bot.internal.OneBotBotImpl
import love.forte.simbot.component.onebot.v11.message.OneBotMessageContent
import love.forte.simbot.component.onebot.v11.message.resolveToMessageElement
import love.forte.simbot.component.onebot.v11.message.segment.OneBotMessageSegment
import love.forte.simbot.component.onebot.v11.message.segment.OneBotReply
import love.forte.simbot.component.onebot.v11.message.segment.OneBotText
import love.forte.simbot.message.Messages
import love.forte.simbot.message.toMessages
Expand Down Expand Up @@ -56,6 +58,13 @@ internal class OneBotMessageContentImpl(
sb?.toString()
}

override suspend fun referenceMessage(): OneBotMessageContent? {
val ref = messages.firstNotNullOfOrNull { it as? OneBotReply }
?: return null

return bot.getMessageContent(ref.id)
}

override suspend fun delete(vararg options: DeleteOption) {
runCatching {
bot.executeData(DeleteMsgApi.create(id))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
public abstract interface class love/forte/simbot/component/onebot/v11/message/OneBotMessageContent : love/forte/simbot/message/MessageContent {
public abstract fun delete ([Llove/forte/simbot/ability/DeleteOption;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public abstract synthetic fun delete ([Llove/forte/simbot/ability/DeleteOption;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public abstract fun getId ()Llove/forte/simbot/common/id/ID;
public abstract fun getMessages ()Llove/forte/simbot/message/Messages;
public abstract fun getPlainText ()Ljava/lang/String;
public fun getReference ()Llove/forte/simbot/component/onebot/v11/message/segment/OneBotReply;
public synthetic fun getReference ()Llove/forte/simbot/message/MessageReference;
public fun getReferenceAsync ()Ljava/util/concurrent/CompletableFuture;
public fun getReferenceMessage ()Llove/forte/simbot/component/onebot/v11/message/OneBotMessageContent;
public synthetic fun getReferenceMessage ()Llove/forte/simbot/message/MessageContent;
public fun getReferenceMessageAsync ()Ljava/util/concurrent/CompletableFuture;
public fun getReferenceMessageReserve ()Llove/forte/simbot/suspendrunner/reserve/SuspendReserve;
public fun getReferenceReserve ()Llove/forte/simbot/suspendrunner/reserve/SuspendReserve;
public abstract fun getSourceSegments ()Ljava/util/List;
public fun reference (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public synthetic fun reference (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static synthetic fun reference$suspendImpl (Llove/forte/simbot/component/onebot/v11/message/OneBotMessageContent;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public abstract synthetic fun referenceMessage (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
}

public abstract interface class love/forte/simbot/component/onebot/v11/message/OneBotMessageElement : love/forte/simbot/message/Message$Element {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ plugins {
kotlin("multiplatform")
kotlin("plugin.serialization")
`simbot-onebot-dokka-partial-configure`
// 没用到
// `simbot-onebot-suspend-transform-configure`
`simbot-onebot-suspend-transform-configure`

alias(libs.plugins.ksp)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import love.forte.simbot.message.MessageContent
import love.forte.simbot.message.Messages
import love.forte.simbot.message.PlainText
import love.forte.simbot.suspendrunner.STP
import kotlin.jvm.JvmSynthetic


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

/**
* 根据 [消息引用][reference] 信息通过API查询对应引用的消息内容。
*
* @throws RuntimeException 任何可能在请求API过程中产生的异常
*
* @since 1.3.0
*/
@STP
override suspend fun referenceMessage(): OneBotMessageContent?

/**
* 消息中所有的 [文本消息][PlainText]

Check warning on line 85 in simbot-component-onebot-v11/simbot-component-onebot-v11-message/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/message/OneBotMessageContent.kt

View workflow job for this annotation

GitHub Actions / qodana

Unresolved reference in KDoc

Cannot resolve symbol 'PlainText'
* (或者说 [sourceSegments] 中所有的 [OneBotText])
Expand All @@ -88,6 +99,7 @@ public interface OneBotMessageContent : MessageContent {
* @throws Exception 任何请求API过程中可能会产生的异常,
* 例如因权限不足或消息不存在得到的请求错误
*/
@JvmSynthetic
override suspend fun delete(vararg options: DeleteOption)
}

Expand Down

0 comments on commit 814c5d9

Please sign in to comment.