Skip to content

Commit 213a418

Browse files
committed
feat(core): add an abbr of respond() to simplify pure text logics
no test due to there are no impl for the current milky version
1 parent 4bfc891 commit 213a418

6 files changed

Lines changed: 46 additions & 44 deletions

File tree

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,9 @@ import io.ktor.client.request.*
1010
import io.ktor.http.*
1111
import io.ktor.serialization.kotlinx.*
1212
import io.ktor.serialization.kotlinx.json.*
13-
import io.ktor.util.logging.KtorSimpleLogger
13+
import io.ktor.util.logging.*
1414
import kotlinx.coroutines.*
15-
import kotlinx.coroutines.flow.MutableSharedFlow
16-
import kotlinx.coroutines.flow.MutableStateFlow
17-
import kotlinx.coroutines.flow.SharedFlow
18-
import kotlinx.coroutines.flow.StateFlow
19-
import kotlinx.coroutines.flow.asSharedFlow
20-
import kotlinx.coroutines.flow.asStateFlow
15+
import kotlinx.coroutines.flow.*
2116
import kotlinx.serialization.json.decodeFromJsonElement
2217
import org.ntqqrev.milky.*
2318
import org.ntqqrev.saltify.annotation.WithApiExtension

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.ntqqrev.saltify.dsl
22

3-
import io.ktor.util.logging.KtorSimpleLogger
4-
import io.ktor.util.logging.Logger
3+
import io.ktor.util.logging.*
54
import kotlinx.coroutines.delay
65
import kotlinx.coroutines.flow.filterIsInstance
76
import kotlinx.coroutines.flow.first

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.ntqqrev.saltify.dsl
22

3-
import io.ktor.util.logging.KtorSimpleLogger
4-
import io.ktor.util.logging.Logger
3+
import io.ktor.util.logging.*
54
import kotlinx.coroutines.CoroutineScope
65
import kotlinx.coroutines.Job
76
import kotlinx.coroutines.delay
@@ -13,6 +12,7 @@ import org.ntqqrev.saltify.annotation.SaltifyDsl
1312
import org.ntqqrev.saltify.core.SaltifyApplication
1413
import org.ntqqrev.saltify.core.recallGroupMessage
1514
import org.ntqqrev.saltify.core.recallPrivateMessage
15+
import org.ntqqrev.saltify.core.text
1616
import org.ntqqrev.saltify.extension.command
1717
import org.ntqqrev.saltify.extension.on
1818
import org.ntqqrev.saltify.extension.regex
@@ -79,6 +79,14 @@ public class SaltifyPluginContext internal constructor(
7979
block: MutableList<OutgoingSegment>.() -> Unit
8080
): SendMessageOutput = respond(client, block)
8181

82+
/**
83+
* 响应事件。这是用于返回纯文本的简写。
84+
*/
85+
@ContextParametersMigrationNeeded
86+
public suspend fun Event.MessageReceive.respond(
87+
text: String
88+
): SendMessageOutput = respond { text(text) }
89+
8290
/**
8391
* 响应事件,并在指定延迟后撤回消息。
8492
*/

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package org.ntqqrev.saltify.extension
22

3+
import org.ntqqrev.saltify.core.text
34
import org.ntqqrev.saltify.dsl.SaltifyCommandContext
45
import org.ntqqrev.saltify.dsl.SaltifyCommandExecutionContext
56
import org.ntqqrev.saltify.dsl.SaltifyCommandParamDef
7+
import org.ntqqrev.saltify.model.milky.SendMessageOutput
68

79
/**
810
* 定义一个指令参数。请搭配 [SaltifyCommandExecutionContext.capture] 使用。
@@ -11,3 +13,10 @@ public inline fun <reified T : Any> SaltifyCommandContext.parameter(
1113
name: String,
1214
description: String = ""
1315
): SaltifyCommandParamDef<T> = parameter(T::class, name, description)
16+
17+
/**
18+
* 响应事件。这是用于返回纯文本的简写。
19+
*/
20+
public suspend inline fun SaltifyCommandExecutionContext.respond(
21+
text: String
22+
): SendMessageOutput = respond { text(text) }

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import org.ntqqrev.milky.OutgoingSegment
66
import org.ntqqrev.saltify.core.SaltifyApplication
77
import org.ntqqrev.saltify.core.sendGroupMessage
88
import org.ntqqrev.saltify.core.sendPrivateMessage
9+
import org.ntqqrev.saltify.core.text
910
import org.ntqqrev.saltify.dsl.SaltifyPluginContext
1011
import org.ntqqrev.saltify.model.milky.SendMessageOutput
1112

@@ -25,3 +26,11 @@ public suspend fun Event.MessageReceive.respond(
2526
SendMessageOutput(output.messageSeq, output.time)
2627
}
2728
}
29+
30+
/**
31+
* 响应事件。这是用于返回纯文本的简写。鉴于 Context Parameter 尚未完善,这里需要手动传 client。
32+
*/
33+
public suspend inline fun Event.MessageReceive.respond(
34+
client: SaltifyApplication,
35+
text: String
36+
): SendMessageOutput = respond(client) { text(text) }

saltify-core/src/jvmTest/kotlin/org/ntqqrev/saltify/PluginTest.kt

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import kotlinx.coroutines.runBlocking
55
import org.ntqqrev.milky.IncomingMessage
66
import org.ntqqrev.saltify.core.SaltifyApplication
77
import org.ntqqrev.saltify.core.getLoginInfo
8-
import org.ntqqrev.saltify.core.text
98
import org.ntqqrev.saltify.dsl.SaltifyPlugin
109
import org.ntqqrev.saltify.extension.parameter
1110
import org.ntqqrev.saltify.extension.plainText
11+
import org.ntqqrev.saltify.extension.respond
1212
import org.ntqqrev.saltify.model.EventConnectionType
1313
import kotlin.test.Test
1414

@@ -50,16 +50,14 @@ class PluginTest {
5050

5151
// regex test
5252
regex("""BV1\w{9}""") { event, matches ->
53-
event.respond {
54-
text(matches.joinToString { it.value })
55-
}
53+
event.respond(matches.joinToString { it.value })
5654
}
5755

5856
// config test
5957
command("hello") {
6058
onExecute {
6159
logger.info("config: ${config.response}")
62-
respond { text(config.response) }
60+
respond(config.response)
6361
}
6462
}
6563

@@ -68,15 +66,11 @@ class PluginTest {
6866
val content = greedyStringParameter("content", "words to repeat")
6967

7068
onExecute {
71-
respond {
72-
text(content.value)
73-
}
69+
respond(content.value)
7470
}
7571

7672
onFailure {
77-
respond {
78-
text("Command run failed: ${it.message}")
79-
}
73+
respond("Command run failed: ${it.message}")
8074
}
8175
}
8276

@@ -90,19 +84,15 @@ class PluginTest {
9084
// context propagation test
9185
command("shutdown") {
9286
onExecute {
93-
respond {
94-
text("Are you sure? (yes/no)")
95-
}
87+
respond("Are you sure? (yes/no)")
9688

9789
val event = awaitNextMessage()
9890

9991
if (event == null) {
100-
respond { text("Operation cancelled due to timeout") }
92+
respond("Operation cancelled due to timeout")
10193
} else {
10294
val content = event.segments.plainText
103-
respond {
104-
text("You just responded \"$content\". However, whatever you say I won't shutdown myself.")
105-
}
95+
respond("You just responded \"$content\". However, whatever you say I won't shutdown myself.")
10696
}
10797
}
10898
}
@@ -116,13 +106,11 @@ class PluginTest {
116106

117107
onExecute {
118108
val result = a.value + b.value
119-
respond { text("$result") }
109+
respond("$result")
120110
}
121111

122112
onFailure {
123-
respond {
124-
text("Command run failed: ${it.message}")
125-
}
113+
respond("Command run failed: ${it.message}")
126114
}
127115
}
128116

@@ -131,7 +119,7 @@ class PluginTest {
131119
val base = parameter<Int>("base")
132120
onExecute {
133121
val value = base.value
134-
respond { text("The power of $value is ${value * value}") }
122+
respond("The power of $value is ${value * value}")
135123
}
136124
}
137125
}
@@ -140,19 +128,15 @@ class PluginTest {
140128
command("whereami") {
141129
onGroupExecute {
142130
val data = event.data as IncomingMessage.Group
143-
respond {
144-
text("In group:${data.group.groupName} (${data.group.groupId})")
145-
}
131+
respond("In group:${data.group.groupName} (${data.group.groupId})")
146132
}
147133

148134
onPrivateExecute {
149-
respond {
150-
text("In private chat")
151-
}
135+
respond("In private chat")
152136
}
153137

154138
onExecute {
155-
respond { text("unknown") }
139+
respond("Unknown chat environment")
156140
}
157141
}
158142

@@ -163,9 +147,7 @@ class PluginTest {
163147
val note = greedyStringParameter("note")
164148

165149
onExecute {
166-
respond {
167-
text("Order #${id.value} created\nnote:${note.value}")
168-
}
150+
respond("Order #${id.value} created\nnote:${note.value}")
169151
}
170152
}
171153
}

0 commit comments

Comments
 (0)