|
1 | | -# 在 Kotlin 中使用 |
| 1 | +--- |
| 2 | +title: 在 Kotlin 中使用 |
| 3 | +--- |
2 | 4 |
|
3 | 5 | ## 引入依赖 |
4 | 6 |
|
5 | | -[](https://central.sonatype.com/artifact/org.ntqqrev/acidify-core) |
| 7 | +[](https://central.sonatype.com/artifact/org.ntqqrev/acidify-core) |
6 | 8 |
|
7 | 9 | Acidify 的核心实现库 `acidify-core` 发布在 Maven Central 上。`acidify-core` 是一个 Kotlin Multiplatform 库,支持的平台如下: |
8 | 10 |
|
@@ -85,9 +87,11 @@ val bot = Bot( |
85 | 87 | - [`NopLogHandler`](/kdoc/acidify-core/org.ntqqrev.acidify.logging/-nop-log-handler/index.html):不输出任何日志。 |
86 | 88 | - [`SimpleLogHandler`](/kdoc/acidify-core/org.ntqqrev.acidify.logging/-simple-log-handler/index.html):将日志输出到控制台。 |
87 | 89 |
|
88 | | -> [!tip] |
89 | | -> |
90 | | -> 以下代码均假设你在使用 Kotlin/JVM。如果你使用 Kotlin Multiplatform,可以参考[Yogurt 的源代码](https://github.com/LagrangeDev/acidify/tree/main/yogurt),其中包含了许多在 Kotlin Multiplatform 使用 `acidify-core` 的最佳实践,下文的很多示例均能在 Yogurt 的源代码中找到类似的实现。此外,不要忘记参考 [`acidify-core` 的 KDoc](/kdoc/index.html),其中包含了完整的 API 文档。 |
| 90 | +{/* prettier-ignore */} |
| 91 | +<Callout> |
| 92 | +以下代码均假设你在使用 Kotlin/JVM。如果你使用 Kotlin Multiplatform,可以参考[Yogurt 的源代码](https://github.com/LagrangeDev/acidify/tree/main/yogurt),其中包含了许多在 Kotlin Multiplatform 使用 `acidify-core` 的最佳实践,下文的很多示例均能在 Yogurt 的源代码中找到类似的实现。此外,不要忘记参考 [`acidify-core` 的 KDoc](/kdoc/index.html),其中包含了完整的 API 文档。 |
| 93 | +</Callout> |
| 94 | +{/* prettier-ignore-end */} |
91 | 95 |
|
92 | 96 | ## 调用 Bot API |
93 | 97 |
|
@@ -192,23 +196,29 @@ bot.sendGroupMessage(group.uin) { |
192 | 196 | } |
193 | 197 | ``` |
194 | 198 |
|
195 | | -> [!note] |
196 | | -> |
197 | | -> `acidify-core` 中提供的消息发送功能较为底层,需要调用者自行提供许多元信息,例如被 @ 的成员名片、图片的宽高等。如果你希望使用更高层次的消息发送功能,请考虑使用 Yogurt 并配合应用端框架使用。 |
| 199 | +{/* prettier-ignore */} |
| 200 | +<Callout> |
| 201 | +`acidify-core` 中提供的消息发送功能较为底层,需要调用者自行提供许多元信息,例如被 @ 的成员名片、图片的宽高等。如果你希望使用更高层次的消息发送功能,请考虑使用 Yogurt 并配合应用端框架使用。 |
| 202 | +</Callout> |
| 203 | +{/* prettier-ignore-end */} |
198 | 204 |
|
199 | 205 | 发送消息的 API 会返回一个 [`BotOutgoingMessageResult`](/kdoc/acidify-core/org.ntqqrev.acidify.message/-bot-outgoing-message-result/index.html) 对象,包含了消息的序列号和实际发送的时间。 |
200 | 206 |
|
201 | 207 | ## 处理事件 |
202 | 208 |
|
203 | 209 | Bot 提供了一个 [`eventFlow`](/kdoc/acidify-core/org.ntqqrev.acidify/-bot/event-flow.html) 属性,它是一个 <code><a href="https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-shared-flow/">SharedFlow</a><<a href="/kdoc/acidify-core/org.ntqqrev.acidify.event/-acidify-event/index.html">AcidifyEvent</a>></code>,用于监听 Bot 产生的各种事件。你可以通过调用该 Flow 的 [`collect`](https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/collect.html) 方法来处理事件。 |
204 | 210 |
|
205 | | -> [!note] |
206 | | -> |
207 | | -> `collect` 是一个 `suspend fun`,调用它会导致当前协程挂起并持续运行。因此,建议你在一个单独的协程中调用它(使用 [`CoroutineScope.launch`](https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/launch.html) 等方式),以免阻塞主协程。以下的代码示例均假设你已经启动了一个独立的协程来处理事件。 |
| 211 | +{/* prettier-ignore */} |
| 212 | +<Callout> |
| 213 | +`collect` 是一个 `suspend fun`,调用它会导致当前协程挂起并持续运行。因此,建议你在一个单独的协程中调用它(使用 [`CoroutineScope.launch`](https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/launch.html) 等方式),以免阻塞主协程。以下的代码示例均假设你已经启动了一个独立的协程来处理事件。 |
| 214 | +</Callout> |
| 215 | +{/* prettier-ignore-end */} |
208 | 216 |
|
209 | | -> [!warning] |
210 | | -> |
211 | | -> 任何在 `collect` 块中抛出的异常都会导致该协程终止,从而停止对事件的处理。因此,建议在处理事件时使用 `try-catch` 块来捕获并处理可能的异常,以确保事件处理器能够持续运行。也可以使用 [`runCatching`](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/run-catching.html) 来包装事件处理逻辑。以下示例均未展示异常处理逻辑,请根据实际情况添加。 |
| 217 | +{/* prettier-ignore */} |
| 218 | +<Callout type="warning"> |
| 219 | +任何在 `collect` 块中抛出的异常都会导致该协程终止,从而停止对事件的处理。因此,建议在处理事件时使用 `try-catch` 块来捕获并处理可能的异常,以确保事件处理器能够持续运行。也可以使用 [`runCatching`](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/run-catching.html) 来包装事件处理逻辑。以下示例均未展示异常处理逻辑,请根据实际情况添加。 |
| 220 | +</Callout> |
| 221 | +{/* prettier-ignore-end */} |
212 | 222 |
|
213 | 223 | ### 接收消息 |
214 | 224 |
|
@@ -266,9 +276,7 @@ bot.eventFlow |
266 | 276 | } |
267 | 277 | ``` |
268 | 278 |
|
269 | | -> [!note] |
270 | | -> |
271 | | -> `eventFlow` 是一个热流 (Hot Flow),它会在 Bot 启动时立即开始产生事件。因此,你应该尽早开始监听该 Flow,以免错过重要事件。通常建议在调用 `login` 之前就开始监听 `eventFlow`。 |
| 279 | +`eventFlow` 是一个热流 (Hot Flow),它会在 Bot 启动时立即开始产生事件。因此,你应该尽早开始监听该 Flow,以免错过重要事件。通常建议在调用 `login` 之前就开始监听 `eventFlow`。 |
272 | 280 |
|
273 | 281 | ### 处理好友/群聊请求 |
274 | 282 |
|
|
0 commit comments