Send logout on bot.close

This commit is contained in:
Him188 2021-02-06 17:43:12 +08:00
parent 3964d9976a
commit 13d4554456
3 changed files with 14 additions and 2 deletions

View File

@ -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) }

View File

@ -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)
}

View File

@ -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}" }