From f48940a897a7cef59abf5f69471429afa6c1f101 Mon Sep 17 00:00:00 2001 From: Karlatemp Date: Sat, 18 Jun 2022 23:48:37 +0800 Subject: [PATCH] Prefer SystemCommandSender; fix SystemCommandSender error message rendering --- .../backend/mirai-console/src/command/CommandSender.kt | 4 ++++ .../backend/mirai-console/src/command/CompositeCommand.kt | 4 ++-- .../frontend/mirai-console-terminal/src/ConsoleThread.kt | 3 +++ 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/mirai-console/backend/mirai-console/src/command/CommandSender.kt b/mirai-console/backend/mirai-console/src/command/CommandSender.kt index b2cae5618..5ff3ec0c7 100644 --- a/mirai-console/backend/mirai-console/src/command/CommandSender.kt +++ b/mirai-console/backend/mirai-console/src/command/CommandSender.kt @@ -362,6 +362,10 @@ public interface PluginCustomCommandSender : CommandSender, SystemCommandSender * 控制台指令执行者. 代表由控制台执行指令 * * 控制台拥有一切指令的执行权限. + * + * 不建议在 [CompositeCommand] 中使用 [ConsoleCommandSender], + * 使用 [SystemCommandSender] 以允许其他插件执行 + * */ public object ConsoleCommandSender : AbstractCommandSender(), SystemCommandSender { public const val NAME: String = "ConsoleCommandSender" diff --git a/mirai-console/backend/mirai-console/src/command/CompositeCommand.kt b/mirai-console/backend/mirai-console/src/command/CompositeCommand.kt index 023e13843..79c1facae 100644 --- a/mirai-console/backend/mirai-console/src/command/CompositeCommand.kt +++ b/mirai-console/backend/mirai-console/src/command/CompositeCommand.kt @@ -57,8 +57,8 @@ import kotlin.annotation.AnnotationTarget.FUNCTION * } * * @SubCommand - * suspend fun ConsoleCommandSender.foo() { - * // 使用 ConsoleCommandSender 作为接收者,表示指令只能由控制台执行。 + * suspend fun SystemCommandSender.foo() { + * // 使用 SystemCommandSender 作为接收者,表示指令只能由系统(控制台或其他插件)执行。 * // 当用户尝试在聊天环境执行时将会收到错误提示。 * } * diff --git a/mirai-console/frontend/mirai-console-terminal/src/ConsoleThread.kt b/mirai-console/frontend/mirai-console-terminal/src/ConsoleThread.kt index 9ad0fe4ad..eb4305dfe 100644 --- a/mirai-console/frontend/mirai-console-terminal/src/ConsoleThread.kt +++ b/mirai-console/frontend/mirai-console-terminal/src/ConsoleThread.kt @@ -173,6 +173,9 @@ internal fun CommandReceiverParameter<*>.renderAsName(): String { val classifier = this.type.classifier.cast>() return when { classifier.isSubclassOf(ConsoleCommandSender::class) -> "控制台" + // 只有 classifier 明确为 SystemCommandSender 才有系统的意思 + // classifier 为子类时不满足 "系统" 的定义 + classifier == SystemCommandSender::class -> "系统" classifier.isSubclassOf(FriendCommandSenderOnMessage::class) -> "好友私聊" classifier.isSubclassOf(FriendCommandSender::class) -> "好友" classifier.isSubclassOf(MemberCommandSenderOnMessage::class) -> "群内发言"