From 108e42508279f0ad23d8d8d4abf5b29f3daede98 Mon Sep 17 00:00:00 2001 From: Him188 Date: Sun, 28 Jun 2020 11:46:06 +0800 Subject: [PATCH] Fix checkSubCommand --- .../mirai/console/command/SimpleCommand.kt | 4 +-- .../description/CommandArgParserBuiltins.kt | 29 ++++--------------- .../internal/CompositeCommandInternal.kt | 6 ++-- .../mamoe/mirai/console/TestMiraiConosle.kt | 8 +++-- .../mirai/console/command/TestCommand.kt | 18 ++++++++++++ 5 files changed, 35 insertions(+), 30 deletions(-) diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/SimpleCommand.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/SimpleCommand.kt index bb7cf9bf0..a6199c129 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/SimpleCommand.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/SimpleCommand.kt @@ -41,8 +41,8 @@ abstract class SimpleCommand @JvmOverloads constructor( final override val context: CommandParserContext = CommandParserContext.Builtins + overrideContext - override fun checkSubCommand() { - super.checkSubCommand() + override fun checkSubCommand(subCommands: Array) { + super.checkSubCommand(subCommands) check(subCommands.size == 1) { "There can only be exactly one function annotated with Handler at this moment as overloading is not yet supported." } } diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/description/CommandArgParserBuiltins.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/description/CommandArgParserBuiltins.kt index 48a5c65ca..7ef7124f0 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/description/CommandArgParserBuiltins.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/description/CommandArgParserBuiltins.kt @@ -55,7 +55,6 @@ object FloatArgParser : CommandArgParser { object StringArgParser : CommandArgParser { override fun parse(raw: String, sender: CommandSender): String { - println("STRING PARSER! $raw") return raw } } @@ -76,16 +75,8 @@ object BooleanArgParser : CommandArgParser { */ object ExistBotArgParser : CommandArgParser { override fun parse(raw: String, sender: CommandSender): Bot { - val uin = try { - raw.toLong() - } catch (e: Exception) { - illegalArgument("无法识别QQ UIN$raw") - } - return try { - Bot.getInstance(uin) - } catch (e: NoSuchElementException) { - illegalArgument("无法找到Bot $uin") - } + val uin = raw.toLongOrNull() ?: illegalArgument("无法识别 QQ ID: $raw") + return Bot.getInstanceOrNull(uin) ?: illegalArgument("无法找到 Bot $uin") } } @@ -102,20 +93,12 @@ object ExistFriendArgParser : CommandArgParser { is UserCommandSender -> sender.user.id else -> illegalArgument("无法解析~作为默认") } - return try { - sender.bot.friends[targetID] - } catch (e: NoSuchElementException) { - illegalArgument("无法解析~作为默认") - } + + return sender.bot.friends.getOrNull(targetID) ?: illegalArgument("无法解析~作为默认") } if (sender is BotAwareCommandSender) { - return try { - sender.bot.friends[raw.toLong()] - } catch (e: NoSuchElementException) { - illegalArgument("无法找到" + raw + "这个好友") - } catch (e: NumberFormatException) { - illegalArgument("无法解析$raw") - } + return sender.bot.friends.getOrNull(raw.toLongOrNull() ?: illegalArgument("无法解析 $raw 为整数")) + ?: illegalArgument("无法找到" + raw + "这个好友") } else { raw.split(".").let { args -> if (args.size != 2) { diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/internal/CompositeCommandInternal.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/internal/CompositeCommandInternal.kt index 2a4db7397..ce89f9919 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/internal/CompositeCommandInternal.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/internal/CompositeCommandInternal.kt @@ -69,7 +69,7 @@ internal abstract class AbstractReflectionCommand @JvmOverloads constructor( ) } - internal open fun checkSubCommand() { + internal open fun checkSubCommand(subCommands: Array) { } @@ -90,7 +90,7 @@ internal abstract class AbstractReflectionCommand @JvmOverloads constructor( createSubCommand(function, context) }.toTypedArray().also { _usage = it.firstOrNull()?.usage ?: description - }.also { checkSubCommand() } + }.also { checkSubCommand(it) } } internal val bakedCommandNameToSubDescriptorArray: Map, SubCommandDescriptor> by lazy { @@ -252,7 +252,7 @@ internal fun AbstractReflectionCommand.createSubCommand( } val commandName = - function.findAnnotation()!!.value + subCommandAnnotationResolver.getSubCommandNames(function) .let { namesFromAnnotation -> if (namesFromAnnotation.isNotEmpty()) { namesFromAnnotation diff --git a/backend/mirai-console/src/test/kotlin/net/mamoe/mirai/console/TestMiraiConosle.kt b/backend/mirai-console/src/test/kotlin/net/mamoe/mirai/console/TestMiraiConosle.kt index b971d3be5..d718d48d4 100644 --- a/backend/mirai-console/src/test/kotlin/net/mamoe/mirai/console/TestMiraiConosle.kt +++ b/backend/mirai-console/src/test/kotlin/net/mamoe/mirai/console/TestMiraiConosle.kt @@ -15,6 +15,7 @@ import kotlinx.coroutines.suspendCancellableCoroutine import kotlinx.coroutines.withTimeout import net.mamoe.mirai.Bot import net.mamoe.mirai.console.command.ConsoleCommandSender +import net.mamoe.mirai.console.plugin.DeferredPluginLoader import net.mamoe.mirai.console.plugin.PluginLoader import net.mamoe.mirai.console.plugin.jvm.JarPluginLoader import net.mamoe.mirai.console.setting.MemorySettingStorage @@ -24,6 +25,7 @@ import net.mamoe.mirai.message.data.Message import net.mamoe.mirai.utils.DefaultLogger import net.mamoe.mirai.utils.LoginSolver import net.mamoe.mirai.utils.MiraiLogger +import net.mamoe.mirai.utils.PlatformLogger import java.io.File import kotlin.coroutines.Continuation import kotlin.coroutines.CoroutineContext @@ -35,13 +37,15 @@ fun initTestEnvironment() { MiraiConsoleInitializer.init(object : IMiraiConsole { override val rootDir: File = createTempDir() override val frontEnd: MiraiConsoleFrontEnd = object : MiraiConsoleFrontEnd { - override fun loggerFor(identity: String?): MiraiLogger = DefaultLogger(identity) + override val name: String get() = "Test" + override val version: String get() = "1.0.0" + override fun loggerFor(identity: String?): MiraiLogger = PlatformLogger(identity) override fun pushBot(bot: Bot) = println("pushBot: $bot") override suspend fun requestInput(hint: String): String = readLine()!! override fun createLoginSolver(): LoginSolver = LoginSolver.Default } override val mainLogger: MiraiLogger = DefaultLogger("main") - override val builtInPluginLoaders: List> = listOf(JarPluginLoader) + override val builtInPluginLoaders: List> = listOf(DeferredPluginLoader { JarPluginLoader }) override val consoleCommandSender: ConsoleCommandSender = object : ConsoleCommandSender() { override suspend fun sendMessage(message: Message) = println(message) } diff --git a/backend/mirai-console/src/test/kotlin/net/mamoe/mirai/console/command/TestCommand.kt b/backend/mirai-console/src/test/kotlin/net/mamoe/mirai/console/command/TestCommand.kt index ca9bbbfab..b26703001 100644 --- a/backend/mirai-console/src/test/kotlin/net/mamoe/mirai/console/command/TestCommand.kt +++ b/backend/mirai-console/src/test/kotlin/net/mamoe/mirai/console/command/TestCommand.kt @@ -193,4 +193,22 @@ internal class TestCommand { } } } + + @Test + fun `test simple command`() { + runBlocking { + + val simple = object : SimpleCommand(owner, "test") { + @Handler + fun onCommand(string: String) { + Testing.ok(string) + } + } + + simple.withRegistration { + assertEquals("xxx", withTesting { simple.execute(sender, "xxx") }) + assertEquals("xxx", withTesting { sender.executeCommand("/test xxx") }) + } + } + } } \ No newline at end of file