From 58e4c156e86219ecc3501d2df074c5b451bcd09e Mon Sep 17 00:00:00 2001 From: Him188 Date: Fri, 28 Feb 2020 21:38:23 +0800 Subject: [PATCH] Fix isByBot --- .../net.mamoe.mirai/event/events/BotEvents.kt | 10 +++--- .../net.mamoe.mirai/utils/contentToString.kt | 33 +++++++++++-------- 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/event/events/BotEvents.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/event/events/BotEvents.kt index b394e6543..8a972ca78 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/event/events/BotEvents.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/event/events/BotEvents.kt @@ -200,8 +200,9 @@ interface GroupSettingChangeEvent : GroupEvent, BotPassiveEvent, BroadcastCon data class GroupNameChangeEvent( override val origin: String, override val new: String, - override val group: Group -) : GroupSettingChangeEvent, Packet // 无法获取操作人 + override val group: Group, + val isByBot: Boolean // 无法获取操作人 +) : GroupSettingChangeEvent, Packet /** * 入群公告改变. 此事件广播前修改就已经完成. @@ -251,8 +252,9 @@ data class GroupAllowAnonymousChatEvent( data class GroupAllowConfessTalkEvent( override val origin: Boolean, override val new: Boolean, - override val group: Group -) : GroupSettingChangeEvent, Packet // 无法获取操作人 + override val group: Group, + val isByBot: Boolean // 无法获取操作人 +) : GroupSettingChangeEvent, Packet /** * 群 "允许群员邀请好友加群" 功能状态改变. 此事件广播前修改就已经完成. diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/contentToString.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/contentToString.kt index a5a2d155e..a8744f592 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/contentToString.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/contentToString.kt @@ -13,7 +13,7 @@ package net.mamoe.mirai.utils import net.mamoe.mirai.utils.io.toUHexString import kotlin.reflect.KClass -import kotlin.reflect.KProperty0 +import kotlin.reflect.KProperty1 private val indent: String = " ".repeat(4) @@ -125,22 +125,25 @@ fun Any?._miraiContentToString(prefix: String = ""): String = when (this) { } } +internal expect fun KProperty1<*, *>.getValueAgainstPermission(receiver: Any): Any? + @MiraiDebugAPI private fun Any.contentToStringReflectively(prefix: String, filter: ((name: String, value: Any?) -> Boolean)? = null): String { val newPrefix = "$prefix " return (this::class.simpleName ?: "") + "#" + this::class.hashCode() + " {\n" + - this.allMembersFromSuperClassesMatching { it.simpleName?.startsWith("net.mamoe.mirai") == true } + this.allMembersFromSuperClassesMatching { it.qualifiedName?.startsWith("net.mamoe.mirai") == true } .distinctBy { it.name } .filterNot { it.name.contains("$") || it.name == "Companion" || it.isConst || it.name == "serialVersionUID" } .mapNotNull { - val value = it.get() + val value = it.getValueAgainstPermission(this) if (filter != null) { - kotlin.runCatching { - if (!filter(it.name, value)) - return@mapNotNull it.name to value + if (!filter(it.name, value)) + return@mapNotNull it.name to value + else { + return@mapNotNull null } } - null + it.name to value } .joinToStringPrefixed( prefix = newPrefix @@ -152,16 +155,18 @@ private fun Any.contentToStringReflectively(prefix: String, filter: ((name: Stri } + "\n$prefix}" } -private fun Any.thisClassAndSuperclassSequence(): Sequence> { - return sequenceOf(this::class) + - this::class.supertypes.asSequence() - .mapNotNull { type -> type.classifier?.takeIf { it is KClass<*> } as? KClass } +private fun KClass.thisClassAndSuperclassSequence(): Sequence> { + return sequenceOf(this) + + this.supertypes.asSequence() + .mapNotNull { type -> type.classifier?.takeIf { it is KClass<*> }?.takeIf { it != Any::class } as? KClass }.flatMap { it.thisClassAndSuperclassSequence() } } -private fun Any.allMembersFromSuperClassesMatching(classFilter: (KClass) -> Boolean): Sequence> { - return this.thisClassAndSuperclassSequence() +@Suppress("UNCHECKED_CAST") +private fun Any.allMembersFromSuperClassesMatching(classFilter: (KClass) -> Boolean): Sequence> { + return this::class.thisClassAndSuperclassSequence() .filter { classFilter(it) } .map { it.members } .flatMap { it.asSequence() } - .mapNotNull { it as? KProperty0<*> } + .filterIsInstance>() + .mapNotNull { it as KProperty1 } } \ No newline at end of file