Skip to content

Commit 70f6bdd

Browse files
committed
docs: decouple introductions from one file
1 parent 291516d commit 70f6bdd

File tree

11 files changed

+240
-144
lines changed

11 files changed

+240
-144
lines changed

saltify-core/src/commonMain/kotlin/org/ntqqrev/saltify/dsl/SaltifyCommandContext.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ public class SaltifyCommandContext internal constructor() {
2929
internal var failureBlock: (suspend SaltifyCommandExecutionContext.(CommandError) -> Unit)? = null
3030

3131
/**
32-
* 注册一个子命令
32+
* 注册一个子指令
3333
*/
3434
public fun subCommand(name: String, block: SaltifyCommandContext.() -> Unit) {
3535
subCommands.add(name to SaltifyCommandContext().apply(block))
3636
}
3737

3838
/**
39-
* 定义一个命令参数。请搭配 [SaltifyCommandExecutionContext.capture] 使用。
39+
* 定义一个指令参数。请搭配 [SaltifyCommandExecutionContext.capture] 使用。
4040
*/
4141
public fun <T : Any> parameter(
4242
type: KClass<T>,
@@ -57,7 +57,7 @@ public class SaltifyCommandContext internal constructor() {
5757
}
5858

5959
/**
60-
* 设置通用的命令执行逻辑
60+
* 设置通用的指令执行逻辑
6161
*/
6262
public fun onExecute(block: suspend SaltifyCommandExecutionContext.() -> Unit) {
6363
executionBlock = block
@@ -78,7 +78,7 @@ public class SaltifyCommandContext internal constructor() {
7878
}
7979

8080
/**
81-
* 当命令**解析**失败时执行的逻辑。
81+
* 当指令**解析**失败时执行的逻辑。
8282
*/
8383
public fun onFailure(block: suspend SaltifyCommandExecutionContext.(CommandError) -> Unit) {
8484
failureBlock = block
@@ -109,14 +109,14 @@ public class SaltifyCommandExecutionContext(
109109
get() = capture(this)
110110

111111
/**
112-
* 响应命令
112+
* 响应指令
113113
*/
114114
public suspend fun respond(
115115
block: MutableList<OutgoingSegment>.() -> Unit
116116
): SendMessageOutput = event.respond(client, block)
117117

118118
/**
119-
* 响应命令,并在指定延迟后撤回消息。
119+
* 响应指令,并在指定延迟后撤回消息。
120120
*/
121121
@ContextParametersMigrationNeeded
122122
public suspend inline fun respondWithRecall(
@@ -132,7 +132,7 @@ public class SaltifyCommandExecutionContext(
132132
}
133133

134134
/**
135-
* 获取由命令触发者发送的下一条消息事件。超时返回 null。
135+
* 获取由指令触发者发送的下一条消息事件。超时返回 null。
136136
*/
137137
public suspend fun awaitNextMessage(timeout: Duration = 30.seconds): Event.MessageReceive? {
138138
val messageFlow = client.eventFlow.filterIsInstance<Event.MessageReceive>()
@@ -155,7 +155,7 @@ public class SaltifyCommandExecutionContext(
155155
}
156156

157157
/**
158-
* 命令参数
158+
* 指令参数
159159
*/
160160
public class SaltifyCommandParamDef<T : Any>(
161161
public val type: KClass<T>,

saltify-core/src/commonMain/kotlin/org/ntqqrev/saltify/dsl/SaltifyPluginContext.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public class SaltifyPluginContext internal constructor(
4747
): Job = client.on(pluginScope, block)
4848

4949
/**
50-
* 注册一个命令
50+
* 注册一个指令
5151
*/
5252
public fun command(
5353
name: String,

saltify-core/src/commonMain/kotlin/org/ntqqrev/saltify/extension/ApplicationExtension.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public inline fun <reified T : Event> SaltifyApplication.on(
3535
private val spaceRegex = Regex("\\s+")
3636

3737
/**
38-
* 注册一个命令
38+
* 注册一个指令
3939
*/
4040
public fun SaltifyApplication.command(
4141
name: String,

saltify-core/src/commonMain/kotlin/org/ntqqrev/saltify/extension/CommandContextExtension.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import org.ntqqrev.saltify.dsl.SaltifyCommandExecutionContext
55
import org.ntqqrev.saltify.dsl.SaltifyCommandParamDef
66

77
/**
8-
* 定义一个命令参数。请搭配 [SaltifyCommandExecutionContext.capture] 使用。
8+
* 定义一个指令参数。请搭配 [SaltifyCommandExecutionContext.capture] 使用。
99
*/
1010
public inline fun <reified T : Any> SaltifyCommandContext.parameter(
1111
name: String,

saltify-core/src/commonMain/kotlin/org/ntqqrev/saltify/model/CommandError.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package org.ntqqrev.saltify.model
33
import org.ntqqrev.saltify.dsl.SaltifyCommandParamDef
44

55
/**
6-
* 命令解析错误
6+
* 指令解析错误
77
*/
88
public sealed class CommandError {
99
public abstract val message: String

saltify-docs/.vitepress/config.mts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,29 @@
11
import { defineConfig } from 'vitepress';
22

3-
// https://vitepress.dev/reference/site-config
43
export default defineConfig({
54
srcDir: 'content',
65

76
title: 'Saltify 文档',
87
description: 'Documentation of Saltify',
98
themeConfig: {
10-
// https://vitepress.dev/reference/default-theme-config
119
nav: [
1210
{ text: '主页', link: '/' },
1311
{ text: 'Milky', link: 'https://milky.ntqqrev.org/' },
1412
],
1513

1614
sidebar: [
17-
{ text: '快速上手', link: '/quick-tour' },
15+
{
16+
text: '快速上手',
17+
items: [ { text: 'API 概览', link: '/quick-tour' } ]
18+
},
19+
{
20+
text: '开发指南',
21+
items: [
22+
{ text: '核心配置与异常处理', link: '/guide/application' },
23+
{ text: '插件开发', link: '/guide/plugin' },
24+
{ text: '指令系统', link: '/guide/command' }
25+
]
26+
}
1827
],
1928

2029
socialLinks: [
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# 核心配置与异常处理
2+
3+
## 应用实例化与基础配置
4+
5+
可以通过以下方式快速配置一个 Saltify 实例。这里仅展示了核心配置。
6+
7+
```kotlin
8+
val client = SaltifyApplication {
9+
addressBase = "http://localhost:3000"
10+
accessToken = "your_token" // 选填:访问令牌(不用加 Bearer)
11+
12+
// 事件服务连接配置
13+
eventConnection {
14+
type = EventConnectionType.WebSocket // 可选 WebSocket 或 SSE
15+
autoReconnect = true
16+
}
17+
}
18+
```
19+
20+
事件服务连接不会自动建立,你需要手动调用 `connectEvent()` 才会真正开始监听事件。
21+
22+
```kotlin
23+
suspend fun main() {
24+
val client = SaltifyApplication { /* ... */ }
25+
26+
client.connectEvent()
27+
28+
// 其他逻辑
29+
30+
// 断开事件连接服务,可复用
31+
client.disconnectEvent()
32+
}
33+
```
34+
35+
另外,应用和插件都有生命周期管理。在程序退出时,务必调用 `close()` 方法释放资源,这会触发所有已加载插件的 `onStop` 钩子并关闭 HTTP 客户端。
36+
37+
## 全局异常处理与事件服务状态
38+
39+
Saltify 提供了用于监控连接状态和异常的 Flow,一般必须收集以实现自定义逻辑,否则**全部异常都会被无视**
40+
41+
```kotlin
42+
launch {
43+
client.eventConnectionStateFlow.collect { state ->
44+
println("连接状态变更: $state")
45+
}
46+
}
47+
48+
launch {
49+
client.exceptionFlow.collect { (context, exception) ->
50+
val component = context.saltifyComponent!!
51+
52+
when (component.type) {
53+
SaltifyComponentType.Application -> throw exception
54+
else -> println(
55+
"组件 ${component.name}(${component.type}) 抛出了一个异常: " +
56+
exception.stackTraceToString()
57+
)
58+
}
59+
}
60+
}
61+
```
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# 指令系统
2+
3+
Saltify 提供了一套类型安全的指令构建 DSL,支持参数解析、子指令以及特定上下文的执行逻辑。
4+
5+
## 基础指令与参数解析
6+
7+
你可以为指令定义类型安全的参数(支持自动类型转换),并通过 `.value` 快速获取。
8+
9+
```kotlin
10+
client.command("order", prefix = "/") {
11+
// 定义一个 Int 类型的参数
12+
val id = parameter<Int>("id")
13+
// 定义一个贪婪字符串参数,即将之后所有内容视为一个参数
14+
val note = greedyStringParameter("note")
15+
16+
onExecute {
17+
respond {
18+
text("Order #${id.value} created\nnote:${note.value}")
19+
}
20+
}
21+
22+
/**
23+
* **解析**失败的处理,注意这里不是异常,而是指令参数类型不匹配、过多参数、参数缺失等预期内错误
24+
* 这里的 error 是一个 `Typed error`,说的简单点,密封类。你可以用 when 判断到底是什么问题,并加以处理。
25+
* 默认情况下,如果不定义这个块,会忽视所有解析错误,并对指令不做回复。
26+
*/
27+
onFailure { error ->
28+
respond { /** ... */ }
29+
}
30+
}
31+
```
32+
33+
## 上下文隔离
34+
35+
指令可以针对不同的聊天场景执行不同的逻辑:
36+
37+
```kotlin
38+
client.command("info") {
39+
onGroupExecute {
40+
respond { text("这是群聊专用的信息!") }
41+
}
42+
43+
onPrivateExecute {
44+
respond { text("这是私聊专用的信息!") }
45+
}
46+
47+
onExecute {
48+
// 只有当没有定义上述两个块时,或者在其他未知上下文中,才会执行兜底逻辑
49+
}
50+
}
51+
```
52+
53+
> [!tip]
54+
> `onGroupExecute``onPrivateExecute` 的优先级**高于** `onExecute`。如果群聊触发了指令且你定义了 `onGroupExecute`,那么 `onExecute` 中的兜底逻辑将**不会**被执行。
55+
56+
## 上下文交互
57+
58+
在指令执行上下文中,你可以挂起当前协程,等待该用户的下一条回复(如实现分步对话)。
59+
60+
```kotlin
61+
client.command("shutdown") {
62+
onExecute {
63+
respond { text("真的要关机吗?") }
64+
65+
// 等待该用户在同一上下文中发送的下一条消息(默认 30 秒超时)
66+
val replyEvent = awaitNextMessage(timeout = 30.seconds)
67+
68+
if (replyEvent == null) {
69+
respond { text("等待超时") }
70+
} else {
71+
respond { text("你回复了我,但这不重要,无论如何,我都不想关掉我自己!") }
72+
}
73+
}
74+
}
75+
```
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# 插件开发
2+
3+
使用插件是 Saltify 组织代码逻辑的最佳实践。通过将相关联的事件监听和指令封装在一个插件中,可以大幅提升代码的可维护性。
4+
5+
## 定义插件
6+
7+
你可以预先定义一个 `SaltifyPlugin`,或者在应用配置时直接使用 `plugin {}` 块。
8+
9+
```kotlin
10+
class MyPluginConfig(
11+
var reply: String = "Hello!",
12+
var enableFeatureX: Boolean = true
13+
)
14+
15+
val myPlugin = SaltifyPlugin("my-awesome-plugin", ::MyPluginConfig) { config ->
16+
// 插件加载完成后的初始化逻辑
17+
// 这里的 `name` 就是 "my-awesome-plugin"
18+
onStart {
19+
println("[$name] 插件已启动!")
20+
}
21+
22+
// 应用关闭时的清理逻辑
23+
onStop {
24+
println("[$name] 插件已停止!")
25+
}
26+
27+
// 监听特定事件
28+
on<Event.GroupMemberIncrease> { event ->
29+
event.respond { text(config.reply) }
30+
}
31+
}
32+
```
33+
34+
## 安装插件
35+
36+
```kotlin
37+
val client = SaltifyApplication {
38+
// 安装插件
39+
install(myPlugin) {
40+
reply = "Hello!!"
41+
}
42+
43+
// 或者直接内联定义
44+
plugin("another-plugin") {
45+
onStart { /* ... */ }
46+
}
47+
}
48+
```
49+
50+
外部定义的插件的配置是可选的,在应用初始化块内定义的插件不可配置。

saltify-docs/content/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ features:
1515
- title: 跨平台支持
1616
details: 兼容 Kotlin JVM / Native / JS / WASM 平台,一套代码覆盖更多运行环境
1717
- title: 丰富功能
18-
details: 支持 Milky 协议所有 API / 事件,支持插件化开发、命令路由、事件监听等 DSL
18+
details: 支持 Milky 协议所有 API / 事件,支持插件化开发、指令路由、事件监听等 DSL
1919
---

0 commit comments

Comments
 (0)