From d09d810b6fa12083d04544e79f9cbfd5d3d50199 Mon Sep 17 00:00:00 2001 From: Karlatemp Date: Fri, 12 Feb 2021 17:22:14 +0800 Subject: [PATCH] Fix bot nick not initialized; Fix #1019 --- mirai-core/src/commonMain/kotlin/AbstractBot.kt | 13 +++++++++++++ .../src/commonMain/kotlin/network/ContactUpdater.kt | 6 ++++++ .../network/protocol/packet/list/FriendList.kt | 6 +++++- 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/mirai-core/src/commonMain/kotlin/AbstractBot.kt b/mirai-core/src/commonMain/kotlin/AbstractBot.kt index c818a7a45..5bc3f2f02 100644 --- a/mirai-core/src/commonMain/kotlin/AbstractBot.kt +++ b/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 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" } } } diff --git a/mirai-core/src/commonMain/kotlin/network/ContactUpdater.kt b/mirai-core/src/commonMain/kotlin/network/ContactUpdater.kt index e60e883e3..101cf8826 100644 --- a/mirai-core/src/commonMain/kotlin/network/ContactUpdater.kt +++ b/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() + list } else { refreshFriendList().also { diff --git a/mirai-core/src/commonMain/kotlin/network/protocol/packet/list/FriendList.kt b/mirai-core/src/commonMain/kotlin/network/protocol/packet/list/FriendList.kt index 321d3a31f..f1b9518ac 100644 --- a/mirai-core/src/commonMain/kotlin/network/protocol/packet/list/FriendList.kt +++ b/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(