diff --git a/mirai-core/src/commonMain/kotlin/AbstractBot.kt b/mirai-core/src/commonMain/kotlin/AbstractBot.kt index 59f504a43..6c594f9b6 100644 --- a/mirai-core/src/commonMain/kotlin/AbstractBot.kt +++ b/mirai-core/src/commonMain/kotlin/AbstractBot.kt @@ -86,7 +86,7 @@ internal abstract class AbstractBot constructor( */ abstract val components: ComponentStorage - final override val isOnline: Boolean get() = network.state == State.OK + final override val isOnline: Boolean get() = if (!networkInitialized) false else network.state == State.OK final override val eventChannel: EventChannel = GlobalEventChannel.filterIsInstance().filter { it.bot === this@AbstractBot } @@ -119,7 +119,12 @@ internal abstract class AbstractBot constructor( // network /////////////////////////////////////////////////////////////////////////// - val network: NetworkHandler by lazy { createNetworkHandler() } // the selector handles renewal of [NetworkHandler] + @Volatile + private var networkInitialized = false + val network: NetworkHandler by lazy { + networkInitialized = true + createNetworkHandler() + } // the selector handles renewal of [NetworkHandler] final override suspend fun login() { if (!isActive) error("Bot is already closed and cannot relogin. Please create a new Bot instance then do login.") diff --git a/mirai-core/src/commonTest/kotlin/network/impl/netty/NettyBotLifecycleTest.kt b/mirai-core/src/commonTest/kotlin/network/impl/netty/NettyBotLifecycleTest.kt index 1856a9d9d..341dc006e 100644 --- a/mirai-core/src/commonTest/kotlin/network/impl/netty/NettyBotLifecycleTest.kt +++ b/mirai-core/src/commonTest/kotlin/network/impl/netty/NettyBotLifecycleTest.kt @@ -125,4 +125,10 @@ internal class NettyBotLifecycleTest : AbstractNettyNHTest() { assertFalse { network.isActive } network.assertState(CLOSED) // we do not use selector in this test so it will be CLOSED. It will recover (reconnect) instead in real. } + + + @Test + fun `isOnline returns false if network not initialized`() = runBlockingUnit { + assertFalse { bot.isOnline } + } } \ No newline at end of file