From 9b3bc88b3da8cce4821d6a6040a0d7f8c0a7dd43 Mon Sep 17 00:00:00 2001 From: Him188 Date: Mon, 17 Feb 2020 09:39:02 +0800 Subject: [PATCH] Add Bot.closeAndJoin --- .../src/commonMain/kotlin/net.mamoe.mirai/Bot.kt | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/Bot.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/Bot.kt index 542dffc49..a8dea8551 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/Bot.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/Bot.kt @@ -236,6 +236,8 @@ abstract class Bot : CoroutineScope { * 注: 不可重新登录. 必须重新实例化一个 [Bot]. * * @param cause 原因. 为 null 时视为正常关闭, 非 null 时视为异常关闭 + * + * @see closeAndJoin */ abstract fun close(cause: Throwable? = null) @@ -260,6 +262,18 @@ abstract class Bot : CoroutineScope { // endregion } +/** + * 关闭这个 [Bot], 停止一切相关活动. 所有引用都会被释放. + * + * 注: 不可重新登录. 必须重新实例化一个 [Bot]. + * + * @param cause 原因. 为 null 时视为正常关闭, 非 null 时视为异常关闭 + */ +suspend inline fun Bot.closeAndJoin(cause: Throwable? = null) { + close(cause) + coroutineContext[Job]?.join() +} + inline fun Bot.containsFriend(id: Long): Boolean = this.qqs.contains(id) inline fun Bot.containsGroup(id: Long): Boolean = this.groups.contains(id)