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

Fix NPE on initialization of Bot

Him188 5 лет назад
Родитель
Сommit
7172ef628c

+ 2 - 12
mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/BotImpl.kt

@@ -32,18 +32,8 @@ import kotlin.time.measureTime
 
 internal abstract class BotImpl<N : BotNetworkHandler> constructor(
     context: Context,
-    val configuration: BotConfiguration
-) : Bot(), CoroutineScope {
-    final override val coroutineContext: CoroutineContext =
-        configuration.parentCoroutineContext + SupervisorJob(configuration.parentCoroutineContext[Job]) +
-                (configuration.parentCoroutineContext[CoroutineExceptionHandler]
-                    ?: CoroutineExceptionHandler { _, e ->
-                        logger.error(
-                            "An exception was thrown under a coroutine of Bot",
-                            e
-                        )
-                    })
-
+    configuration: BotConfiguration
+) : Bot(configuration), CoroutineScope {
     override val context: Context by context.unsafeWeakRef()
 
     final override val logger: MiraiLogger by lazy { configuration.botLoggerSupplier(this) }

+ 11 - 1
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/Bot.kt

@@ -46,7 +46,16 @@ suspend inline fun <B : Bot> B.alsoLogin(): B = also { login() }
  * @see BotFactory 构造 [Bot] 的工厂, [Bot] 唯一的构造方式.
  */
 @Suppress("INAPPLICABLE_JVM_NAME", "EXPOSED_SUPER_CLASS")
-abstract class Bot : CoroutineScope, LowLevelBotAPIAccessor, BotJavaFriendlyAPI, ContactOrBot {
+abstract class Bot(
+    val configuration: BotConfiguration
+) : CoroutineScope, LowLevelBotAPIAccessor, BotJavaFriendlyAPI, ContactOrBot {
+    final override val coroutineContext: CoroutineContext =
+        configuration.parentCoroutineContext + SupervisorJob(configuration.parentCoroutineContext[Job]) +
+                (configuration.parentCoroutineContext[CoroutineExceptionHandler]
+                    ?: CoroutineExceptionHandler { _, e ->
+                        logger.error("An exception was thrown under a coroutine of Bot", e)
+                    })
+
     companion object {
         @Suppress("ObjectPropertyName")
         internal val _instances: LockFreeLinkedList<WeakRef<Bot>> = LockFreeLinkedList()
@@ -106,6 +115,7 @@ abstract class Bot : CoroutineScope, LowLevelBotAPIAccessor, BotJavaFriendlyAPI,
      * 在 JVM 的默认实现为 `class ContextImpl : Context`
      * 在 Android 实现为 `android.content.Context`
      */
+    @MiraiExperimentalAPI
     abstract val context: Context
 
     /**