Просмотр исходного кода

Don't relogin bot when bot logging in

Karlatemp 5 лет назад
Родитель
Сommit
dffade2a92
1 измененных файлов с 9 добавлено и 0 удалено
  1. 9 0
      mirai-core/src/commonMain/kotlin/AbstractBot.kt

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

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