mirror of
https://github.com/mamoe/mirai.git
synced 2025-03-23 22:00:10 +08:00
Add context getter
This commit is contained in:
parent
aed2f3f7d9
commit
b59ae076ec
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user