Commit b59ae076 authored by Him188's avatar Him188

Add context getter

parent aed2f3f7
......@@ -43,7 +43,7 @@ internal abstract class QQAndroidBotBase constructor(
context: Context,
account: BotAccount,
configuration: BotConfiguration
) : BotImpl<QQAndroidBotNetworkHandler>(account, configuration) {
) : BotImpl<QQAndroidBotNetworkHandler>(context, account, configuration) {
val client: QQAndroidClient =
QQAndroidClient(
context,
......
......@@ -44,7 +44,8 @@ abstract class Bot : CoroutineScope {
* 复制一份此时的 [Bot] 实例列表.
*/
@JvmStatic
val instances: List<WeakRef<Bot>> get() = BotImpl.instances.toList()
val instances: List<WeakRef<Bot>>
get() = BotImpl.instances.toList()
/**
* 遍历每一个 [Bot] 实例
......@@ -58,6 +59,14 @@ abstract class Bot : CoroutineScope {
fun instanceWhose(qq: Long): Bot = BotImpl.instanceWhose(qq = qq)
}
/**
* [Bot] 运行的 [Context].
*
* 在 JVM 的默认实现为 `class ContextImpl : Context`
* 在 Android 实现为 `android.content.Context`
*/
abstract val context: Context
/**
* 账号信息
*/
......
......@@ -32,6 +32,7 @@ import kotlin.coroutines.CoroutineContext
@UseExperimental(MiraiExperimentalAPI::class)
@MiraiInternalAPI
abstract class BotImpl<N : BotNetworkHandler> constructor(
context: Context,
account: BotAccount,
val configuration: BotConfiguration
) : Bot(), CoroutineScope {
......@@ -39,6 +40,7 @@ abstract class BotImpl<N : BotNetworkHandler> constructor(
override val coroutineContext: CoroutineContext =
configuration.parentCoroutineContext + botJob + (configuration.parentCoroutineContext[CoroutineExceptionHandler]
?: CoroutineExceptionHandler { _, e -> logger.error("An exception was thrown under a coroutine of Bot", e) })
override val context: Context by context.unsafeWeakRef()
@Suppress("CanBePrimaryConstructorProperty") // for logger
final override val account: BotAccount = account
......
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