Skip to content

Commit

Permalink
Merge pull request #3 from simple-robot/dev/support-meta-event
Browse files Browse the repository at this point in the history
增加对Meta事件的组件事件类型实现;部分类型增加toString实现
  • Loading branch information
ForteScarlet authored Jun 7, 2024
2 parents a2da0b2 + bc4c97a commit 2b7f181
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
*/

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')"
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -101,6 +102,8 @@ internal class OneBotNormalGroupMessageEventImpl(
override suspend fun author(): OneBotMember {
return sourceEvent.sender.toMember(bot)
}

override fun toString(): String = eventToString("OneBotNormalGroupMessageEvent")
}


Expand All @@ -113,18 +116,24 @@ internal class OneBotAnonymousGroupMessageEventImpl(
override suspend fun author(): OneBotMember {
return sourceEvent.sender.toMember(bot)
}

override fun toString(): String = eventToString("OneBotAnonymousGroupMessageEvent")
}

internal class OneBotNoticeGroupMessageEventImpl(
override val sourceEventRaw: String?,
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")
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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")
}
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
*/

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")
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -41,4 +44,7 @@ internal class OneBotBotStartedEventImpl(

@OptIn(ExperimentalSimbotAPI::class)
override val time: Timestamp = Timestamp.now()

override fun toString(): String =
"OneBotBotStartedEvent(bot=$bot)"
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@

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


/**
* OneBot中的元事件类型。
* @author ForteScarlet
*/
public interface OneBotMetaEvent : OneBotEvent {
public interface OneBotMetaEvent : OneBotBotEvent {
override val sourceEvent: MetaEvent
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 2b7f181

Please sign in to comment.