Commit 7172ef62 authored by Him188's avatar Him188

Fix NPE on initialization of Bot

parent 2ba31871
...@@ -32,18 +32,8 @@ import kotlin.time.measureTime ...@@ -32,18 +32,8 @@ import kotlin.time.measureTime
internal abstract class BotImpl<N : BotNetworkHandler> constructor( internal abstract class BotImpl<N : BotNetworkHandler> constructor(
context: Context, context: Context,
val configuration: BotConfiguration configuration: BotConfiguration
) : Bot(), CoroutineScope { ) : Bot(configuration), 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
)
})
override val context: Context by context.unsafeWeakRef() override val context: Context by context.unsafeWeakRef()
final override val logger: MiraiLogger by lazy { configuration.botLoggerSupplier(this) } final override val logger: MiraiLogger by lazy { configuration.botLoggerSupplier(this) }
......
...@@ -46,7 +46,16 @@ suspend inline fun <B : Bot> B.alsoLogin(): B = also { login() } ...@@ -46,7 +46,16 @@ suspend inline fun <B : Bot> B.alsoLogin(): B = also { login() }
* @see BotFactory 构造 [Bot] 的工厂, [Bot] 唯一的构造方式. * @see BotFactory 构造 [Bot] 的工厂, [Bot] 唯一的构造方式.
*/ */
@Suppress("INAPPLICABLE_JVM_NAME", "EXPOSED_SUPER_CLASS") @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 { companion object {
@Suppress("ObjectPropertyName") @Suppress("ObjectPropertyName")
internal val _instances: LockFreeLinkedList<WeakRef<Bot>> = LockFreeLinkedList() internal val _instances: LockFreeLinkedList<WeakRef<Bot>> = LockFreeLinkedList()
...@@ -106,6 +115,7 @@ abstract class Bot : CoroutineScope, LowLevelBotAPIAccessor, BotJavaFriendlyAPI, ...@@ -106,6 +115,7 @@ abstract class Bot : CoroutineScope, LowLevelBotAPIAccessor, BotJavaFriendlyAPI,
* 在 JVM 的默认实现为 `class ContextImpl : Context` * 在 JVM 的默认实现为 `class ContextImpl : Context`
* 在 Android 实现为 `android.content.Context` * 在 Android 实现为 `android.content.Context`
*/ */
@MiraiExperimentalAPI
abstract val context: Context 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