Don't relogin bot when bot logging in

This commit is contained in:
Karlatemp 2020-12-24 00:14:17 +08:00
parent 7446453e8e
commit dffade2a92
No known key found for this signature in database
GPG Key ID: 21FBDDF664FF06F8

View File

@ -72,6 +72,8 @@ internal abstract class AbstractBot<N : BotNetworkHandler> constructor(
@Suppress("PropertyName") @Suppress("PropertyName")
internal lateinit var _network: N internal lateinit var _network: N
internal var _isConnecting: Boolean = false
override val isOnline: Boolean get() = _network.areYouOk() override val isOnline: Boolean get() = _network.areYouOk()
val otherClientsLock = Mutex() // lock sync val otherClientsLock = Mutex() // lock sync
@ -99,6 +101,10 @@ internal abstract class AbstractBot<N : BotNetworkHandler> constructor(
// bot 还未登录就被 close // bot 还未登录就被 close
return@subscribeAlways return@subscribeAlways
} }
if (_isConnecting) {
// bot 还在登入
return@subscribeAlways
}
/* /*
if (network.areYouOk() && event !is BotOfflineEvent.Force && event !is BotOfflineEvent.MsfOffline) { if (network.areYouOk() && event !is BotOfflineEvent.Force && event !is BotOfflineEvent.MsfOffline) {
// network 运行正常 // network 运行正常
@ -197,6 +203,7 @@ internal abstract class AbstractBot<N : BotNetworkHandler> constructor(
while (true) { while (true) {
_network = createNetworkHandler(this.coroutineContext) _network = createNetworkHandler(this.coroutineContext)
try { try {
_isConnecting = true
@OptIn(ThisApiMustBeUsedInWithConnectionLockBlock::class) @OptIn(ThisApiMustBeUsedInWithConnectionLockBlock::class)
relogin(null) relogin(null)
return return
@ -212,6 +219,8 @@ internal abstract class AbstractBot<N : BotNetworkHandler> constructor(
} catch (e: Exception) { } catch (e: Exception) {
network.logger.error(e) network.logger.error(e)
_network.closeAndJoin(e) _network.closeAndJoin(e)
} finally {
_isConnecting = false
} }
logger.warning { "Login failed. Retrying in 3s..." } logger.warning { "Login failed. Retrying in 3s..." }
delay(3000) delay(3000)