mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-24 06:50:08 +08:00
Send logout on bot.close
This commit is contained in:
parent
3964d9976a
commit
13d4554456
@ -322,6 +322,7 @@ internal abstract class AbstractBot<N : BotNetworkHandler> constructor(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected abstract suspend fun sendLogout()
|
||||||
|
|
||||||
override fun close(cause: Throwable?) {
|
override fun close(cause: Throwable?) {
|
||||||
if (!this.isActive) {
|
if (!this.isActive) {
|
||||||
@ -331,6 +332,10 @@ internal abstract class AbstractBot<N : BotNetworkHandler> constructor(
|
|||||||
|
|
||||||
if (::_network.isInitialized) {
|
if (::_network.isInitialized) {
|
||||||
if (this.network.areYouOk()) {
|
if (this.network.areYouOk()) {
|
||||||
|
|
||||||
|
// send log out
|
||||||
|
kotlin.runCatching { runBlocking { sendLogout() } } // just ignore errors
|
||||||
|
|
||||||
GlobalScope.launch {
|
GlobalScope.launch {
|
||||||
runCatching { BotOfflineEvent.Active(this@AbstractBot, cause).broadcast() }.exceptionOrNull()
|
runCatching { BotOfflineEvent.Active(this@AbstractBot, cause).broadcast() }.exceptionOrNull()
|
||||||
?.let { logger.error(it) }
|
?.let { logger.error(it) }
|
||||||
|
@ -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.OutgoingPacket
|
||||||
import net.mamoe.mirai.internal.network.protocol.packet.OutgoingPacketWithRespType
|
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.chat.*
|
||||||
|
import net.mamoe.mirai.internal.network.protocol.packet.login.StatSvc
|
||||||
import net.mamoe.mirai.internal.network.useNextServers
|
import net.mamoe.mirai.internal.network.useNextServers
|
||||||
import net.mamoe.mirai.message.data.*
|
import net.mamoe.mirai.message.data.*
|
||||||
import net.mamoe.mirai.network.LoginFailedException
|
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 {
|
override fun createNetworkHandler(coroutineContext: CoroutineContext): QQAndroidBotNetworkHandler {
|
||||||
return QQAndroidBotNetworkHandler(coroutineContext, this)
|
return QQAndroidBotNetworkHandler(coroutineContext, this)
|
||||||
}
|
}
|
||||||
|
@ -782,8 +782,8 @@ internal class QQAndroidBotNetworkHandler(coroutineContext: CoroutineContext, bo
|
|||||||
*/
|
*/
|
||||||
suspend fun OutgoingPacket.sendWithoutExpect() {
|
suspend fun OutgoingPacket.sendWithoutExpect() {
|
||||||
check(bot.isActive) { "bot is dead therefore can't send ${this.commandName}" }
|
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(this@QQAndroidBotNetworkHandler.isActive) { "network is dead therefore can't send ${this.commandName}" }
|
||||||
check(channel.isOpen) { "network channel is closed" }
|
check(channel.isOpen) { "network channel is closed therefore can't send ${this.commandName}" }
|
||||||
|
|
||||||
logger.verbose { "Send: ${this.commandName}" }
|
logger.verbose { "Send: ${this.commandName}" }
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user