Him188 5 лет назад
Родитель
Сommit
157bd3740c

+ 21 - 12
mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/QQAndroidBotNetworkHandler.kt

@@ -20,8 +20,10 @@ import kotlinx.io.core.ByteReadPacket
 import kotlinx.io.core.buildPacket
 import kotlinx.io.core.use
 import net.mamoe.mirai.event.*
+import net.mamoe.mirai.event.Listener.EventPriority.MONITOR
 import net.mamoe.mirai.event.events.BotOfflineEvent
 import net.mamoe.mirai.event.events.BotOnlineEvent
+import net.mamoe.mirai.event.events.BotReloginEvent
 import net.mamoe.mirai.message.MessageEvent
 import net.mamoe.mirai.network.BotNetworkHandler
 import net.mamoe.mirai.network.UnsupportedSMSLoginException
@@ -336,13 +338,9 @@ internal class QQAndroidBotNetworkHandler(coroutineContext: CoroutineContext, bo
             launch { reloadGroupList() }
         }
 
-        [email protected] {
+        [email protected](CoroutineName("Awaiting ConfigPushSvc.PushReq")) {
             logger.info { "Awaiting ConfigPushSvc.PushReq" }
-            val resp =
-                syncFromEventOrNull<ConfigPushSvc.PushReq.PushReqResponse, ConfigPushSvc.PushReq.PushReqResponse>(
-                    10_000) { it }
-
-            when (resp) {
+            when (val resp: ConfigPushSvc.PushReq.PushReqResponse? = nextEventOrNull(10_000)) {
                 null -> logger.info { "Missing ConfigPushSvc.PushReq." }
                 is ConfigPushSvc.PushReq.PushReqResponse.Success -> {
                     logger.info { "ConfigPushSvc.PushReq: Success" }
@@ -360,12 +358,7 @@ internal class QQAndroidBotNetworkHandler(coroutineContext: CoroutineContext, bo
             }
         }
 
-        logger.info { "Syncing friend message history..." }
-        withTimeoutOrNull(30000) {
-            launch { syncFromEvent<MessageSvcPbGetMsg.GetMsgSuccess, Unit> { Unit } }
-            MessageSvcPbGetMsg(bot.client, MsgSvc.SyncFlag.START, currentTimeSeconds).sendAndExpect<Packet>()
-        } ?: error("timeout syncing friend message history")
-        logger.info { "Syncing friend message history: Success" }
+        syncMessageSvc()
 
         bot.firstLoginSucceed = true
 
@@ -396,6 +389,22 @@ internal class QQAndroidBotNetworkHandler(coroutineContext: CoroutineContext, bo
         Unit // dont remove. can help type inference
     }
 
+    init {
+        val listener = bot.subscribeAlways<BotReloginEvent>(priority = MONITOR) {
+            [email protected] { syncMessageSvc() }
+        }
+        supervisor.invokeOnCompletion { listener.cancel() }
+    }
+
+    private suspend fun syncMessageSvc() {
+        logger.info { "Syncing friend message history..." }
+        withTimeoutOrNull(30000) {
+            launch(CoroutineName("Syncing friend message history")) { syncFromEvent<MessageSvcPbGetMsg.GetMsgSuccess, Unit> { Unit } }
+            MessageSvcPbGetMsg(bot.client, MsgSvc.SyncFlag.START, currentTimeSeconds).sendAndExpect<Packet>()
+        } ?: error("timeout syncing friend message history")
+        logger.info { "Syncing friend message history: Success" }
+    }
+
     private suspend fun doHeartBeat(): Exception? {
         val lastException: Exception?
         try {