Skip to content

Commit e4fb1fc

Browse files
committed
refactor: simplify several logics; clarify docs
1 parent cf9af51 commit e4fb1fc

11 files changed

Lines changed: 38 additions & 40 deletions

File tree

saltify-core/src/commonMain/kotlin/org/ntqqrev/saltify/core/SaltifyApplicationSSE.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ public class SaltifyApplicationSSE(config: SaltifyApplicationConfig) : SaltifyAp
4141
incoming.collect { sseEvent ->
4242
if (sseEvent.event == "milky_event") {
4343
sseEvent.data?.let { data ->
44-
val event = milkyJsonModule.decodeFromString<Event>(data)
45-
events.emit(event)
44+
events.emit(milkyJsonModule.decodeFromString<Event>(data))
4645
}
4746
}
4847
}

saltify-core/src/commonMain/kotlin/org/ntqqrev/saltify/core/SaltifyApplicationWebSocket.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ public class SaltifyApplicationWebSocket(config: SaltifyApplicationConfig) : Sal
4141
)
4242

4343
while (isActive) {
44-
val event = receiveDeserialized<Event>()
45-
events.emit(event)
44+
events.emit(receiveDeserialized<Event>())
4645
}
4746
}
4847
}

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
@@ -85,8 +85,8 @@ public class SaltifyPluginContext internal constructor(
8585
*/
8686
@ContextParametersMigrationNeeded
8787
public suspend fun Event.MessageReceive.respond(
88-
text: String
89-
): SendMessageOutput = respond { text(text) }
88+
text: Any?
89+
): SendMessageOutput = respond { text(text.toString()) }
9090

9191
/**
9292
* 响应事件,并在指定延迟后撤回消息。

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

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ public inline fun SaltifyApplication.regex(
4646
val regex = Regex(regex)
4747

4848
return on<Event.MessageReceive>(scope) { event ->
49-
val text = event.segments.plainText
49+
val text = event.segments.filterIsInstance<IncomingSegment.Text>()
50+
.joinToString("") { it.text }
5051

5152
val matches = regex.findAll(text)
5253
if (matches.any()) block(event, matches)
@@ -71,12 +72,10 @@ public fun SaltifyApplication.command(
7172
.joinToString("") { it.text }
7273
.trim()
7374

74-
if (rawText != "$prefix$name" && !rawText.startsWith("$prefix$name ")) return@on
75-
76-
val content = rawText.removePrefix("$prefix$name").trim()
77-
val tokens = if (content.isEmpty()) emptyList() else content.split(spaceRegex)
75+
val tokens = if (rawText.isEmpty()) emptyList() else rawText.split(spaceRegex)
76+
if (tokens.isEmpty() || tokens[0] != "$prefix$name") return@on
7877

79-
executeCommand(rootDsl, tokens, this, event, name)
78+
executeCommand(rootDsl, tokens.drop(1), this, event, name)
8079
}
8180
}
8281

@@ -87,21 +86,28 @@ private suspend fun executeCommand(
8786
event: Event.MessageReceive,
8887
name: String
8988
) {
89+
val argumentMap = mutableMapOf<SaltifyCommandParamDef<*>, ParameterParseResult<Any>>()
90+
val execution = SaltifyCommandExecutionContext(client, event, name, argumentMap)
91+
92+
dsl.requirementBlock?.let { block ->
93+
val requirement = SaltifyCommandRequirementContext(execution).block()
94+
if (!requirement.satisfies()) return
95+
}
96+
9097
if (tokens.isNotEmpty()) {
9198
val subName = tokens[0]
9299
val subCommand = dsl.subCommands.find { it.first == subName }
93100
if (subCommand != null) {
94-
executeCommand(subCommand.second, tokens.drop(1), client, event, name)
101+
executeCommand(subCommand.second, tokens.drop(1), client, event, "$name $subName")
95102
return
96103
}
97104
}
98105

99-
val argumentMap = mutableMapOf<SaltifyCommandParamDef<*>, ParameterParseResult<Any>>()
100106
val errors = mutableListOf<CommandError>()
101107
val currentTokens = tokens.toMutableList()
102108

103109
for (param in dsl.parameters) {
104-
argumentMap[param] = when {
110+
val result = when {
105111
currentTokens.isEmpty() -> ParameterParseResult.MissingParam
106112
param.isGreedy -> {
107113
val value = currentTokens.joinToString(" ")
@@ -113,24 +119,18 @@ private suspend fun executeCommand(
113119
convertValue(rawValue, param.type)?.let { ParameterParseResult.Success(it) }
114120
?: ParameterParseResult.InvalidParam(rawValue)
115121
}
116-
}.also { res ->
117-
when (res) {
118-
is ParameterParseResult.MissingParam -> errors.add(CommandError.MissingParam(param))
119-
is ParameterParseResult.InvalidParam -> errors.add(CommandError.InvalidParam(param, res.rawValue))
120-
else -> {}
121-
}
122122
}
123-
}
124123

125-
if (currentTokens.isNotEmpty()) {
126-
errors.add(CommandError.TooManyArguments(currentTokens))
124+
argumentMap[param] = result
125+
if (result is ParameterParseResult.MissingParam) errors.add(CommandError.MissingParam(param))
126+
if (result is ParameterParseResult.InvalidParam) errors.add(CommandError.InvalidParam(param, result.rawValue))
127127
}
128128

129-
val execution = SaltifyCommandExecutionContext(client, event, name, argumentMap)
129+
if (currentTokens.isNotEmpty()) errors.add(CommandError.TooManyArguments(currentTokens))
130130

131-
dsl.requirementBlock?.let { block ->
132-
val requirement = SaltifyCommandRequirementContext(execution).block()
133-
if (!requirement.satisfies()) return
131+
if (errors.isNotEmpty()) {
132+
dsl.failureBlock?.invoke(execution, errors.first())
133+
return
134134
}
135135

136136
val startInstant = Clock.System.now()

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ public inline fun <reified T : Any> SaltifyCommandContext.parameter(
1818
* 响应事件。这是用于返回纯文本的简写。
1919
*/
2020
public suspend inline fun SaltifyCommandExecutionContext.respond(
21-
text: String
22-
): SendMessageOutput = respond { text(text) }
21+
text: Any?
22+
): SendMessageOutput = respond { text(text.toString()) }

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@ public suspend fun Event.MessageReceive.respond(
3232
*/
3333
public suspend inline fun Event.MessageReceive.respond(
3434
client: SaltifyApplication,
35-
text: String
36-
): SendMessageOutput = respond(client) { text(text) }
35+
text: Any?
36+
): SendMessageOutput = respond(client) { text(text.toString()) }

saltify-docs/content/docs-core/application.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,4 @@ launch {
7575
}
7676
```
7777

78-
需要注意的是,由连接状态断开引起的异常不会出现在 Application 异常流,而是会在这个流中出现
78+
需要注意的是,由连接状态断开引起的异常不会出现在 Application 异常流,而是出现在此流中

saltify-docs/content/docs-core/command.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ client.command("order", prefix = "/") {
2626

2727
onFailure 块是**解析**失败的处理,注意这里不是异常,而是指令参数类型不匹配、过多参数、参数缺失等预期内错误。
2828

29-
这里的 error 是一个 `Typed error`说的简单点,密封类。你可以用 when 判断到底是什么问题,并加以处理。
29+
这里的 error 是一个 `Typed error`简单来说就是密封类。你可以用 when 判断具体是什么问题,并加以处理。
3030

3131
默认情况下,如果不定义这个块,会忽视所有解析错误,并对指令不做回复。
3232

@@ -71,7 +71,7 @@ command("math") {
7171

7272
onExecute {
7373
val result = a.value + b.value
74-
respond("$result")
74+
respond(result)
7575
}
7676
}
7777

@@ -109,7 +109,7 @@ client.command("shutdown") {
109109

110110
## Requirements
111111

112-
Requirements 是一个用于指令鉴权的语法,妥当使用可以减少很多 onExecute 块内的判断条件
112+
Requirements 是用于指令鉴权的语法,合理使用可以减少很多 onExecute 块内的判断逻辑
113113

114114
```kotlin
115115
client.command("stop") {
@@ -125,7 +125,7 @@ client.command("stop") {
125125
}
126126
```
127127

128-
相信效果是什么不需要解释,自定义这么一个 requirement 函数也很简单:
128+
效果不言自明,自定义这样的 requirement 函数也很简单:
129129

130130
```kotlin
131131
fun SaltifyCommandRequirementContext.user(vararg targetId: Long) =

saltify-docs/content/docs-core/logging.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Saltify 使用 KtorSimpleLogger 输出运行时日志。
44

55
## JVM 平台
66

7-
这里依照快速开始中的日志配置,使用 Logback 框架。
7+
以快速开始中的日志配置为例,这里使用 Logback 框架。
88

99
### 配置日志
1010

saltify-docs/content/docs-core/permission.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ client.on<Event.MessageReceive> { event ->
2525
}
2626
```
2727

28-
需要注意的是,requirements 判定失败是静默返回的,所以上例1所示适用情况可能较少
28+
需要注意的是,requirements 判定失败时会静默返回,因此上述第一种用法的适用场景可能相对有限

0 commit comments

Comments
 (0)