diff --git a/mirai-core/src/commonMain/kotlin/network/QQAndroidBotNetworkHandler.kt b/mirai-core/src/commonMain/kotlin/network/QQAndroidBotNetworkHandler.kt index fd808bc1e..b8383cddc 100644 --- a/mirai-core/src/commonMain/kotlin/network/QQAndroidBotNetworkHandler.kt +++ b/mirai-core/src/commonMain/kotlin/network/QQAndroidBotNetworkHandler.kt @@ -538,7 +538,7 @@ internal class QQAndroidBotNetworkHandler(coroutineContext: CoroutineContext, bo private suspend fun syncMessageSvc() { logger.info { "Syncing friend message history..." } withTimeoutOrNull(30000) { - launch(CoroutineName("Syncing friend message history")) { syncFromEvent { } } + launch(CoroutineName("Syncing friend message history")) { nextEvent { it.bot == this@QQAndroidBotNetworkHandler.bot } } MessageSvcPbGetMsg(bot.client, MsgSvc.SyncFlag.START, null).sendAndExpect() } ?: error("timeout syncing friend message history.") @@ -791,9 +791,15 @@ internal class QQAndroidBotNetworkHandler(coroutineContext: CoroutineContext, bo require(timeoutMillis > 100) { "timeoutMillis must > 100" } require(retry in 0..10) { "retry must in 0..10" } - check(bot.isActive) { "bot is dead therefore can't send ${this.commandName}" } - check(this@QQAndroidBotNetworkHandler.isActive) { "network is dead therefore can't send any packet" } - check(channel.isOpen) { "network channel is closed" } + if (!bot.isActive) { + throw CancellationException("bot is dead therefore can't send ${this.commandName}") + } + if (!this@QQAndroidBotNetworkHandler.isActive) { + throw CancellationException("network is dead therefore can't send any packet") + } + if (!channel.isOpen) { + throw CancellationException("network channel is closed") + } val data = this.delegate.withUse { readBytes() } diff --git a/mirai-core/src/commonMain/kotlin/network/protocol/packet/chat/receive/MessageSvc.PbGetMsg.kt b/mirai-core/src/commonMain/kotlin/network/protocol/packet/chat/receive/MessageSvc.PbGetMsg.kt index 8cef406df..0cf368651 100644 --- a/mirai-core/src/commonMain/kotlin/network/protocol/packet/chat/receive/MessageSvc.PbGetMsg.kt +++ b/mirai-core/src/commonMain/kotlin/network/protocol/packet/chat/receive/MessageSvc.PbGetMsg.kt @@ -91,7 +91,7 @@ internal object MessageSvcPbGetMsg : OutgoingPacketFactory, syncCookie: ByteArray?) : Response( + open class GetMsgSuccess(delegate: List, syncCookie: ByteArray?, val bot: QQAndroidBot) : Response( MsgSvc.SyncFlag.STOP, delegate, syncCookie ), Event, @@ -116,7 +116,9 @@ internal object MessageSvcPbGetMsg : OutgoingPacketFactory))" } - object EmptyResponse : GetMsgSuccess(emptyList(), null) + class EmptyResponse( + bot: QQAndroidBot + ) : GetMsgSuccess(emptyList(), null, bot) @OptIn(FlowPreview::class) override suspend fun ByteReadPacket.decode(bot: QQAndroidBot): Response { @@ -132,7 +134,7 @@ internal object MessageSvcPbGetMsg : OutgoingPacketFactory { @@ -155,7 +157,7 @@ internal object MessageSvcPbGetMsg : OutgoingPacketFactory = messages.toList() if (resp.syncFlag == MsgSvc.SyncFlag.STOP) { - return GetMsgSuccess(list, resp.syncCookie) + return GetMsgSuccess(list, resp.syncCookie, bot) } return Response(resp.syncFlag, list, resp.syncCookie) }