From 13d4554456107fe174a5c507aefb8ff03ac2dd4b Mon Sep 17 00:00:00 2001 From: Him188 Date: Sat, 6 Feb 2021 17:43:12 +0800 Subject: [PATCH] Send logout on bot.close --- mirai-core/src/commonMain/kotlin/AbstractBot.kt | 5 +++++ mirai-core/src/commonMain/kotlin/QQAndroidBot.kt | 7 +++++++ .../kotlin/network/QQAndroidBotNetworkHandler.kt | 4 ++-- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/mirai-core/src/commonMain/kotlin/AbstractBot.kt b/mirai-core/src/commonMain/kotlin/AbstractBot.kt index c0aedb974..d2ae64c2e 100644 --- a/mirai-core/src/commonMain/kotlin/AbstractBot.kt +++ b/mirai-core/src/commonMain/kotlin/AbstractBot.kt @@ -322,6 +322,7 @@ internal abstract class AbstractBot constructor( } } + protected abstract suspend fun sendLogout() override fun close(cause: Throwable?) { if (!this.isActive) { @@ -331,6 +332,10 @@ internal abstract class AbstractBot constructor( if (::_network.isInitialized) { if (this.network.areYouOk()) { + + // send log out + kotlin.runCatching { runBlocking { sendLogout() } } // just ignore errors + GlobalScope.launch { runCatching { BotOfflineEvent.Active(this@AbstractBot, cause).broadcast() }.exceptionOrNull() ?.let { logger.error(it) } diff --git a/mirai-core/src/commonMain/kotlin/QQAndroidBot.kt b/mirai-core/src/commonMain/kotlin/QQAndroidBot.kt index 3f6d1012a..d16f89425 100644 --- a/mirai-core/src/commonMain/kotlin/QQAndroidBot.kt +++ b/mirai-core/src/commonMain/kotlin/QQAndroidBot.kt @@ -29,6 +29,7 @@ import net.mamoe.mirai.internal.network.QQAndroidClient import net.mamoe.mirai.internal.network.protocol.packet.OutgoingPacket import net.mamoe.mirai.internal.network.protocol.packet.OutgoingPacketWithRespType import net.mamoe.mirai.internal.network.protocol.packet.chat.* +import net.mamoe.mirai.internal.network.protocol.packet.login.StatSvc import net.mamoe.mirai.internal.network.useNextServers import net.mamoe.mirai.message.data.* import net.mamoe.mirai.network.LoginFailedException @@ -104,6 +105,12 @@ internal class QQAndroidBot constructor( } } + override suspend fun sendLogout() { + network.run { + StatSvc.Register.offline(client). sendWithoutExpect() + } + } + override fun createNetworkHandler(coroutineContext: CoroutineContext): QQAndroidBotNetworkHandler { return QQAndroidBotNetworkHandler(coroutineContext, this) } diff --git a/mirai-core/src/commonMain/kotlin/network/QQAndroidBotNetworkHandler.kt b/mirai-core/src/commonMain/kotlin/network/QQAndroidBotNetworkHandler.kt index 1013932d5..60b66944f 100644 --- a/mirai-core/src/commonMain/kotlin/network/QQAndroidBotNetworkHandler.kt +++ b/mirai-core/src/commonMain/kotlin/network/QQAndroidBotNetworkHandler.kt @@ -782,8 +782,8 @@ internal class QQAndroidBotNetworkHandler(coroutineContext: CoroutineContext, bo */ suspend fun OutgoingPacket.sendWithoutExpect() { 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" } + check(this@QQAndroidBotNetworkHandler.isActive) { "network is dead therefore can't send ${this.commandName}" } + check(channel.isOpen) { "network channel is closed therefore can't send ${this.commandName}" } logger.verbose { "Send: ${this.commandName}" }