From bc4c97a5788b775bf5c1f3a8748a94239709dbbd Mon Sep 17 00:00:00 2001 From: ForteScarlet Date: Fri, 7 Jun 2024 19:29:42 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=AF=B9Meta=E4=BA=8B?= =?UTF-8?q?=E4=BB=B6=E7=9A=84=E7=BB=84=E4=BB=B6=E4=BA=8B=E4=BB=B6=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E5=AE=9E=E7=8E=B0=EF=BC=9B=E9=83=A8=E5=88=86=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E5=A2=9E=E5=8A=A0toString=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../v11/core/bot/internal/OneBotBotImpl.kt | 31 ++++++++- .../core/event/internal/OneBotEventImpls.kt | 25 +++++++ .../message/OneBotGroupMessageEventImpl.kt | 13 +++- .../message/OneBotPrivateMessageEventImpl.kt | 13 +++- .../internal/meta/OneBotMetaEventImpl.kt | 69 +++++++++++++++++++ .../internal/stage/OneBotBotStageEventImpl.kt | 6 ++ .../v11/core/event/meta/OneBotMetaEvent.kt | 4 +- .../core/event/request/OneBotRequestEvent.kt | 8 ++- .../onebot/v11/event/meta/LifecycleEvent.kt | 2 +- 9 files changed, 161 insertions(+), 10 deletions(-) create mode 100644 simbot-component-onebot-v11/simbot-component-onebot-v11-core/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/core/event/internal/OneBotEventImpls.kt create mode 100644 simbot-component-onebot-v11/simbot-component-onebot-v11-core/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/core/event/internal/meta/OneBotMetaEventImpl.kt diff --git a/simbot-component-onebot-v11/simbot-component-onebot-v11-core/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/core/bot/internal/OneBotBotImpl.kt b/simbot-component-onebot-v11/simbot-component-onebot-v11-core/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/core/bot/internal/OneBotBotImpl.kt index 7f760d1..c11c68c 100644 --- a/simbot-component-onebot-v11/simbot-component-onebot-v11-core/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/core/bot/internal/OneBotBotImpl.kt +++ b/simbot-component-onebot-v11/simbot-component-onebot-v11-core/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/core/bot/internal/OneBotBotImpl.kt @@ -84,11 +84,17 @@ import love.forte.simbot.component.onebot.v11.core.event.internal.message.OneBot import love.forte.simbot.component.onebot.v11.core.event.internal.message.OneBotGroupPrivateMessageEventImpl import love.forte.simbot.component.onebot.v11.core.event.internal.message.OneBotNormalGroupMessageEventImpl import love.forte.simbot.component.onebot.v11.core.event.internal.message.OneBotNoticeGroupMessageEventImpl +import love.forte.simbot.component.onebot.v11.core.event.internal.meta.OneBotDefaultMetaEventImpl +import love.forte.simbot.component.onebot.v11.core.event.internal.meta.OneBotHeartbeatEventImpl +import love.forte.simbot.component.onebot.v11.core.event.internal.meta.OneBotLifecycleEventImpl import love.forte.simbot.component.onebot.v11.core.event.internal.stage.OneBotBotStartedEventImpl 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.GroupMessageEvent import love.forte.simbot.component.onebot.v11.event.message.PrivateMessageEvent +import love.forte.simbot.component.onebot.v11.event.meta.HeartbeatEvent +import love.forte.simbot.component.onebot.v11.event.meta.LifecycleEvent +import love.forte.simbot.component.onebot.v11.event.meta.MetaEvent import love.forte.simbot.component.onebot.v11.event.resolveEventSerializer import love.forte.simbot.component.onebot.v11.event.resolveEventSubTypeFieldName import love.forte.simbot.event.Event @@ -449,7 +455,7 @@ internal class OneBotBotImpl( resolveRawEvent(eventRaw) }.getOrElse { e -> val exMsg = "Failed to resolve raw event $eventRaw, " + - "session and bot will be closed exceptionally" + "session and bot will be closed exceptionally" val ex = IllegalStateException( exMsg, @@ -547,7 +553,28 @@ internal class OneBotBotImpl( ) } - // TODO meta events + //region Meta events + is MetaEvent -> when (event) { + is LifecycleEvent -> OneBotLifecycleEventImpl( + raw, + event, + bot, + ) + + is HeartbeatEvent -> OneBotHeartbeatEventImpl( + raw, + event, + bot, + ) + + else -> OneBotDefaultMetaEventImpl( + raw, + event, + bot + ) + } + //endregion + // TODO notice events is UnknownEvent -> OneBotUnknownEvent(raw, event) diff --git a/simbot-component-onebot-v11/simbot-component-onebot-v11-core/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/core/event/internal/OneBotEventImpls.kt b/simbot-component-onebot-v11/simbot-component-onebot-v11-core/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/core/event/internal/OneBotEventImpls.kt new file mode 100644 index 0000000..991e736 --- /dev/null +++ b/simbot-component-onebot-v11/simbot-component-onebot-v11-core/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/core/event/internal/OneBotEventImpls.kt @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2024. ForteScarlet. + * + * This file is part of simbot-component-onebot. + * + * simbot-component-onebot is free software: you can redistribute it and/or modify it under the terms + * of the GNU Lesser General Public License as published by the Free Software Foundation, + * either version 3 of the License, or (at your option) any later version. + * + * simbot-component-onebot is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License along with simbot-component-onebot. + * If not, see . + */ + +package love.forte.simbot.component.onebot.v11.core.event.internal + +import love.forte.simbot.component.onebot.v11.core.event.OneBotEvent + +internal fun OneBotEvent.eventToString( + eventName: String +): String = + "$eventName(source=$sourceEvent, raw='$sourceEventRaw')" diff --git a/simbot-component-onebot-v11/simbot-component-onebot-v11-core/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/core/event/internal/message/OneBotGroupMessageEventImpl.kt b/simbot-component-onebot-v11/simbot-component-onebot-v11-core/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/core/event/internal/message/OneBotGroupMessageEventImpl.kt index 01e1332..d13e525 100644 --- a/simbot-component-onebot-v11/simbot-component-onebot-v11-core/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/core/event/internal/message/OneBotGroupMessageEventImpl.kt +++ b/simbot-component-onebot-v11/simbot-component-onebot-v11-core/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/core/event/internal/message/OneBotGroupMessageEventImpl.kt @@ -25,6 +25,7 @@ import love.forte.simbot.component.onebot.v11.core.actor.internal.toGroup import love.forte.simbot.component.onebot.v11.core.actor.internal.toMember import love.forte.simbot.component.onebot.v11.core.bot.internal.OneBotBotImpl import love.forte.simbot.component.onebot.v11.core.bot.requestDataBy +import love.forte.simbot.component.onebot.v11.core.event.internal.eventToString import love.forte.simbot.component.onebot.v11.core.event.message.OneBotAnonymousGroupMessageEvent import love.forte.simbot.component.onebot.v11.core.event.message.OneBotGroupMessageEvent import love.forte.simbot.component.onebot.v11.core.event.message.OneBotNormalGroupMessageEvent @@ -101,6 +102,8 @@ internal class OneBotNormalGroupMessageEventImpl( override suspend fun author(): OneBotMember { return sourceEvent.sender.toMember(bot) } + + override fun toString(): String = eventToString("OneBotNormalGroupMessageEvent") } @@ -113,6 +116,8 @@ internal class OneBotAnonymousGroupMessageEventImpl( override suspend fun author(): OneBotMember { return sourceEvent.sender.toMember(bot) } + + override fun toString(): String = eventToString("OneBotAnonymousGroupMessageEvent") } internal class OneBotNoticeGroupMessageEventImpl( @@ -120,11 +125,15 @@ internal class OneBotNoticeGroupMessageEventImpl( override val sourceEvent: GroupMessageEvent, bot: OneBotBotImpl, ) : OneBotGroupMessageEventImpl(sourceEvent, bot), - OneBotNoticeGroupMessageEvent + OneBotNoticeGroupMessageEvent { + override fun toString(): String = eventToString("OneBotNoticeGroupMessageEvent") +} internal class OneBotDefaultGroupMessageEventImpl( override val sourceEventRaw: String?, override val sourceEvent: GroupMessageEvent, bot: OneBotBotImpl, -) : OneBotGroupMessageEventImpl(sourceEvent, bot) +) : OneBotGroupMessageEventImpl(sourceEvent, bot) { + override fun toString(): String = eventToString("OneBotDefaultGroupMessageEvent") +} diff --git a/simbot-component-onebot-v11/simbot-component-onebot-v11-core/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/core/event/internal/message/OneBotPrivateMessageEventImpl.kt b/simbot-component-onebot-v11/simbot-component-onebot-v11-core/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/core/event/internal/message/OneBotPrivateMessageEventImpl.kt index 4def09b..8f45441 100644 --- a/simbot-component-onebot-v11/simbot-component-onebot-v11-core/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/core/event/internal/message/OneBotPrivateMessageEventImpl.kt +++ b/simbot-component-onebot-v11/simbot-component-onebot-v11-core/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/core/event/internal/message/OneBotPrivateMessageEventImpl.kt @@ -26,6 +26,7 @@ import love.forte.simbot.component.onebot.v11.core.actor.internal.toFriend import love.forte.simbot.component.onebot.v11.core.actor.internal.toMember import love.forte.simbot.component.onebot.v11.core.bot.internal.OneBotBotImpl import love.forte.simbot.component.onebot.v11.core.bot.requestDataBy +import love.forte.simbot.component.onebot.v11.core.event.internal.eventToString import love.forte.simbot.component.onebot.v11.core.event.message.OneBotFriendMessageEvent import love.forte.simbot.component.onebot.v11.core.event.message.OneBotGroupPrivateMessageEvent import love.forte.simbot.component.onebot.v11.core.event.message.OneBotPrivateMessageEvent @@ -94,6 +95,9 @@ internal class OneBotFriendMessageEventImpl( override suspend fun content(): OneBotFriend { return sourceEvent.sender.toFriend(bot) } + + override fun toString(): String = + eventToString("OneBotFriendMessageEvent") } internal class OneBotGroupPrivateMessageEventImpl( @@ -111,10 +115,17 @@ internal class OneBotGroupPrivateMessageEventImpl( // 无法得知群号 throw UnsupportedOperationException("Don't know how to get the group number from PrivateMessageEvent") } + + override fun toString(): String = + eventToString("OneBotGroupPrivateMessageEvent") } internal class OneBotDefaultPrivateMessageEventImpl( override val sourceEventRaw: String?, sourceEvent: PrivateMessageEvent, bot: OneBotBotImpl, -) : OneBotPrivateMessageEventImpl(sourceEvent, bot) +) : OneBotPrivateMessageEventImpl(sourceEvent, bot) { + + override fun toString(): String = + eventToString("OneBotDefaultPrivateMessageEvent") +} diff --git a/simbot-component-onebot-v11/simbot-component-onebot-v11-core/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/core/event/internal/meta/OneBotMetaEventImpl.kt b/simbot-component-onebot-v11/simbot-component-onebot-v11-core/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/core/event/internal/meta/OneBotMetaEventImpl.kt new file mode 100644 index 0000000..c64cb9c --- /dev/null +++ b/simbot-component-onebot-v11/simbot-component-onebot-v11-core/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/core/event/internal/meta/OneBotMetaEventImpl.kt @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2024. ForteScarlet. + * + * This file is part of simbot-component-onebot. + * + * simbot-component-onebot is free software: you can redistribute it and/or modify it under the terms + * of the GNU Lesser General Public License as published by the Free Software Foundation, + * either version 3 of the License, or (at your option) any later version. + * + * simbot-component-onebot is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License along with simbot-component-onebot. + * If not, see . + */ + +package love.forte.simbot.component.onebot.v11.core.event.internal.meta + +import love.forte.simbot.common.id.ID +import love.forte.simbot.common.id.UUID +import love.forte.simbot.common.time.Timestamp +import love.forte.simbot.component.onebot.v11.core.bot.OneBotBot +import love.forte.simbot.component.onebot.v11.core.event.internal.eventToString +import love.forte.simbot.component.onebot.v11.core.event.meta.OneBotHeartbeatEvent +import love.forte.simbot.component.onebot.v11.core.event.meta.OneBotLifecycleEvent +import love.forte.simbot.component.onebot.v11.core.event.meta.OneBotMetaEvent +import love.forte.simbot.component.onebot.v11.core.utils.timestamp +import love.forte.simbot.component.onebot.v11.event.meta.HeartbeatEvent +import love.forte.simbot.component.onebot.v11.event.meta.LifecycleEvent +import love.forte.simbot.component.onebot.v11.event.meta.MetaEvent + +/** + * OneBot中的元事件类型。 + * @author ForteScarlet + */ +internal abstract class OneBotMetaEventImpl : OneBotMetaEvent { + override val id: ID = UUID.random() + + override val time: Timestamp + get() = sourceEvent.timestamp() +} + +internal class OneBotHeartbeatEventImpl( + override val sourceEventRaw: String?, + override val sourceEvent: HeartbeatEvent, + override val bot: OneBotBot, +) : OneBotHeartbeatEvent, OneBotMetaEventImpl() { + override fun toString(): String = + eventToString("OneBotHeartbeatEvent") +} + +internal class OneBotLifecycleEventImpl( + override val sourceEventRaw: String?, + override val sourceEvent: LifecycleEvent, + override val bot: OneBotBot, +) : OneBotLifecycleEvent, OneBotMetaEventImpl() { + override fun toString(): String = + eventToString("OneBotLifecycleEvent") +} + +internal class OneBotDefaultMetaEventImpl( + override val sourceEventRaw: String?, + override val sourceEvent: MetaEvent, + override val bot: OneBotBot, +) : OneBotMetaEventImpl() { + override fun toString(): String = + eventToString("OneBotDefaultMetaEvent") +} diff --git a/simbot-component-onebot-v11/simbot-component-onebot-v11-core/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/core/event/internal/stage/OneBotBotStageEventImpl.kt b/simbot-component-onebot-v11/simbot-component-onebot-v11-core/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/core/event/internal/stage/OneBotBotStageEventImpl.kt index b13e8ed..312a0b9 100644 --- a/simbot-component-onebot-v11/simbot-component-onebot-v11-core/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/core/event/internal/stage/OneBotBotStageEventImpl.kt +++ b/simbot-component-onebot-v11/simbot-component-onebot-v11-core/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/core/event/internal/stage/OneBotBotStageEventImpl.kt @@ -32,6 +32,9 @@ internal class OneBotBotRegisteredEventImpl( @OptIn(ExperimentalSimbotAPI::class) override val time: Timestamp = Timestamp.now() + + override fun toString(): String = + "OneBotBotRegisteredEvent(bot=$bot)" } internal class OneBotBotStartedEventImpl( @@ -41,4 +44,7 @@ internal class OneBotBotStartedEventImpl( @OptIn(ExperimentalSimbotAPI::class) override val time: Timestamp = Timestamp.now() + + override fun toString(): String = + "OneBotBotStartedEvent(bot=$bot)" } diff --git a/simbot-component-onebot-v11/simbot-component-onebot-v11-core/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/core/event/meta/OneBotMetaEvent.kt b/simbot-component-onebot-v11/simbot-component-onebot-v11-core/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/core/event/meta/OneBotMetaEvent.kt index 843b82e..621d846 100644 --- a/simbot-component-onebot-v11/simbot-component-onebot-v11-core/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/core/event/meta/OneBotMetaEvent.kt +++ b/simbot-component-onebot-v11/simbot-component-onebot-v11-core/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/core/event/meta/OneBotMetaEvent.kt @@ -17,7 +17,7 @@ package love.forte.simbot.component.onebot.v11.core.event.meta -import love.forte.simbot.component.onebot.v11.core.event.OneBotEvent +import love.forte.simbot.component.onebot.v11.core.event.OneBotBotEvent import love.forte.simbot.component.onebot.v11.event.meta.MetaEvent @@ -25,6 +25,6 @@ import love.forte.simbot.component.onebot.v11.event.meta.MetaEvent * OneBot中的元事件类型。 * @author ForteScarlet */ -public interface OneBotMetaEvent : OneBotEvent { +public interface OneBotMetaEvent : OneBotBotEvent { override val sourceEvent: MetaEvent } diff --git a/simbot-component-onebot-v11/simbot-component-onebot-v11-core/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/core/event/request/OneBotRequestEvent.kt b/simbot-component-onebot-v11/simbot-component-onebot-v11-core/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/core/event/request/OneBotRequestEvent.kt index 4e0cf1f..35fb67a 100644 --- a/simbot-component-onebot-v11/simbot-component-onebot-v11-core/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/core/event/request/OneBotRequestEvent.kt +++ b/simbot-component-onebot-v11/simbot-component-onebot-v11-core/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/core/event/request/OneBotRequestEvent.kt @@ -18,12 +18,16 @@ package love.forte.simbot.component.onebot.v11.core.event.request import love.forte.simbot.component.onebot.v11.core.event.OneBotBotEvent +import love.forte.simbot.event.RequestEvent +public typealias OBSourceRequestEvent = love.forte.simbot.component.onebot.v11.event.request.RequestEvent /** + * OneBot组件中的 [事件请求][love.forte.simbot.component.onebot.v11.event.request.RequestEvent] + * 的组件事件类型。 * * @author ForteScarlet */ -public interface OneBotRequestEvent : OneBotBotEvent { - // TODO +public interface OneBotRequestEvent : OneBotBotEvent, RequestEvent { + override val sourceEvent: OBSourceRequestEvent } diff --git a/simbot-component-onebot-v11/simbot-component-onebot-v11-event/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/event/meta/LifecycleEvent.kt b/simbot-component-onebot-v11/simbot-component-onebot-v11-event/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/event/meta/LifecycleEvent.kt index b6e0221..eb43767 100644 --- a/simbot-component-onebot-v11/simbot-component-onebot-v11-event/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/event/meta/LifecycleEvent.kt +++ b/simbot-component-onebot-v11/simbot-component-onebot-v11-event/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/event/meta/LifecycleEvent.kt @@ -32,7 +32,7 @@ import love.forte.simbot.component.onebot.v11.event.ExpectEventType * 只有正向 WebSocket 和反向 WebSocket 可以收到 `connect`。 */ @Serializable -@ExpectEventType(postType = MetaEvent.POST_TYPE, subType = "private") +@ExpectEventType(postType = MetaEvent.POST_TYPE, subType = "lifecycle") public data class LifecycleEvent( override val time: Long, @SerialName("self_id")