You need to sign in or sign up before continuing.
Commit 84a9e51c authored by Him188's avatar Him188

Change all the params to be crossinline to reduce lambda creation

parent 33409f3a
...@@ -137,7 +137,7 @@ class MessageSubscribersBuilder<T : MessagePacket<*, *>>( ...@@ -137,7 +137,7 @@ class MessageSubscribersBuilder<T : MessagePacket<*, *>>(
* 无任何触发条件. * 无任何触发条件.
*/ */
@MessageDsl @MessageDsl
suspend inline fun always(noinline onEvent: @MessageDsl suspend T.(String) -> Unit) { suspend inline fun always(crossinline onEvent: @MessageDsl suspend T.(String) -> Unit) {
content({ true }, onEvent) content({ true }, onEvent)
} }
...@@ -151,7 +151,7 @@ class MessageSubscribersBuilder<T : MessagePacket<*, *>>( ...@@ -151,7 +151,7 @@ class MessageSubscribersBuilder<T : MessagePacket<*, *>>(
equals: String, equals: String,
trim: Boolean = true, trim: Boolean = true,
ignoreCase: Boolean = false, ignoreCase: Boolean = false,
noinline onEvent: @MessageDsl suspend T.(String) -> Unit crossinline onEvent: @MessageDsl suspend T.(String) -> Unit
) { ) {
val toCheck = if (trim) equals.trim() else equals val toCheck = if (trim) equals.trim() else equals
content({ toCheck.equals(if (trim) it.trim() else it, ignoreCase = ignoreCase) }, onEvent) content({ toCheck.equals(if (trim) it.trim() else it, ignoreCase = ignoreCase) }, onEvent)
...@@ -161,7 +161,7 @@ class MessageSubscribersBuilder<T : MessagePacket<*, *>>( ...@@ -161,7 +161,7 @@ class MessageSubscribersBuilder<T : MessagePacket<*, *>>(
* 如果消息内容包含 [sub], 就执行 [onEvent] * 如果消息内容包含 [sub], 就执行 [onEvent]
*/ */
@MessageDsl @MessageDsl
suspend inline fun contains(sub: String, noinline onEvent: @MessageDsl suspend T.(String) -> Unit) = content({ sub in it }, onEvent) suspend inline fun contains(sub: String, crossinline onEvent: @MessageDsl suspend T.(String) -> Unit) = content({ sub in it }, onEvent)
/** /**
* 如果消息的前缀是 [prefix], 就执行 [onEvent] * 如果消息的前缀是 [prefix], 就执行 [onEvent]
...@@ -170,7 +170,7 @@ class MessageSubscribersBuilder<T : MessagePacket<*, *>>( ...@@ -170,7 +170,7 @@ class MessageSubscribersBuilder<T : MessagePacket<*, *>>(
suspend inline fun startsWith( suspend inline fun startsWith(
prefix: String, prefix: String,
removePrefix: Boolean = true, removePrefix: Boolean = true,
noinline onEvent: @MessageDsl suspend T.(String) -> Unit crossinline onEvent: @MessageDsl suspend T.(String) -> Unit
) = ) =
content({ it.startsWith(prefix) }) { content({ it.startsWith(prefix) }) {
if (removePrefix) this.onEvent(this.message.stringValue.substringAfter(prefix)) if (removePrefix) this.onEvent(this.message.stringValue.substringAfter(prefix))
...@@ -181,68 +181,68 @@ class MessageSubscribersBuilder<T : MessagePacket<*, *>>( ...@@ -181,68 +181,68 @@ class MessageSubscribersBuilder<T : MessagePacket<*, *>>(
* 如果消息的结尾是 [suffix], 就执行 [onEvent] * 如果消息的结尾是 [suffix], 就执行 [onEvent]
*/ */
@MessageDsl @MessageDsl
suspend inline fun endsWith(suffix: String, noinline onEvent: @MessageDsl suspend T.(String) -> Unit) = suspend inline fun endsWith(suffix: String, crossinline onEvent: @MessageDsl suspend T.(String) -> Unit) =
content({ it.endsWith(suffix) }, onEvent) content({ it.endsWith(suffix) }, onEvent)
/** /**
* 如果是这个人发的消息, 就执行 [onEvent]. 消息可以是好友消息也可以是群消息 * 如果是这个人发的消息, 就执行 [onEvent]. 消息可以是好友消息也可以是群消息
*/ */
@MessageDsl @MessageDsl
suspend inline fun sentBy(qqId: UInt, noinline onEvent: @MessageDsl suspend T.(String) -> Unit) = suspend inline fun sentBy(qqId: UInt, crossinline onEvent: @MessageDsl suspend T.(String) -> Unit) =
content({ sender.id == qqId }, onEvent) content({ sender.id == qqId }, onEvent)
/** /**
* 如果是这个人发的消息, 就执行 [onEvent]. 消息可以是好友消息也可以是群消息 * 如果是这个人发的消息, 就执行 [onEvent]. 消息可以是好友消息也可以是群消息
*/ */
@MessageDsl @MessageDsl
suspend inline fun sentBy(qqId: Long, noinline onEvent: @MessageDsl suspend T.(String) -> Unit) = sentBy(qqId.toUInt(), onEvent) suspend inline fun sentBy(qqId: Long, crossinline onEvent: @MessageDsl suspend T.(String) -> Unit) = sentBy(qqId.toUInt(), onEvent)
/** /**
* 如果是管理员或群主发的消息, 就执行 [onEvent] * 如果是管理员或群主发的消息, 就执行 [onEvent]
*/ */
@MessageDsl @MessageDsl
suspend inline fun sentByOperator(noinline onEvent: @MessageDsl suspend T.(String) -> Unit) = suspend inline fun sentByOperator(crossinline onEvent: @MessageDsl suspend T.(String) -> Unit) =
content({ this is GroupMessage && sender.permission.isOperator() }, onEvent) content({ this is GroupMessage && sender.permission.isOperator() }, onEvent)
/** /**
* 如果是管理员发的消息, 就执行 [onEvent] * 如果是管理员发的消息, 就执行 [onEvent]
*/ */
@MessageDsl @MessageDsl
suspend inline fun sentByAdministrator(noinline onEvent: @MessageDsl suspend T.(String) -> Unit) = suspend inline fun sentByAdministrator(crossinline onEvent: @MessageDsl suspend T.(String) -> Unit) =
content({ this is GroupMessage && sender.permission.isAdministrator() }, onEvent) content({ this is GroupMessage && sender.permission.isAdministrator() }, onEvent)
/** /**
* 如果是群主发的消息, 就执行 [onEvent] * 如果是群主发的消息, 就执行 [onEvent]
*/ */
@MessageDsl @MessageDsl
suspend inline fun sentByOwner(noinline onEvent: @MessageDsl suspend T.(String) -> Unit) = suspend inline fun sentByOwner(crossinline onEvent: @MessageDsl suspend T.(String) -> Unit) =
content({ this is GroupMessage && sender.permission.isOwner() }, onEvent) content({ this is GroupMessage && sender.permission.isOwner() }, onEvent)
/** /**
* 如果是来自这个群的消息, 就执行 [onEvent] * 如果是来自这个群的消息, 就执行 [onEvent]
*/ */
@MessageDsl @MessageDsl
suspend inline fun sentFrom(id: UInt, noinline onEvent: @MessageDsl suspend T.(String) -> Unit) = suspend inline fun sentFrom(id: UInt, crossinline onEvent: @MessageDsl suspend T.(String) -> Unit) =
content({ if (this is GroupMessage) group.id == id else false }, onEvent) content({ if (this is GroupMessage) group.id == id else false }, onEvent)
/** /**
* 如果是来自这个群的消息, 就执行 [onEvent] * 如果是来自这个群的消息, 就执行 [onEvent]
*/ */
@MessageDsl @MessageDsl
suspend inline fun sentFrom(id: Long, noinline onEvent: @MessageDsl suspend T.(String) -> Unit) = sentFrom(id.toUInt(), onEvent) suspend inline fun sentFrom(id: Long, crossinline onEvent: @MessageDsl suspend T.(String) -> Unit) = sentFrom(id.toUInt(), onEvent)
/** /**
* 如果消息内容包含 [M] 类型的 [Message], 就执行 [onEvent] * 如果消息内容包含 [M] 类型的 [Message], 就执行 [onEvent]
*/ */
@MessageDsl @MessageDsl
suspend inline fun <reified M : Message> has(noinline onEvent: @MessageDsl suspend T.(String) -> Unit) = suspend inline fun <reified M : Message> has(crossinline onEvent: @MessageDsl suspend T.(String) -> Unit) =
subscriber { if (message.any<M>()) onEvent(this) } subscriber { if (message.any<M>()) onEvent(this) }
/** /**
* 如果 [filter] 返回 `true` 就执行 `onEvent` * 如果 [filter] 返回 `true` 就执行 `onEvent`
*/ */
@MessageDsl @MessageDsl
suspend inline fun content(noinline filter: T.(String) -> Boolean, noinline onEvent: @MessageDsl suspend T.(String) -> Unit) = suspend inline fun content(crossinline filter: T.(String) -> Boolean, crossinline onEvent: @MessageDsl suspend T.(String) -> Unit) =
subscriber { if (this.filter(message.stringValue)) onEvent(this) } subscriber { if (this.filter(message.stringValue)) onEvent(this) }
/** /**
...@@ -260,7 +260,7 @@ class MessageSubscribersBuilder<T : MessagePacket<*, *>>( ...@@ -260,7 +260,7 @@ class MessageSubscribersBuilder<T : MessagePacket<*, *>>(
* @param replier 若返回 [Message] 则直接发送; 若返回 [Unit] 则不回复; 其他情况则 [Any.toString] 后回复 * @param replier 若返回 [Message] 则直接发送; 若返回 [Unit] 则不回复; 其他情况则 [Any.toString] 后回复
*/ */
@MessageDsl @MessageDsl
suspend inline infix fun String.containsReply(noinline replier: AnyReplier<T>) = suspend inline infix fun String.containsReply(crossinline replier: AnyReplier<T>) =
content({ this@containsReply in it }) { content({ this@containsReply in it }) {
@Suppress("DSL_SCOPE_VIOLATION_WARNING") // false negative warning @Suppress("DSL_SCOPE_VIOLATION_WARNING") // false negative warning
executeAndReply(replier) executeAndReply(replier)
...@@ -280,7 +280,7 @@ class MessageSubscribersBuilder<T : MessagePacket<*, *>>( ...@@ -280,7 +280,7 @@ class MessageSubscribersBuilder<T : MessagePacket<*, *>>(
* @param replier 若返回 [Message] 则直接发送; 若返回 [Unit] 则不回复; 其他类型则 [Any.toString] 后回复 * @param replier 若返回 [Message] 则直接发送; 若返回 [Unit] 则不回复; 其他类型则 [Any.toString] 后回复
*/ */
@MessageDsl @MessageDsl
suspend inline infix fun String.startsWithReply(noinline replier: AnyReplier<T>) { suspend inline infix fun String.startsWithReply(crossinline replier: AnyReplier<T>) {
val toCheck = this.trimStart() val toCheck = this.trimStart()
content({ it.trimStart().startsWith(toCheck) }) { content({ it.trimStart().startsWith(toCheck) }) {
@Suppress("DSL_SCOPE_VIOLATION_WARNING") // false negative warning @Suppress("DSL_SCOPE_VIOLATION_WARNING") // false negative warning
...@@ -304,7 +304,7 @@ class MessageSubscribersBuilder<T : MessagePacket<*, *>>( ...@@ -304,7 +304,7 @@ class MessageSubscribersBuilder<T : MessagePacket<*, *>>(
* @param replier 若返回 [Message] 则直接发送; 若返回 [Unit] 则不回复; 其他情况则 [Any.toString] 后回复 * @param replier 若返回 [Message] 则直接发送; 若返回 [Unit] 则不回复; 其他情况则 [Any.toString] 后回复
*/ */
@MessageDsl @MessageDsl
suspend inline infix fun String.endswithReply(noinline replier: AnyReplier<T>) { suspend inline infix fun String.endswithReply(crossinline replier: AnyReplier<T>) {
val toCheck = this.trimEnd() val toCheck = this.trimEnd()
content({ it.endsWith(this@endswithReply) }) { content({ it.endsWith(this@endswithReply) }) {
@Suppress("DSL_SCOPE_VIOLATION_WARNING") // false negative warning @Suppress("DSL_SCOPE_VIOLATION_WARNING") // false negative warning
...@@ -320,14 +320,14 @@ class MessageSubscribersBuilder<T : MessagePacket<*, *>>( ...@@ -320,14 +320,14 @@ class MessageSubscribersBuilder<T : MessagePacket<*, *>>(
} }
@MessageDsl @MessageDsl
suspend inline infix fun String.reply(noinline replier: AnyReplier<T>) = case(this) { suspend inline infix fun String.reply(crossinline replier: AnyReplier<T>) = case(this) {
@Suppress("DSL_SCOPE_VIOLATION_WARNING") // false negative warning @Suppress("DSL_SCOPE_VIOLATION_WARNING") // false negative warning
executeAndReply(replier) executeAndReply(replier)
} }
@PublishedApi @PublishedApi
@Suppress("NOTHING_TO_INLINE") @Suppress("NOTHING_TO_INLINE")
internal suspend inline fun T.executeAndReply(noinline replier: AnyReplier<T>) { internal suspend inline fun T.executeAndReply(crossinline replier: AnyReplier<T>) {
when (val message = replier(this)) { when (val message = replier(this)) {
is Message -> this.reply(message) is Message -> this.reply(message)
is Unit -> { is Unit -> {
......
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