Add regex matching for MessageSubscribersBuilder by @Hoshino_Tented

This commit is contained in:
Him188 2019-12-15 17:47:02 +08:00
parent 2bdc6274a7
commit 393e5d6c5d

View File

@ -245,6 +245,14 @@ class MessageSubscribersBuilder<T : MessagePacket<*, *>>(
suspend inline fun content(noinline filter: T.(String) -> Boolean, noinline onEvent: @MessageDsl suspend T.(String) -> Unit) =
subscriber { if (this.filter(message.stringValue)) onEvent(this) }
/**
* 如果消息内容可由正则表达式匹配, 就执行 `onEvent`
*/
@MessageDsl
suspend inline fun matching(regex: Regex, noinline onEvent: @MessageDsl suspend T.(String) -> Unit) {
content({ regex.matchEntire(it) != null }, onEvent)
}
/**
* 若消息内容包含 [this] 则回复 [reply]
*/
@ -266,6 +274,21 @@ class MessageSubscribersBuilder<T : MessagePacket<*, *>>(
executeAndReply(replier)
}
/**
* 若消息内容可由正则表达式匹配, 则执行 [replier] 并将其返回值回复给发信对象.
*
* [replier] `it` 将会是消息内容 string.
*
* @param replier 若返回 [Message] 则直接发送; 若返回 [Unit] 则不回复; 其他情况则 [Any.toString] 后回复
*/
@MessageDsl
suspend inline fun Regex.matchingReply(noinline replier: AnyReplier<T>) {
content({ this@matchingReply.matchEntire(it) != null }){
@Suppress("DSL_SCOPE_VIOLATION_WARNING") // false negative warning
executeAndReply(replier)
}
}
/**
* 不考虑空格, 若消息内容以 [this] 开始则执行 [replier] 并将其返回值回复给发信对象.
*