Him188 5 лет назад
Родитель
Сommit
13d4554456

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

@@ -322,6 +322,7 @@ internal abstract class AbstractBot<N : BotNetworkHandler> constructor(
         }
     }
 
+    protected abstract suspend fun sendLogout()
 
     override fun close(cause: Throwable?) {
         if (!this.isActive) {
@@ -331,6 +332,10 @@ internal abstract class AbstractBot<N : BotNetworkHandler> 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) }

+ 7 - 0
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)
     }

+ 2 - 2
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([email protected]) { "network is dead therefore can't send any packet" }
-        check(channel.isOpen) { "network channel is closed" }
+        check([email protected]) { "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}" }