Bladeren bron

Fix bot nick not initialized; Fix #1019

Karlatemp 5 jaren geleden
bovenliggende
commit
d09d810b6f

+ 13 - 0
mirai-core/src/commonMain/kotlin/AbstractBot.kt

@@ -28,6 +28,7 @@ import net.mamoe.mirai.event.EventPriority.MONITOR
 import net.mamoe.mirai.event.events.BotEvent
 import net.mamoe.mirai.event.events.BotOfflineEvent
 import net.mamoe.mirai.event.events.BotReloginEvent
+import net.mamoe.mirai.internal.message.contextualBugReportException
 import net.mamoe.mirai.internal.network.BotNetworkHandler
 import net.mamoe.mirai.internal.network.DefaultServerList
 import net.mamoe.mirai.internal.network.closeAndJoin
@@ -288,6 +289,18 @@ internal abstract class AbstractBot<N : BotNetworkHandler> constructor(
                 @OptIn(ThisApiMustBeUsedInWithConnectionLockBlock::class)
                 reinitializeNetworkHandler(null)
             }
+
+            // https://github.com/mamoe/mirai/issues/1019
+            kotlin.runCatching {
+                nick
+            }.onFailure {
+                throw contextualBugReportException(
+                    context = "Bot login",
+                    forDebug = it.toString(),
+                    e = it,
+                )
+            }
+
             logger.info { "Login successful" }
         }
     }

+ 6 - 0
mirai-core/src/commonMain/kotlin/network/ContactUpdater.kt

@@ -125,6 +125,12 @@ internal class ContactUpdaterImpl(
         val list = if (friendListCache?.isValid(registerResp) == true) {
             val list = friendListCache.list
             bot.network.logger.info { "Loaded ${list.size} friends from local cache." }
+
+            // For sync bot nick
+            FriendList.GetFriendGroupList(
+                bot.client, 0, 1, 0, 0
+            ).sendAndExpect<Packet>()
+
             list
         } else {
             refreshFriendList().also {

+ 5 - 1
mirai-core/src/commonMain/kotlin/network/protocol/packet/list/FriendList.kt

@@ -1,5 +1,5 @@
 /*
- * Copyright 2019-2020 Mamoe Technologies and contributors.
+ * Copyright 2019-2021 Mamoe Technologies and contributors.
  *
  *  此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
  *  Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
@@ -174,6 +174,10 @@ internal class FriendList {
             override fun toString(): String = "FriendList.GetFriendGroupList.Response"
         }
 
+        override suspend fun QQAndroidBot.handle(packet: Response) {
+            packet.selfInfo?.let { this.nick = it.nick }
+        }
+
         override suspend fun ByteReadPacket.decode(bot: QQAndroidBot): Response {
             val res = this.readUniPacket(GetFriendListResp.serializer())
             return Response(