Skip to content

Commit

Permalink
Merge pull request #42 from simple-robot/dev/bot-support-query-msg
Browse files Browse the repository at this point in the history
OneBotBot 增加实验性API getMessageContent 允许通过messageId查询消息内容
  • Loading branch information
ForteScarlet authored Jun 16, 2024
2 parents 561ce5a + 6c632d7 commit d6adcff
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 5 deletions.
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/P.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ object P {
override val homepage: String get() = HOMEPAGE


private val baseVersion = v(0, 5, 0)
private val baseVersion = v(0, 6, 0)

val snapshotVersion = baseVersion - Version.SNAPSHOT
override val version = if (isSnapshot()) snapshotVersion else baseVersion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ import love.forte.simbot.bot.GroupRelation
import love.forte.simbot.bot.GuildRelation
import love.forte.simbot.common.collectable.Collectable
import love.forte.simbot.common.id.ID
import love.forte.simbot.component.onebot.common.annotations.ExperimentalOneBotAPI
import love.forte.simbot.component.onebot.common.annotations.InternalOneBotAPI
import love.forte.simbot.component.onebot.common.annotations.OneBotInternalImplementationsOnly
import love.forte.simbot.component.onebot.v11.core.actor.OneBotFriend
import love.forte.simbot.component.onebot.v11.core.actor.OneBotGroup
import love.forte.simbot.component.onebot.v11.core.actor.OneBotMember
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.suspendrunner.ST
import kotlin.coroutines.CoroutineContext
import kotlin.jvm.JvmSynthetic
Expand Down Expand Up @@ -181,6 +183,19 @@ public interface OneBotBot : Bot {
@ST
public suspend fun getCsrfToken(): GetCsrfTokenResult

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

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import love.forte.simbot.component.onebot.v11.core.event.internal.notice.*
import love.forte.simbot.component.onebot.v11.core.event.internal.request.OneBotFriendRequestEventImpl
import love.forte.simbot.component.onebot.v11.core.event.internal.request.OneBotGroupRequestEventImpl
import love.forte.simbot.component.onebot.v11.core.event.internal.stage.OneBotBotStartedEventImpl
import love.forte.simbot.component.onebot.v11.core.internal.message.OneBotMessageContentImpl
import love.forte.simbot.component.onebot.v11.core.utils.onEachErrorLog
import love.forte.simbot.component.onebot.v11.event.UnknownEvent
import love.forte.simbot.component.onebot.v11.event.message.RawGroupMessageEvent
Expand All @@ -71,6 +72,7 @@ import love.forte.simbot.component.onebot.v11.event.request.RawFriendRequestEven
import love.forte.simbot.component.onebot.v11.event.request.RawGroupRequestEvent
import love.forte.simbot.component.onebot.v11.event.resolveEventSerializer
import love.forte.simbot.component.onebot.v11.event.resolveEventSubTypeFieldName
import love.forte.simbot.component.onebot.v11.message.OneBotMessageContent
import love.forte.simbot.event.Event
import love.forte.simbot.event.EventProcessor
import love.forte.simbot.logger.LoggerFactory
Expand Down Expand Up @@ -564,6 +566,15 @@ internal class OneBotBotImpl(
override suspend fun getCsrfToken(): GetCsrfTokenResult =
GetCsrfTokenApi.create().requestDataBy(this)

override suspend fun getMessageContent(messageId: ID): OneBotMessageContent {
val result = GetMsgApi.create(messageId).requestDataBy(this)
return OneBotMessageContentImpl(
result.messageId,
result.message,
this
)
}

override fun toString(): String =
"OneBotBot(uniqueId='$uniqueId', isStarted=$isStarted, isActive=$isActive)"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ internal abstract class OneBotGroupMessageEventImpl(
override val authorId: ID
get() = sourceEvent.userId

override val messageContent: MessageContent = OneBotMessageContentImpl(
override val messageContent: OneBotMessageContent = OneBotMessageContentImpl(
sourceEvent.messageId,
sourceEvent.message,
bot
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ internal abstract class OneBotPrivateMessageEventImpl(
override val authorId: ID
get() = sourceEvent.userId

override val messageContent: MessageContent = OneBotMessageContentImpl(
override val messageContent: OneBotMessageContent = OneBotMessageContentImpl(
sourceEvent.messageId,
sourceEvent.message,
bot
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import love.forte.simbot.common.id.LongID
import love.forte.simbot.component.onebot.v11.core.actor.OneBotGroup
import love.forte.simbot.component.onebot.v11.core.actor.OneBotMember
import love.forte.simbot.component.onebot.v11.event.message.RawGroupMessageEvent
import love.forte.simbot.component.onebot.v11.message.OneBotMessageContent
import love.forte.simbot.event.ChatGroupEvent
import love.forte.simbot.event.ChatGroupMessageEvent
import love.forte.simbot.suspendrunner.STP
Expand All @@ -41,6 +42,7 @@ import love.forte.simbot.suspendrunner.STP
@STP
public interface OneBotGroupMessageEvent : OneBotMessageEvent, ChatGroupEvent {
override val sourceEvent: RawGroupMessageEvent
override val messageContent: OneBotMessageContent

/**
* 事件发生所在群
Expand Down Expand Up @@ -85,6 +87,7 @@ public interface OneBotGroupMessageEvent : OneBotMessageEvent, ChatGroupEvent {
*/
@STP
public interface OneBotNormalGroupMessageEvent : OneBotGroupMessageEvent, ChatGroupMessageEvent {
override val messageContent: OneBotMessageContent
override suspend fun content(): OneBotGroup
override suspend fun author(): OneBotMember
}
Expand All @@ -95,6 +98,7 @@ public interface OneBotNormalGroupMessageEvent : OneBotGroupMessageEvent, ChatGr
*/
@STP
public interface OneBotAnonymousGroupMessageEvent : OneBotGroupMessageEvent, ChatGroupMessageEvent {
override val messageContent: OneBotMessageContent
override suspend fun content(): OneBotGroup
override suspend fun author(): OneBotMember
}
Expand All @@ -105,5 +109,6 @@ public interface OneBotAnonymousGroupMessageEvent : OneBotGroupMessageEvent, Cha
*/
@STP
public interface OneBotNoticeGroupMessageEvent : OneBotGroupMessageEvent {
override val messageContent: OneBotMessageContent
override suspend fun content(): OneBotGroup
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import love.forte.simbot.component.onebot.v11.core.actor.OneBotFriend
import love.forte.simbot.component.onebot.v11.core.actor.OneBotGroup
import love.forte.simbot.component.onebot.v11.core.actor.OneBotMember
import love.forte.simbot.component.onebot.v11.event.message.RawPrivateMessageEvent
import love.forte.simbot.component.onebot.v11.message.OneBotMessageContent
import love.forte.simbot.event.ContactMessageEvent
import love.forte.simbot.event.MemberMessageEvent
import love.forte.simbot.suspendrunner.STP
Expand All @@ -37,6 +38,7 @@ import love.forte.simbot.suspendrunner.STP
*/
public interface OneBotPrivateMessageEvent : OneBotMessageEvent {
override val sourceEvent: RawPrivateMessageEvent
override val messageContent: OneBotMessageContent

/**
* private 消息类型
Expand All @@ -50,6 +52,7 @@ public interface OneBotPrivateMessageEvent : OneBotMessageEvent {
*/
public val subType: String
get() = sourceEvent.subType

}

/**
Expand All @@ -58,6 +61,7 @@ public interface OneBotPrivateMessageEvent : OneBotMessageEvent {
*/
@STP
public interface OneBotFriendMessageEvent : OneBotPrivateMessageEvent, ContactMessageEvent {
override val messageContent: OneBotMessageContent
override suspend fun content(): OneBotFriend
}

Expand All @@ -67,6 +71,7 @@ public interface OneBotFriendMessageEvent : OneBotPrivateMessageEvent, ContactMe
*/
@STP
public interface OneBotGroupPrivateMessageEvent : OneBotPrivateMessageEvent, MemberMessageEvent {
override val messageContent: OneBotMessageContent

/**
* 发起会话的成员
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,13 @@ public data class RawGroupMessageEvent @SourceEventConstructor constructor(
@SerialName("user_id")
override val userId: LongID,
override val nickname: String,
override val sex: String = DEFAULT_SEX,
override val age: Int = DEFAULT_AGE,
public val card: String = "",
public val area: String? = null,
public val level: Int? = null,
public val role: String = "member",
public val title: String? = null,
override val sex: String = DEFAULT_SEX,
override val age: Int = DEFAULT_AGE,
) : RawMessageEvent.Sender


Expand Down

0 comments on commit d6adcff

Please sign in to comment.