Fix reconnect. Fix #894

This commit is contained in:
Him188 2021-01-23 23:29:09 +08:00
parent 2e5b223b6a
commit 65eff19f32

View File

@ -96,16 +96,14 @@ internal abstract class AbstractBot<N : BotNetworkHandler> constructor(
priority = MONITOR,
concurrency = ConcurrencyKind.LOCKED
) { event ->
if (!event.bot.isActive) {
// bot closed
return@subscribeAlways
}
if (!::_network.isInitialized) {
// bot 还未登录就被 close
return@subscribeAlways
}
if (_isConnecting) {
// bot 还在登入
if (
!event.bot.isActive // bot closed
|| !::_network.isInitialized // bot 还未登录就被 close
|| _isConnecting // bot 还在登入
) {
// Close network to avoid endless reconnection while network is ok
// https://github.com/mamoe/mirai/issues/894
kotlin.runCatching { network.close(event.castOrNull<BotOfflineEvent.CauseAware>()?.cause) }
return@subscribeAlways
}
/*
@ -167,13 +165,20 @@ internal abstract class AbstractBot<N : BotNetworkHandler> constructor(
if (tryCount != 0) {
delay(configuration.reconnectPeriodMillis)
}
network.withConnectionLock {
/**
* [AbstractBot.relogin] only, no [BotNetworkHandler.init]
*/
@OptIn(ThisApiMustBeUsedInWithConnectionLockBlock::class)
relogin((event as? BotOfflineEvent.Dropped)?.cause)
}
// Close network to avoid endless reconnection while network is ok
// https://github.com/mamoe/mirai/issues/894
kotlin.runCatching { network.close(event.castOrNull<BotOfflineEvent.CauseAware>()?.cause) }
login()
// network.withConnectionLock {
// /**
// * [AbstractBot.relogin] only, no [BotNetworkHandler.init]
// */
// @OptIn(ThisApiMustBeUsedInWithConnectionLockBlock::class)
// relogin((event as? BotOfflineEvent.Dropped)?.cause)
// }
launch {
BotReloginEvent(bot, (event as? BotOfflineEvent.CauseAware)?.cause).broadcast()
}