Commit c1351f48 authored by Him188's avatar Him188

Add more extensions for MessageChain

parent b6f54dbb
...@@ -51,7 +51,7 @@ interface Message { ...@@ -51,7 +51,7 @@ interface Message {
* *
* @param M 指代持有它的消息类型 * @param M 指代持有它的消息类型
*/ */
interface Key<M> interface Key<M : Message>
infix fun eq(other: Message): Boolean = this == other infix fun eq(other: Message): Boolean = this == other
...@@ -233,6 +233,31 @@ inline fun <reified M : Message> MessageChain.first(): Message = this.first { M: ...@@ -233,6 +233,31 @@ inline fun <reified M : Message> MessageChain.first(): Message = this.first { M:
*/ */
inline fun <reified M : Message> MessageChain.any(): Boolean = this.firstOrNull { M::class.isInstance(it) } !== null inline fun <reified M : Message> MessageChain.any(): Boolean = this.firstOrNull { M::class.isInstance(it) } !== null
/**
* 获取第一个 [M] 类型的 [Message] 实例
*/
@Suppress("UNCHECKED_CAST")
fun <M : Message> MessageChain.firstOrNull(key: Message.Key<M>): M? = when (key) {
At -> first<At>()
PlainText -> first<PlainText>()
Image -> first<Image>()
Face -> first<Face>()
else -> null
} as M
/**
* 获取第一个 [M] 类型的 [Message] 实例
* @throws [NoSuchElementException] 如果找不到该类型的实例
*/
@Suppress("UNCHECKED_CAST")
fun <M : Message> MessageChain.first(key: Message.Key<M>): M = firstOrNull(key) ?: error("unknown key: $key")
/**
* 获取第一个 [M] 类型的 [Message] 实例
*/
@Suppress("UNCHECKED_CAST")
fun <M : Message> MessageChain.any(key: Message.Key<M>): Boolean = firstOrNull(key) != null
// endregion // endregion
/** /**
...@@ -267,13 +292,7 @@ interface MessageChain : Message, MutableList<Message> { ...@@ -267,13 +292,7 @@ interface MessageChain : Message, MutableList<Message> {
* @param key 由各个类型消息的伴生对象持有. 如 [PlainText.Key] * @param key 由各个类型消息的伴生对象持有. 如 [PlainText.Key]
*/ */
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
operator fun <M> get(key: Message.Key<M>): M = when (key) { operator fun <M : Message> get(key: Message.Key<M>): M = first(key)
At -> first<At>()
PlainText -> first<PlainText>()
Image -> first<Image>()
Face -> first<Face>()
else -> error("unknown key: $key")
} as M
} }
/** /**
...@@ -333,6 +352,12 @@ object NullMessageChain : MessageChain { ...@@ -333,6 +352,12 @@ object NullMessageChain : MessageChain {
private fun unsupported(): Nothing = throw UnsupportedOperationException() private fun unsupported(): Nothing = throw UnsupportedOperationException()
} }
// ==============================================================================
// ================================== INTERNAL ==================================
// ==============================================================================
/** /**
* [MessageChain] 实现 * [MessageChain] 实现
* 它是一个特殊的 [Message], 实现 [MutableList] 接口, 但将所有的接口调用都转到内部维护的另一个 [MutableList]. * 它是一个特殊的 [Message], 实现 [MutableList] 接口, 但将所有的接口调用都转到内部维护的另一个 [MutableList].
......
...@@ -39,7 +39,7 @@ suspend fun main() { ...@@ -39,7 +39,7 @@ suspend fun main() {
bot.subscribeMessages { bot.subscribeMessages {
"你好" reply "你好!" "你好" reply "你好!"
startsWith("随机色图", removePrefix = true) { startsWith("随机图片", removePrefix = true) {
withContext(Dispatchers.Default) { withContext(Dispatchers.Default) {
try { try {
repeat(it.toIntOrNull() ?: 1) { repeat(it.toIntOrNull() ?: 1) {
...@@ -56,7 +56,7 @@ suspend fun main() { ...@@ -56,7 +56,7 @@ suspend fun main() {
} }
bot.subscribeGroupMessages { bot.subscribeGroupMessages {
startsWith("色图", removePrefix = true) { startsWith("图片", removePrefix = true) {
HPictureSession(group, sender, it) HPictureSession(group, sender, it)
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment