From a49e97d2e6332a92d2a8c4835476c79acb942179 Mon Sep 17 00:00:00 2001 From: Him188 Date: Sat, 16 Nov 2019 18:46:47 +0800 Subject: [PATCH] Add contracts --- .../net.mamoe.mirai/event/MessageSubscribers.kt | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/event/MessageSubscribers.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/event/MessageSubscribers.kt index 3d785ff75..4d138c6fe 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/event/MessageSubscribers.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/event/MessageSubscribers.kt @@ -136,6 +136,7 @@ class MessageSubscribersBuilder>( * @param trim `true` 则删除首尾空格后比较 * @param ignoreCase `true` 则不区分大小写 */ + @MessageDsl suspend fun case( equals: String, trim: Boolean = true, @@ -147,11 +148,13 @@ class MessageSubscribersBuilder>( /** * 如果消息内容包含 [sub], 就执行 [onEvent] */ + @MessageDsl suspend fun contains(sub: String, onEvent: @MessageDsl suspend T.(String) -> Unit) = content({ sub in it }, onEvent) /** * 如果消息的前缀是 [prefix], 就执行 [onEvent] */ + @MessageDsl suspend fun startsWith( prefix: String, removePrefix: Boolean = true, @@ -165,59 +168,72 @@ class MessageSubscribersBuilder>( /** * 如果消息的结尾是 [suffix], 就执行 [onEvent] */ + @MessageDsl suspend fun endsWith(suffix: String, onEvent: @MessageDsl suspend T.(String) -> Unit) = content({ it.endsWith(suffix) }, onEvent) /** * 如果是这个人发的消息, 就执行 [onEvent]. 消息可以是好友消息也可以是群消息 */ + @MessageDsl suspend fun sentBy(qqId: UInt, onEvent: @MessageDsl suspend T.(String) -> Unit) = content({ sender.id == qqId }, onEvent) /** * 如果是这个人发的消息, 就执行 [onEvent]. 消息可以是好友消息也可以是群消息 */ + @MessageDsl suspend fun sentBy(qqId: Long, onEvent: @MessageDsl suspend T.(String) -> Unit) = sentBy(qqId.toUInt(), onEvent) /** * 如果是来自这个群的消息, 就执行 [onEvent] */ + @MessageDsl suspend fun sentFrom(id: UInt, onEvent: @MessageDsl suspend T.(String) -> Unit) = content({ if (this is GroupMessage) group.id == id else false }, onEvent) /** * 如果是来自这个群的消息, 就执行 [onEvent] */ + @MessageDsl suspend fun sentFrom(id: Long, onEvent: @MessageDsl suspend T.(String) -> Unit) = sentFrom(id.toUInt(), onEvent) /** * 如果消息内容包含 [M] 类型的 [Message], 就执行 [onEvent] */ + @MessageDsl suspend inline fun has(noinline onEvent: @MessageDsl suspend T.(String) -> Unit) = subscriber { if (message.any()) onEvent(this) } /** * 如果 [filter] 返回 `true` 就执行 `onEvent` */ + @MessageDsl suspend fun content(filter: T.(String) -> Boolean, onEvent: @MessageDsl suspend T.(String) -> Unit) = subscriber { if (this.filter(message.stringValue)) onEvent(this) } + @MessageDsl suspend infix fun String.containsReply(replier: String) = content({ this@containsReply in it }) { this@content.reply(replier) } + @MessageDsl suspend infix fun String.containsReply(replier: StringReplier) = content({ this@containsReply in it }) { replier(this) } + @MessageDsl suspend infix fun String.startsWithReply(replier: StringReplier) = content({ it.startsWith(this@startsWithReply) }) { replier(this) } + @MessageDsl suspend infix fun String.endswithReply(replier: StringReplier) = content({ it.endsWith(this@endswithReply) }) { replier(this) } + @MessageDsl suspend infix fun String.reply(reply: String) = case(this) { this@case.reply(reply) } + @MessageDsl suspend infix fun String.reply(reply: StringReplier) = case(this) { this@case.reply(reply(this)) }