diff --git a/mirai-console/backend/mirai-console/src/command/CompositeCommand.kt b/mirai-console/backend/mirai-console/src/command/CompositeCommand.kt index d713256d41..a20bb847c1 100644 --- a/mirai-console/backend/mirai-console/src/command/CompositeCommand.kt +++ b/mirai-console/backend/mirai-console/src/command/CompositeCommand.kt @@ -18,7 +18,6 @@ import net.mamoe.mirai.console.internal.command.CommandReflector import net.mamoe.mirai.console.internal.command.CompositeCommandSubCommandAnnotationResolver import net.mamoe.mirai.console.permission.Permission import net.mamoe.mirai.console.util.ConsoleExperimentalApi -import kotlin.DeprecationLevel.* import kotlin.annotation.AnnotationRetention.RUNTIME import kotlin.annotation.AnnotationTarget.FUNCTION diff --git a/mirai-console/backend/mirai-console/src/internal/command/CommandReflector.kt b/mirai-console/backend/mirai-console/src/internal/command/CommandReflector.kt index 98b135396f..0c176103f9 100644 --- a/mirai-console/backend/mirai-console/src/internal/command/CommandReflector.kt +++ b/mirai-console/backend/mirai-console/src/internal/command/CommandReflector.kt @@ -98,24 +98,26 @@ internal object CompositeCommandSubCommandAnnotationResolver : @Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER") internal object GroupedCommandSubCommandAnnotationResolver : - SubCommandAnnotationResolver { - override fun isDeclaredSubCommand(ownerCommand: Any, function: KFunction<*>) = + SubCommandAnnotationResolver { + override fun isDeclaredSubCommand(ownerCommand: SubCommandGroup, function: KFunction<*>) = function.hasAnnotation() - override fun getDeclaredSubCommandNames(ownerCommand: Any, function: KFunction<*>): Array { + override fun getDeclaredSubCommandNames(ownerCommand: SubCommandGroup, function: KFunction<*>): Array { val annotated = function.findAnnotation()!!.value return if (annotated.isEmpty()) arrayOf(function.name) else annotated } - override fun getAnnotatedName(ownerCommand: Any, parameter: KParameter): String? = null + override fun getAnnotatedName(ownerCommand: SubCommandGroup, parameter: KParameter): String? = + parameter.findAnnotation()?.value - override fun getDescription(ownerCommand: Any, function: KFunction<*>): String? = null + override fun getDescription(ownerCommand: SubCommandGroup, function: KFunction<*>): String? = + function.findAnnotation()?.value - override fun isCombinedSubCommands(command: Any, kProperty: KProperty<*>): Boolean = + override fun isCombinedSubCommands(command: SubCommandGroup, kProperty: KProperty<*>): Boolean = kProperty.hasAnnotation() || kProperty.hasAnnotation() - override fun getCombinedAdditionNames(command: Any, kProperty: KProperty<*>): Array { + override fun getCombinedAdditionNames(command: SubCommandGroup, kProperty: KProperty<*>): Array { return if (kProperty.hasAnnotation()) { emptyArray() } else { @@ -516,4 +518,4 @@ internal class SubCommandReflector( private fun KParameter.nameForCommandParameter(): String? = annotationResolver.getAnnotatedName(owner, this) ?: this.name -} +} \ No newline at end of file diff --git a/mirai-console/backend/mirai-console/test/command/InstanceTestCommand.kt b/mirai-console/backend/mirai-console/test/command/InstanceTestCommand.kt index 38932f1441..1afea87849 100644 --- a/mirai-console/backend/mirai-console/test/command/InstanceTestCommand.kt +++ b/mirai-console/backend/mirai-console/test/command/InstanceTestCommand.kt @@ -37,12 +37,29 @@ import kotlin.test.* import net.mamoe.mirai.console.command.SubCommandGroup.FlattenSubCommands +/** + * 测试:CompositeCommand下接3种子节点;AbstractSubCommandGroup下接3种子节点; + * + * + * [MyUnifiedCommand : CompositeCommand] + * | + * ------------------------------------------------------- + * | \ \ + * [ModuleB : AbstractSubCommandGroup] [functionA0:KFunction] [ModuleE : CompositeCommand] + * | + * ----------------------------------------------------------------- + * | \ \ + * [ModuleC : AbstractSubCommandGroup] [ModuleD : CompositeCommand] [functionB0/functionB1:KFunction] + * + */ class MyUnifiedCommand : CompositeCommand( owner, "testMyUnifiedCommand", "tsMUC" ) { - class ModuleAPartB : AbstractSubCommandGroup() { + class ModuleB : AbstractSubCommandGroup() { + @FlattenSubCommands + val moduleC = ModuleC() @FlattenSubCommands - val partC = ModuleAPartC() + val moduleDInB = ModuleD() @SubCommand fun functionB0(arg0: Int) { @@ -54,7 +71,7 @@ class MyUnifiedCommand : CompositeCommand( } } - class ModuleAPartC : AbstractSubCommandGroup() { + class ModuleC : AbstractSubCommandGroup() { @SubCommand fun functionC0(arg0: Int) { Testing.ok(arg0) @@ -65,8 +82,33 @@ class MyUnifiedCommand : CompositeCommand( } } - @FlattenSubCommands // 新增若干, 不带前缀注册指令, 执行 `/base function10` `/base functionCustomName11` - val partB = ModuleAPartB() + class ModuleD : CompositeCommand(owner, "USELESS") { + @SubCommand + fun functionD0(arg0: Int) { + Testing.ok(arg0) + } + @SubCommand("customNameD1") + fun functionD1(arg0: Int) { + Testing.ok(arg0) + } + } + + class ModuleE : CompositeCommand(owner, "USELESS") { + @SubCommand + fun functionE0(arg0: Int) { + Testing.ok(arg0) + } + @SubCommand("customNameE1") + fun functionE1(arg0: Int) { + Testing.ok(arg0) + } + } + + @FlattenSubCommands + val moduleB = ModuleB() + + @FlattenSubCommands + val moduleE = ModuleE() @SubCommand fun functionA0(arg0: Int) { @@ -558,6 +600,21 @@ internal class InstanceTestCommand : AbstractConsoleInstanceTest() { assertEquals(0, withTesting { assertSuccess(unifiedCompositeCommand.execute(sender, "customNameC1 0")) }) + assertEquals(0, withTesting { + assertSuccess(unifiedCompositeCommand.execute(sender, "functionD0 0")) + }) + assertEquals(0, withTesting { + assertSuccess(unifiedCompositeCommand.execute(sender, "customNameD1 0")) + }) + assertEquals(true, unifiedCompositeCommand.usage.contains("/${unifiedCompositeCommand.primaryName} functionA0 ")) + assertEquals(true, unifiedCompositeCommand.usage.contains("/${unifiedCompositeCommand.primaryName} functionB0 ")) + assertEquals(true, unifiedCompositeCommand.usage.contains("/${unifiedCompositeCommand.primaryName} customNameB1 ")) + assertEquals(true, unifiedCompositeCommand.usage.contains("/${unifiedCompositeCommand.primaryName} functionC0 ")) + assertEquals(true, unifiedCompositeCommand.usage.contains("/${unifiedCompositeCommand.primaryName} customNameC1 ")) + assertEquals(true, unifiedCompositeCommand.usage.contains("/${unifiedCompositeCommand.primaryName} functionD0 ")) + assertEquals(true, unifiedCompositeCommand.usage.contains("/${unifiedCompositeCommand.primaryName} customNameD1 ")) + assertEquals(true, unifiedCompositeCommand.usage.contains("/${unifiedCompositeCommand.primaryName} functionE0 ")) + assertEquals(true, unifiedCompositeCommand.usage.contains("/${unifiedCompositeCommand.primaryName} customNameE1 ")) } }