From c62fbc062aef2436c3bc46d311228f6161b6e36a Mon Sep 17 00:00:00 2001 From: Him188 Date: Tue, 25 Feb 2020 09:02:02 +0800 Subject: [PATCH] Add `ListenerFilter.quoteReply` --- .../event/subscribeMessages.kt | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/event/subscribeMessages.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/event/subscribeMessages.kt index dac261adb..26945a705 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/event/subscribeMessages.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/event/subscribeMessages.kt @@ -264,6 +264,21 @@ class MessageSubscribersBuilder>( executeAndReply(replier) } } + + infix fun quoteReply(toReply: String): Listener { + return content(filter) { quoteReply(toReply) } + } + + infix fun quoteReply(message: Message): Listener { + return content(filter) { quoteReply(message) } + } + + infix fun quoteReply(replier: (@MessageDsl suspend T.(String) -> Any?)): Listener { + return content(filter) { + @Suppress("DSL_SCOPE_VIOLATION_WARNING") + executeAndQuoteReply(replier) + } + } } /** @@ -713,6 +728,17 @@ class MessageSubscribersBuilder>( } } + @PublishedApi + @Suppress("REDUNDANT_INLINE_SUSPEND_FUNCTION_TYPE") // false positive + internal suspend inline fun T.executeAndQuoteReply(replier: suspend T.(String) -> Any?) { + when (val message = replier(this, this.message.toString())) { + is Message -> this.quoteReply(message) + is Unit -> { + + } + else -> this.quoteReply(message.toString()) + } + } /* 易产生迷惑感 fun replyCase(equals: String, trim: Boolean = true, replier: MessageReplier) = case(equals, trim) { reply(replier(this)) } fun replyContains(value: String, replier: MessageReplier) = content({ value in it }) { replier(this) }