Commit 7172ef62 authored by Him188's avatar Him188

Fix NPE on initialization of Bot

parent 2ba31871
......@@ -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) }
......
......@@ -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
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment