diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/CommandDescriptor.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/CommandDescriptor.kt index ac509ed74..a5866c826 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/CommandDescriptor.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/CommandDescriptor.kt @@ -68,7 +68,7 @@ class CommandDescriptor( /** * `fullName + aliases` */ - val allNames = arrayOf(fullName, *aliases) + val allNames: Array = arrayOf(fullName, *aliases) override fun equals(other: Any?): Boolean { if (this === other) return true @@ -117,7 +117,7 @@ internal fun Any.flattenCommandComponents(): Sequence = when (this) { is String -> splitToSequence(' ').filterNot { it.isBlank() } is PlainText -> content.flattenCommandComponents() is SingleMessage -> sequenceOf(this) - is MessageChain -> this.asSequence().map { it.flattenCommandComponents() } + is MessageChain -> this.asSequence().flatMap { it.flattenCommandComponents() } else -> throw IllegalArgumentException("Illegal component: $this") } @@ -134,11 +134,14 @@ internal fun CommandFullName.checkFullName(errorHint: String): CommandFullName { inline fun CommandDescriptor( vararg fullName: Any, block: CommandDescriptorBuilder.() -> Unit = {} -): CommandDescriptor = CommandDescriptorBuilder(fullName).apply(block).build() +): CommandDescriptor = CommandDescriptorBuilder(*fullName).apply(block).build() class CommandDescriptorBuilder( - vararg val fullName: Any + vararg fullName: Any ) { + @PublishedApi + internal var fullName: CommandFullName = fullName.checkFullName("fullName") + @PublishedApi internal var context: CommandParserContext = CommandParserContext.Builtins diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/CommandManager.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/CommandManager.kt index 915307de3..19a64f127 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/CommandManager.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/CommandManager.kt @@ -25,16 +25,14 @@ fun CommandOwner.unregisterAllCommands() { * 注册一个指令. 若此指令已经注册或有已经注册的指令与 [allNames] 重名, 返回 `false` */ fun Command.register(): Boolean = InternalCommandManager.modifyLock.withLock { - with(descriptor) { - if (findDuplicate() != null) { - return false - } - InternalCommandManager.registeredCommands.add(this@register) - for (name in this.allNames) { - InternalCommandManager.nameToCommandMap[name] = this@register - } - return true + if (findDuplicate() != null) { + return false } + InternalCommandManager.registeredCommands.add(this@register) + for (name in this.allNames) { + InternalCommandManager.nameToCommandMap[name] = this@register + } + return true } /** @@ -85,8 +83,7 @@ suspend fun CommandSender.execute(command: Command, args: CommandArgs): Boolean } suspend fun Command.execute(sender: CommandSender, args: CommandArgs): Boolean = sender.execute(this, args) -suspend fun Command.execute(sender: CommandSender, vararg args: Any): Boolean = sender.execute(this, args) -suspend fun CommandSender.execute(command: Command, vararg args: Any): Boolean = command.execute(this, args) +suspend fun CommandSender.execute(vararg args: Any): Boolean = args.toList().executeCommand(this) internal suspend fun List.executeCommand(sender: CommandSender): Boolean { diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/DefaultCommands.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/DefaultCommands.kt index 09198e400..ad9be438e 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/DefaultCommands.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/DefaultCommands.kt @@ -10,24 +10,14 @@ package net.mamoe.mirai.console.command suspend fun main() { - ConsoleCommandSender.execute(DefaultCommands.Test, "test") -} -internal object DefaultCommands { - - object Test : ConsoleCommand( - CommandDescriptor("test") { - param() - } - ) { - override suspend fun onCommand(sender: CommandSender, args: CommandArgs): Boolean { - val s = args.getReified() - sender.sendMessage(s) - return true - } + if (ConsoleCommandSender.execute("test")) { + } } +internal object DefaultCommands + /* /** diff --git a/backend/mirai-console/src/test/java/net/mamoe/mirai/console/command/TestCommands.kt b/backend/mirai-console/src/test/java/net/mamoe/mirai/console/command/TestCommands.kt new file mode 100644 index 000000000..fdd46931c --- /dev/null +++ b/backend/mirai-console/src/test/java/net/mamoe/mirai/console/command/TestCommands.kt @@ -0,0 +1,75 @@ +/* + * Copyright 2020 Mamoe Technologies and contributors. + * + * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. + * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. + * + * https://github.com/mamoe/mirai/blob/master/LICENSE + */ + +@file:Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE") + +package net.mamoe.mirai.console.command + +import net.mamoe.mirai.console.plugins.PluginBase +import net.mamoe.mirai.message.data.PlainText +import net.mamoe.mirai.message.data.messageChainOf +import net.mamoe.mirai.message.data.toMessage +import org.junit.jupiter.api.Test +import kotlin.test.assertEquals +import kotlin.test.assertFalse +import kotlin.test.assertTrue + + +val plugin: PluginBase = object : PluginBase() { + +} + +internal object TestCommand : PluginCommand( + plugin, + CommandDescriptor("test") { + param() + } +) { + override suspend fun onCommand(sender: CommandSender, args: CommandArgs): Boolean { + val s = args.getReified() + sender.sendMessage(s) + return true + } +} + +internal class TestCommands { + @Test + fun testFlatten() { + assertEquals(listOf("test", "v1"), "test v1".flattenCommandComponents().toList()) + assertEquals(listOf("test", "v1"), PlainText("test v1").flattenCommandComponents().toList()) + assertEquals(listOf("test", "v1"), arrayOf("test ", "v1", " ").flattenCommandComponents().toList()) + assertEquals( + listOf("test", "v1"), + messageChainOf("test v1".toMessage(), " ".toMessage()).flattenCommandComponents().toList() + ) + } + + @Test + fun testRegister() { + assertTrue(TestCommand.register()) + assertEquals(listOf("test"), TestCommand.allNames.single().toList()) + + assertFalse(TestCommand.register()) + assertFalse( + object : PluginCommand( + plugin, + CommandDescriptor("test") { + param() + } + ) { + override suspend fun onCommand(sender: CommandSender, args: CommandArgs): Boolean { + val s = args.getReified() + sender.sendMessage(s) + return true + } + }.register() + ) + } +} +