Skip to content

Commit 78258fc

Browse files
committed
refactor(docs): clarify docs
1 parent 213a418 commit 78258fc

8 files changed

Lines changed: 73 additions & 124 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ import kotlin.time.Duration
2222

2323
@SaltifyDsl
2424
public class SaltifyPluginContext internal constructor(
25-
name: String,
25+
pluginName: String,
2626
public val client: SaltifyApplication,
2727
@PublishedApi internal val pluginScope: CoroutineScope
2828
) : CoroutineScope by pluginScope {
2929
internal val onStartHooks = mutableListOf<suspend () -> Unit>()
3030
internal val onStopHooks = mutableListOf<() -> Unit>()
3131

32-
public val logger: Logger = KtorSimpleLogger("Saltify/plugin:$name")
32+
public val logger: Logger = KtorSimpleLogger("Saltify/plugin:$pluginName")
3333

3434
/**
3535
* 插件被加载,即 [SaltifyApplication.Companion.invoke] 后执行的逻辑。

saltify-docs/.vitepress/config.mts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ export default defineConfig({
1616
],
1717

1818
sidebar: [
19-
{ text: '快速上手', link: '/quick-tour' },
19+
{ text: '快速开始', link: '/quick-tour' },
2020
{
21-
text: '开发指南',
21+
text: 'saltify-core 文档',
2222
items: [
23-
{ text: '核心配置', link: '/guide/application' },
23+
{ text: '应用配置', link: '/guide/application' },
2424
{ text: '插件开发', link: '/guide/plugin' },
25-
{ text: '指令系统', link: '/guide/command' },
26-
{ text: '日志配置', link: '/guide/logging' }
25+
{ text: '指令开发', link: '/guide/command' },
26+
{ text: '日志实现', link: '/guide/logging' }
2727
]
2828
}
2929
],

saltify-docs/content/guide/application.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
# 核心配置
1+
# 应用配置
22

33
## 初始化 `SaltifyApplication`
44

5-
可以通过以下方式快速配置一个 Saltify 实例。这里仅展示了核心配置。
5+
可以通过以下方式创建 Saltify 实例。这里仅展示了核心配置。
66

77
```kotlin
88
val client = SaltifyApplication {
@@ -16,7 +16,7 @@ val client = SaltifyApplication {
1616
autoReconnect = true
1717
}
1818
}
19-
}.start()
19+
}.start() // 不要忘记这里的 start() 调用!
2020
```
2121

2222
事件服务连接不会自动建立,你需要手动调用 `connectEvent()` 才会真正开始监听事件。
@@ -38,7 +38,7 @@ suspend fun main() {
3838

3939
## 全局异常处理
4040

41-
Saltify 提供了用于监控未显式捕获的异常的 Flow,一般必须收集以实现自定义逻辑,否则**全部异常都会被无视**
41+
Saltify 提供了用于监控未显式捕获的异常的 Flow,如果选择使用日志功能,报错等信息会自动打印到控制台。你可以通过以下代码实现自定义逻辑:
4242

4343
```kotlin
4444
launch {
@@ -56,6 +56,8 @@ launch {
5656
}
5757
```
5858

59+
如上所见,所有 Saltify 创建的协程都有一个名为 `SaltifyComponent` 的上下文,它包含了组件的元信息。例如这里,Application 代表应用实例。
60+
5961
## 事件服务状态监听
6062

6163
Saltify 还提供了一个 Flow 用于监控事件服务连接状态的变更:
@@ -67,3 +69,5 @@ launch {
6769
}
6870
}
6971
```
72+
73+
需要注意的是,由连接状态断开引起的异常不会出现在 Application 异常流,而是会在这个流中出现。

saltify-docs/content/guide/command.md

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
# 指令系统
1+
# 指令开发
22

33
Saltify 提供了一套类型安全的指令构建 DSL,支持参数解析、子指令以及特定上下文的执行逻辑。
44

5-
## 基础指令与参数解析
5+
## 定义指令
66

7-
你可以为指令定义类型安全的参数,并通过 `.value` 快速获取。
7+
以下是一个简单的指令定义:
88

99
```kotlin
1010
client.command("order", prefix = "/") {
@@ -14,9 +14,8 @@ client.command("order", prefix = "/") {
1414
val note = greedyStringParameter("note")
1515

1616
onExecute {
17-
respond {
18-
text("Order #${id.value} created\nnote:${note.value}")
19-
}
17+
// some logic...
18+
respond("订单 #${id.value} 已创建\nNote:${note.value}")
2019
}
2120

2221
onFailure { error ->
@@ -32,7 +31,7 @@ onFailure 块是**解析**失败的处理,注意这里不是异常,而是指
3231
默认情况下,如果不定义这个块,会忽视所有解析错误,并对指令不做回复。
3332

3433
> [!tip]
35-
> `on` 一样,command 在插件初始化块中也是可以使用的,并且不用手动传 client。
34+
> `on` 一样,command 在插件初始化块中也是可以,并且推荐使用的。不需要传 `client`
3635
3736
## 上下文隔离
3837

@@ -41,11 +40,11 @@ onFailure 块是**解析**失败的处理,注意这里不是异常,而是指
4140
```kotlin
4241
client.command("info") {
4342
onGroupExecute {
44-
respond { text("这是群聊专用的信息!") }
43+
respond("这是群聊专用的信息!")
4544
}
4645

4746
onPrivateExecute {
48-
respond { text("这是私聊专用的信息!") }
47+
respond("这是私聊专用的信息!")
4948
}
5049

5150
onExecute {
@@ -57,22 +56,50 @@ client.command("info") {
5756
> [!tip]
5857
> `onGroupExecute``onPrivateExecute` 的优先级**高于** `onExecute`。如果群聊触发了指令且你定义了 `onGroupExecute`,那么 `onExecute` 中的兜底逻辑将**不会**被执行。
5958
59+
## 子指令
60+
61+
通过 `subCommand` 函数,可以方便地定义一个任意嵌套层级的指令树:
62+
63+
```kotlin
64+
command("math") {
65+
// /math add <num1> <num2>
66+
subCommand("add") {
67+
val a = parameter<Int>("a")
68+
val b = parameter<Int>("b")
69+
70+
onExecute {
71+
val result = a.value + b.value
72+
respond("$result")
73+
}
74+
}
75+
76+
// /math power <base>
77+
subCommand("power") {
78+
val base = parameter<Int>("base")
79+
onExecute {
80+
val value = base.value
81+
respond("$value 的平方是 ${value * value}")
82+
}
83+
}
84+
}
85+
```
86+
6087
## 上下文交互
6188

6289
在指令执行上下文中,你可以挂起当前协程,等待该用户的下一条回复。
6390

6491
```kotlin
6592
client.command("shutdown") {
6693
onExecute {
67-
respond { text("真的要关机吗?") }
94+
respond("真的要关机吗?")
6895

6996
// 等待该用户在同一上下文中发送的下一条消息(默认 30 秒超时)
7097
val replyEvent = awaitNextMessage(timeout = 30.seconds)
7198

7299
if (replyEvent == null) {
73-
respond { text("等待超时") }
100+
respond("等待超时")
74101
} else {
75-
respond { text("你回复了我,但这不重要,无论如何,我都不想关掉我自己!") }
102+
respond("你回复了我,但这不重要,无论如何,我都不想关掉我自己!")
76103
}
77104
}
78105
}

saltify-docs/content/guide/logging.md

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
1-
# 日志配置
1+
# 日志实现
22

33
Saltify 使用 KtorSimpleLogger 输出运行时日志。
44

55
## JVM 平台
66

7-
在 JVM 平台上,使用 SLF4J 作为日志框架,这里以 Logback 为例:
8-
9-
```kotlin
10-
dependencies {
11-
implementation("ch.qos.logback:logback-classic:$logbackVersion")
12-
}
13-
```
7+
这里依照快速开始中的日志配置,使用 Logback 框架。
148

159
### 配置日志
1610

saltify-docs/content/guide/plugin.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
## 定义插件
66

7-
你可以预先定义一个 `SaltifyPlugin`,或者在应用配置时直接使用 `plugin {}` 块。
7+
可以预先定义一个 `SaltifyPlugin`
88

99
```kotlin
1010
class MyPluginConfig(
@@ -14,14 +14,13 @@ class MyPluginConfig(
1414

1515
val myPlugin = SaltifyPlugin("my-awesome-plugin", ::MyPluginConfig) { config ->
1616
// 插件加载完成后的初始化逻辑
17-
// 这里的 `name` 就是 "my-awesome-plugin"
1817
onStart {
19-
println("[$name] 插件已启动!")
18+
println("插件已启动!")
2019
}
2120

2221
// 应用关闭时的清理逻辑
2322
onStop {
24-
println("[$name] 插件已停止!")
23+
println("插件已停止!")
2524
}
2625

2726
// 监听特定事件
@@ -38,16 +37,20 @@ val myPlugin = SaltifyPlugin("my-awesome-plugin", ::MyPluginConfig) { config ->
3837
}
3938
```
4039

40+
> [!tip]
41+
> 所有 `respond` 函数都有一个为纯文本情况提供的简写形式。如上例,完全可以写成 `event.respond(config.reply)`
42+
43+
4144
## 安装插件
4245

4346
```kotlin
4447
val client = SaltifyApplication {
45-
// 安装插件
48+
// 1. 安装外部插件
4649
install(myPlugin) {
4750
reply = "Hello!!"
4851
}
4952

50-
// 或者直接内联定义
53+
// 2. 或者直接内联定义
5154
plugin("another-plugin") {
5255
onStart { /* ... */ }
5356
}

saltify-docs/content/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ hero:
66
tagline: 跨平台、可扩展的 QQ Bot 框架 & Milky SDK
77
actions:
88
- theme: brand
9-
text: 快速上手
9+
text: 快速开始
1010
link: /quick-tour
1111

1212
features:

saltify-docs/content/quick-tour.md

Lines changed: 7 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# 快速上手
22

3-
欢迎来到 Saltify 的文档!本页面将会通过简单的示例介绍如何快速上手 Saltify。
3+
欢迎来到 Saltify 的文档!
44

55
## 开始之前
66

@@ -13,95 +13,16 @@
1313
```kotlin
1414
dependencies {
1515
implementation("org.ntqqrev:saltify-core:$saltifyVersion")
16+
17+
// 你可以选择为 Ktor client 使用不同的引擎而不是 cio,这里只是一个推荐。
1618
implementation("io.ktor:ktor-client-cio:$ktorVersion")
19+
20+
// 日志相关实现。同样,可以选择你所需要的平台日志实现。
21+
implementation("ch.qos.logback:logback-classic:$logbackVersion")
1722
}
1823
```
1924

20-
你可以选择为 Ktor client 使用不同的引擎而不是 cio,这里只是一个推荐。
21-
2225
> [!warning]
2326
> 本项目仍处于开发阶段,**强烈建议自行构建项目并依赖你的本地构建**,目前本文档将基于最新提交而不是最新发行版。待项目稳定会区分稳定文档与开发文档。
2427
25-
## 初始化 `SaltifyApplication`
26-
27-
```kotlin
28-
val client = SaltifyApplication {
29-
connection {
30-
baseUrl = "http://localhost:3000"
31-
accessToken = "your_token"
32-
33-
// 事件服务相关配置
34-
events {
35-
type = EventConnectionType.WebSocket
36-
autoReconnect = true
37-
}
38-
}
39-
}.start()
40-
```
41-
42-
更多内容请参见[核心配置](guide/application.md)。请在浏览完本页后**一定仔细阅读**这一页。
43-
44-
## 调用 API
45-
46-
```kotlin
47-
// 获取登录信息
48-
val loginInfo = client.getLoginInfo()
49-
50-
// 发送群消息
51-
client.sendGroupMessage(123456789L) {
52-
text("Hello from Saltify!")
53-
image("https://example.com/example.jpg")
54-
}
55-
```
56-
57-
更多 API 请参见项目源码与 [Milky 文档](https://milky.ntqqrev.org/)
58-
59-
## 定义插件
60-
61-
插件是一系列功能的集合,建议将功能逻辑封装在插件中。
62-
63-
```kotlin
64-
val myPlugin = SaltifyPlugin("my-plugin") {
65-
onStart {
66-
println("插件已启动")
67-
}
68-
69-
on<Event.GroupMemberIncrease> { event ->
70-
println("新成员加入: ${event.userId}")
71-
}
72-
}
73-
74-
val client = SaltifyApplication {
75-
// ...
76-
77-
// 在初始化时安装插件
78-
install(myPlugin)
79-
80-
// 也可以直接定义插件并安装
81-
plugin {
82-
// ...
83-
}
84-
}
85-
```
86-
87-
更多内容请参见[插件开发](guide/plugin.md)
88-
89-
## 定义指令
90-
91-
```kotlin
92-
client.command("say") {
93-
val content = greedyStringParameter("content", "要重复的内容")
94-
95-
onExecute {
96-
respond {
97-
text(content.value)
98-
}
99-
}
100-
}
101-
```
102-
103-
更多内容请参见[指令系统](guide/command.md)
104-
105-
## 日志输出
106-
107-
请参见[日志配置](guide/logging.md)
28+
在完成了以上配置后,点击页面下方的按钮,了解如何使用 Saltify 构建应用!

0 commit comments

Comments
 (0)