diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/CommandExecuteResult.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/CommandExecuteResult.kt index f2195ae3b..9076951b4 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/CommandExecuteResult.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/CommandExecuteResult.kt @@ -55,6 +55,21 @@ public sealed class CommandExecuteResult { public override val status: CommandExecuteStatus get() = CommandExecuteStatus.SUCCESSFUL } + /** 执行执行时发生了一个非法参数错误 */ + public class IllegalArgument( + /** 指令执行时发生的错误 */ + public override val exception: IllegalArgumentException, + /** 尝试执行的指令 */ + public override val command: Command, + /** 尝试执行的指令名 */ + public override val commandName: String, + /** 基础分割后的实际参数列表, 元素类型可能为 [Message] 或 [String] */ + public override val args: MessageChain + ) : CommandExecuteResult() { + /** 指令最终执行状态, 总是 [CommandExecuteStatus.EXECUTION_EXCEPTION] */ + public override val status: CommandExecuteStatus get() = CommandExecuteStatus.ILLEGAL_ARGUMENT + } + /** 指令执行过程出现了错误 */ public class ExecutionFailed( /** 指令执行时发生的错误 */ @@ -119,7 +134,9 @@ public sealed class CommandExecuteResult { COMMAND_NOT_FOUND, /** 权限不足 */ - PERMISSION_DENIED + PERMISSION_DENIED, + /** 非法参数 */ + ILLEGAL_ARGUMENT, } } @@ -138,6 +155,18 @@ public fun CommandExecuteResult.isSuccess(): Boolean { return this is CommandExecuteResult.Success } +/** + * 当 [this] 为 [CommandExecuteResult.IllegalArgument] 时返回 `true` + */ +@JvmSynthetic +public fun CommandExecuteResult.isIllegalArgument(): Boolean { + contract { + returns(true) implies (this@isIllegalArgument is CommandExecuteResult.IllegalArgument) + returns(false) implies (this@isIllegalArgument !is CommandExecuteResult.IllegalArgument) + } + return this is CommandExecuteResult.IllegalArgument +} + /** * 当 [this] 为 [CommandExecuteResult.ExecutionFailed] 时返回 `true` */ diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/command/CommandManagerImpl.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/command/CommandManagerImpl.kt index eb34fb34a..14dfa51b2 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/command/CommandManagerImpl.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/command/CommandManagerImpl.kt @@ -72,6 +72,10 @@ internal object CommandManagerImpl : CommandManager, CoroutineScope by Coroutine intercept() } } + is CommandExecuteResult.IllegalArgument -> { + result.exception.message?.let { sender.sendMessage(it) } + intercept() + } is CommandExecuteResult.Success -> { intercept() } diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/command/executeCommandInternal.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/command/executeCommandInternal.kt index 703a7e062..a9ff54cca 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/command/executeCommandInternal.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/command/executeCommandInternal.kt @@ -36,11 +36,16 @@ internal suspend fun CommandSender.executeCommandInternal( args = args ) }, - onFailure = { - return CommandExecuteResult.ExecutionFailed( + onFailure = { exception -> + return if (exception is IllegalArgumentException) CommandExecuteResult.IllegalArgument( commandName = commandName, command = command, - exception = it, + exception = exception, + args = args + ) else CommandExecuteResult.ExecutionFailed( + commandName = commandName, + command = command, + exception = exception, args = args ) } diff --git a/frontend/mirai-console-terminal/src/main/kotlin/net/mamoe/mirai/console/terminal/ConsoleThread.kt b/frontend/mirai-console-terminal/src/main/kotlin/net/mamoe/mirai/console/terminal/ConsoleThread.kt index a21b63c1e..021d58ae6 100644 --- a/frontend/mirai-console-terminal/src/main/kotlin/net/mamoe/mirai/console/terminal/ConsoleThread.kt +++ b/frontend/mirai-console-terminal/src/main/kotlin/net/mamoe/mirai/console/terminal/ConsoleThread.kt @@ -15,11 +15,8 @@ import kotlinx.coroutines.CoroutineName import kotlinx.coroutines.delay import kotlinx.coroutines.launch import net.mamoe.mirai.console.MiraiConsole -import net.mamoe.mirai.console.command.BuiltInCommands -import net.mamoe.mirai.console.command.CommandExecuteStatus -import net.mamoe.mirai.console.command.CommandManager +import net.mamoe.mirai.console.command.* import net.mamoe.mirai.console.command.CommandManager.INSTANCE.executeCommand -import net.mamoe.mirai.console.command.ConsoleCommandSender import net.mamoe.mirai.console.terminal.noconsole.NoConsole import net.mamoe.mirai.console.util.ConsoleInternalApi import net.mamoe.mirai.console.util.requestInput @@ -65,6 +62,9 @@ internal fun startupConsoleThread() { when (result.status) { CommandExecuteStatus.SUCCESSFUL -> { } + CommandExecuteStatus.ILLEGAL_ARGUMENT -> { + result.exception?.message?.let { consoleLogger.warning(it) } + } CommandExecuteStatus.EXECUTION_EXCEPTION -> { result.exception?.let(consoleLogger::error) }