Add CommandExecuteResult.IllegalArgument

This commit is contained in:
Karlatemp 2020-09-20 09:40:51 +08:00
parent b77a856ebe
commit 58d799581b
No known key found for this signature in database
GPG Key ID: 21FBDDF664FF06F8
4 changed files with 46 additions and 8 deletions

View File

@ -55,6 +55,21 @@ public sealed class CommandExecuteResult {
public override val status: CommandExecuteStatus get() = CommandExecuteStatus.SUCCESSFUL 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( public class ExecutionFailed(
/** 指令执行时发生的错误 */ /** 指令执行时发生的错误 */
@ -119,7 +134,9 @@ public sealed class CommandExecuteResult {
COMMAND_NOT_FOUND, COMMAND_NOT_FOUND,
/** 权限不足 */ /** 权限不足 */
PERMISSION_DENIED PERMISSION_DENIED,
/** 非法参数 */
ILLEGAL_ARGUMENT,
} }
} }
@ -138,6 +155,18 @@ public fun CommandExecuteResult.isSuccess(): Boolean {
return this is CommandExecuteResult.Success 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` * [this] [CommandExecuteResult.ExecutionFailed] 时返回 `true`
*/ */

View File

@ -72,6 +72,10 @@ internal object CommandManagerImpl : CommandManager, CoroutineScope by Coroutine
intercept() intercept()
} }
} }
is CommandExecuteResult.IllegalArgument -> {
result.exception.message?.let { sender.sendMessage(it) }
intercept()
}
is CommandExecuteResult.Success -> { is CommandExecuteResult.Success -> {
intercept() intercept()
} }

View File

@ -36,11 +36,16 @@ internal suspend fun CommandSender.executeCommandInternal(
args = args args = args
) )
}, },
onFailure = { onFailure = { exception ->
return CommandExecuteResult.ExecutionFailed( return if (exception is IllegalArgumentException) CommandExecuteResult.IllegalArgument(
commandName = commandName, commandName = commandName,
command = command, command = command,
exception = it, exception = exception,
args = args
) else CommandExecuteResult.ExecutionFailed(
commandName = commandName,
command = command,
exception = exception,
args = args args = args
) )
} }

View File

@ -15,11 +15,8 @@ import kotlinx.coroutines.CoroutineName
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import net.mamoe.mirai.console.MiraiConsole import net.mamoe.mirai.console.MiraiConsole
import net.mamoe.mirai.console.command.BuiltInCommands import net.mamoe.mirai.console.command.*
import net.mamoe.mirai.console.command.CommandExecuteStatus
import net.mamoe.mirai.console.command.CommandManager
import net.mamoe.mirai.console.command.CommandManager.INSTANCE.executeCommand 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.terminal.noconsole.NoConsole
import net.mamoe.mirai.console.util.ConsoleInternalApi import net.mamoe.mirai.console.util.ConsoleInternalApi
import net.mamoe.mirai.console.util.requestInput import net.mamoe.mirai.console.util.requestInput
@ -65,6 +62,9 @@ internal fun startupConsoleThread() {
when (result.status) { when (result.status) {
CommandExecuteStatus.SUCCESSFUL -> { CommandExecuteStatus.SUCCESSFUL -> {
} }
CommandExecuteStatus.ILLEGAL_ARGUMENT -> {
result.exception?.message?.let { consoleLogger.warning(it) }
}
CommandExecuteStatus.EXECUTION_EXCEPTION -> { CommandExecuteStatus.EXECUTION_EXCEPTION -> {
result.exception?.let(consoleLogger::error) result.exception?.let(consoleLogger::error)
} }