Adapt for new Message.contentToString

This commit is contained in:
Him188 2020-04-05 23:51:34 +08:00
parent 7e6badb8db
commit eb0696be14

View File

@ -57,8 +57,8 @@ fun <R> CoroutineScope.subscribeMessages(
// subscribeAlways 即注册一个监听器. 这个监听器收到消息后就传递给 [messageListener] // subscribeAlways 即注册一个监听器. 这个监听器收到消息后就传递给 [messageListener]
// messageListener 即为 DSL 里 `contains(...) { }`, `startsWith(...) { }` 的代码块. // messageListener 即为 DSL 里 `contains(...) { }`, `startsWith(...) { }` 的代码块.
subscribeAlways(coroutineContext, concurrencyKind) { subscribeAlways(coroutineContext, concurrencyKind) {
// this.message.toString() 即为 messageListener 中 it 接收到的值 // this.message.contentToString() 即为 messageListener 中 it 接收到的值
val toString = this.message.toString() val toString = this.message.contentToString()
if (filter.invoke(this, toString)) if (filter.invoke(this, toString))
messageListener.invoke(this, toString) messageListener.invoke(this, toString)
} }
@ -83,7 +83,7 @@ fun <R> CoroutineScope.subscribeGroupMessages(
} }
return GroupMessageSubscribersBuilder(Unit) { filter, listener -> return GroupMessageSubscribersBuilder(Unit) { filter, listener ->
subscribeAlways(coroutineContext, concurrencyKind) { subscribeAlways(coroutineContext, concurrencyKind) {
val toString = this.message.toString() val toString = this.message.contentToString()
if (filter(this, toString)) if (filter(this, toString))
listener(this, toString) listener(this, toString)
} }
@ -108,7 +108,7 @@ fun <R> CoroutineScope.subscribeFriendMessages(
} }
return FriendMessageSubscribersBuilder(Unit) { filter, listener -> return FriendMessageSubscribersBuilder(Unit) { filter, listener ->
subscribeAlways(coroutineContext, concurrencyKind) { subscribeAlways(coroutineContext, concurrencyKind) {
val toString = this.message.toString() val toString = this.message.contentToString()
if (filter(this, toString)) if (filter(this, toString))
listener(this, toString) listener(this, toString)
} }
@ -131,7 +131,7 @@ fun <R> Bot.subscribeMessages(
} }
return MessagePacketSubscribersBuilder(Unit) { filter, listener -> return MessagePacketSubscribersBuilder(Unit) { filter, listener ->
this.subscribeAlways(coroutineContext, concurrencyKind) { this.subscribeAlways(coroutineContext, concurrencyKind) {
val toString = this.message.toString() val toString = this.message.contentToString()
if (filter(this, toString)) if (filter(this, toString))
listener(this, toString) listener(this, toString)
} }
@ -156,7 +156,7 @@ fun <R> Bot.subscribeGroupMessages(
} }
return GroupMessageSubscribersBuilder(Unit) { filter, listener -> return GroupMessageSubscribersBuilder(Unit) { filter, listener ->
this.subscribeAlways(coroutineContext, concurrencyKind) { this.subscribeAlways(coroutineContext, concurrencyKind) {
val toString = this.message.toString() val toString = this.message.contentToString()
if (filter(this, toString)) if (filter(this, toString))
listener(this, toString) listener(this, toString)
} }
@ -179,7 +179,7 @@ fun <R> Bot.subscribeFriendMessages(
} }
return FriendMessageSubscribersBuilder(Unit) { filter, listener -> return FriendMessageSubscribersBuilder(Unit) { filter, listener ->
this.subscribeAlways(coroutineContext, concurrencyKind) { this.subscribeAlways(coroutineContext, concurrencyKind) {
val toString = this.message.toString() val toString = this.message.contentToString()
if (filter(this, toString)) if (filter(this, toString))
listener(this, toString) listener(this, toString)
} }
@ -420,7 +420,7 @@ open class MessageSubscribersBuilder<M : ContactMessage, out Ret, R : RR, RR>(
): Ret { ): Ret {
val toCheck = if (trim) equals.trim() else equals val toCheck = if (trim) equals.trim() else equals
return content({ (if (trim) it.trim() else it).equals(toCheck, ignoreCase = ignoreCase) }, { return content({ (if (trim) it.trim() else it).equals(toCheck, ignoreCase = ignoreCase) }, {
onEvent(this, this.message.toString()) onEvent(this, this.message.contentToString())
}) })
} }
@ -444,11 +444,11 @@ open class MessageSubscribersBuilder<M : ContactMessage, out Ret, R : RR, RR>(
return if (trim) { return if (trim) {
val toCheck = sub.trim() val toCheck = sub.trim()
content({ it.contains(toCheck, ignoreCase = ignoreCase) }, { content({ it.contains(toCheck, ignoreCase = ignoreCase) }, {
onEvent(this, this.message.toString().trim()) onEvent(this, this.message.contentToString().trim())
}) })
} else { } else {
content({ it.contains(sub, ignoreCase = ignoreCase) }, { content({ it.contains(sub, ignoreCase = ignoreCase) }, {
onEvent(this, this.message.toString()) onEvent(this, this.message.contentToString())
}) })
} }
} }
@ -475,13 +475,13 @@ open class MessageSubscribersBuilder<M : ContactMessage, out Ret, R : RR, RR>(
content({ content({
list.any { toCheck -> it.contains(toCheck, ignoreCase = ignoreCase) } list.any { toCheck -> it.contains(toCheck, ignoreCase = ignoreCase) }
}, { }, {
onEvent(this, this.message.toString().trim()) onEvent(this, this.message.contentToString().trim())
}) })
} else { } else {
content({ content({
sub.any { toCheck -> it.contains(toCheck, ignoreCase = ignoreCase) } sub.any { toCheck -> it.contains(toCheck, ignoreCase = ignoreCase) }
}, { }, {
onEvent(this, this.message.toString()) onEvent(this, this.message.contentToString())
}) })
} }
} }
@ -508,13 +508,13 @@ open class MessageSubscribersBuilder<M : ContactMessage, out Ret, R : RR, RR>(
content({ content({
list.all { toCheck -> it.contains(toCheck, ignoreCase = ignoreCase) } list.all { toCheck -> it.contains(toCheck, ignoreCase = ignoreCase) }
}, { }, {
onEvent(this, this.message.toString().trim()) onEvent(this, this.message.contentToString().trim())
}) })
} else { } else {
content({ content({
sub.all { toCheck -> it.contains(toCheck, ignoreCase = ignoreCase) } sub.all { toCheck -> it.contains(toCheck, ignoreCase = ignoreCase) }
}, { }, {
onEvent(this, this.message.toString()) onEvent(this, this.message.contentToString())
}) })
} }
} }
@ -544,13 +544,13 @@ open class MessageSubscribersBuilder<M : ContactMessage, out Ret, R : RR, RR>(
return if (trim) { return if (trim) {
val toCheck = prefix.trim() val toCheck = prefix.trim()
content({ it.trimStart().startsWith(toCheck) }, { content({ it.trimStart().startsWith(toCheck) }, {
if (removePrefix) this.onEvent(this.message.toString().substringAfter(toCheck).trim()) if (removePrefix) this.onEvent(this.message.contentToString().substringAfter(toCheck).trim())
else onEvent(this, this.message.toString().trim()) else onEvent(this, this.message.contentToString().trim())
}) })
} else { } else {
content({ it.startsWith(prefix) }, { content({ it.startsWith(prefix) }, {
if (removePrefix) this.onEvent(this.message.toString().removePrefix(prefix)) if (removePrefix) this.onEvent(this.message.contentToString().removePrefix(prefix))
else onEvent(this, this.message.toString()) else onEvent(this, this.message.contentToString())
}) })
} }
} }
@ -575,13 +575,13 @@ open class MessageSubscribersBuilder<M : ContactMessage, out Ret, R : RR, RR>(
return if (trim) { return if (trim) {
val toCheck = suffix.trim() val toCheck = suffix.trim()
content({ it.trimEnd().endsWith(toCheck) }, { content({ it.trimEnd().endsWith(toCheck) }, {
if (removeSuffix) this.onEvent(this.message.toString().removeSuffix(toCheck).trim()) if (removeSuffix) this.onEvent(this.message.contentToString().removeSuffix(toCheck).trim())
else onEvent(this, this.message.toString().trim()) else onEvent(this, this.message.contentToString().trim())
}) })
} else { } else {
content({ it.endsWith(suffix) }, { content({ it.endsWith(suffix) }, {
if (removeSuffix) this.onEvent(this.message.toString().removeSuffix(suffix)) if (removeSuffix) this.onEvent(this.message.contentToString().removeSuffix(suffix))
else onEvent(this, this.message.toString()) else onEvent(this, this.message.contentToString())
}) })
} }
} }
@ -713,7 +713,7 @@ open class MessageSubscribersBuilder<M : ContactMessage, out Ret, R : RR, RR>(
): Ret = always { ): Ret = always {
onEvent.invoke( onEvent.invoke(
this, this,
mapper.invoke(this, message.toString()) ?: return@always this@MessageSubscribersBuilder.stub mapper.invoke(this, message.contentToString()) ?: return@always this@MessageSubscribersBuilder.stub
) )
} }
@ -898,7 +898,7 @@ open class MessageSubscribersBuilder<M : ContactMessage, out Ret, R : RR, RR>(
@PublishedApi @PublishedApi
@Suppress("REDUNDANT_INLINE_SUSPEND_FUNCTION_TYPE", "UNCHECKED_CAST") // false positive @Suppress("REDUNDANT_INLINE_SUSPEND_FUNCTION_TYPE", "UNCHECKED_CAST") // false positive
internal suspend inline fun executeAndReply(m: M, replier: suspend M.(String) -> Any?): RR { internal suspend inline fun executeAndReply(m: M, replier: suspend M.(String) -> Any?): RR {
when (val message = replier(m, m.message.toString())) { when (val message = replier(m, m.message.contentToString())) {
is Message -> m.reply(message) is Message -> m.reply(message)
is Unit -> { is Unit -> {
@ -911,7 +911,7 @@ open class MessageSubscribersBuilder<M : ContactMessage, out Ret, R : RR, RR>(
@PublishedApi @PublishedApi
@Suppress("REDUNDANT_INLINE_SUSPEND_FUNCTION_TYPE", "UNCHECKED_CAST") // false positive @Suppress("REDUNDANT_INLINE_SUSPEND_FUNCTION_TYPE", "UNCHECKED_CAST") // false positive
internal suspend inline fun executeAndQuoteReply(m: M, replier: suspend M.(String) -> Any?): RR { internal suspend inline fun executeAndQuoteReply(m: M, replier: suspend M.(String) -> Any?): RR {
when (val message = replier(m, m.message.toString())) { when (val message = replier(m, m.message.contentToString())) {
is Message -> m.quoteReply(message) is Message -> m.quoteReply(message)
is Unit -> { is Unit -> {