Update command execute result

This commit is contained in:
Karlatemp 2020-06-22 12:49:02 +08:00
parent 2620d323e6
commit 64d13f0a19
No known key found for this signature in database
GPG Key ID: 21FBDDF664FF06F8
4 changed files with 66 additions and 31 deletions

View File

@ -39,7 +39,7 @@ internal object MiraiConsoleInitializer {
internal object MiraiConsoleBuildConstants { // auto-filled on build (task :mirai-console:fillBuildConstants)
@JvmStatic
val buildDate: Date = Date(1592723625351) // 2020-06-21 15:13:45
val buildDate: Date = Date(1592799753404L) // 2020-06-22 12:22:33
const val version: String = "0.5.1"
}

View File

@ -138,8 +138,10 @@ fun Command.unregister(): Boolean = InternalCommandManager.modifyLock.withLock {
*
* @see JCommandManager.executeCommand Java 方法
*/
suspend fun CommandSender.executeCommand(vararg messages: Any): Boolean {
if (messages.isEmpty()) return false
suspend fun CommandSender.executeCommand(vararg messages: Any): CommandExecuteResult {
if (messages.isEmpty()) return CommandExecuteResult(
status = CommandExecuteStatus.EMPTY_COMMAND
)
return executeCommandInternal(
messages,
messages[0].let { if (it is SingleMessage) it.toString() else it.toString().substringBefore(' ') })
@ -154,8 +156,10 @@ internal inline fun <reified T> List<T>.dropToTypedArray(n: Int): Array<T> = Arr
*
* @see JCommandManager.executeCommand Java 方法
*/
suspend fun CommandSender.executeCommand(message: MessageChain): Boolean {
if (message.isEmpty()) return false
suspend fun CommandSender.executeCommand(message: MessageChain): CommandExecuteResult {
if (message.isEmpty()) return CommandExecuteResult(
status = CommandExecuteStatus.EMPTY_COMMAND
)
return executeCommandInternal(message, message[0].toString())
}
@ -163,9 +167,44 @@ suspend fun CommandSender.executeCommand(message: MessageChain): Boolean {
internal suspend inline fun CommandSender.executeCommandInternal(
messages: Any,
commandName: String
): Boolean {
val command = InternalCommandManager.matchCommand(commandName) ?: return false
): CommandExecuteResult {
val command = InternalCommandManager.matchCommand(commandName) ?: return CommandExecuteResult(
status = CommandExecuteStatus.COMMAND_NOT_FOUND,
commandName = commandName
)
val rawInput = messages.flattenCommandComponents()
command.onCommand(this, rawInput.dropToTypedArray(1))
return true
}
kotlin.runCatching {
command.onCommand(this, rawInput.dropToTypedArray(1))
}.onFailure {
return CommandExecuteResult(
status = CommandExecuteStatus.FAILED,
commandName = commandName,
command = command,
exception = it
)
}
return CommandExecuteResult(
status = CommandExecuteStatus.SUCCESSFUL,
commandName = commandName,
command = command
)
}
/**
* 命令的执行返回
*/
class CommandExecuteResult(
val status: CommandExecuteStatus,
val exception: Throwable? = null,
val command: Command? = null,
val commandName: String? = null
) {
/**
* 命令的执行状态
*/
enum class CommandExecuteStatus {
SUCCESSFUL, FAILED, COMMAND_NOT_FOUND, EMPTY_COMMAND
}
}
typealias CommandExecuteStatus = CommandExecuteResult.CommandExecuteStatus

View File

@ -22,22 +22,13 @@ kotlin {
}
}
var debugging = true
dependencies {
fun import0(dep: Any) {
if (debugging) {
implementation(dep)
} else {
compileOnly(dep)
}
}
import0("org.jline:jline:3.15.0")
import0("org.fusesource.jansi:jansi:1.18")
implementation("org.jline:jline:3.15.0")
implementation("org.fusesource.jansi:jansi:1.18")
import0(project(":mirai-console"))
import0("net.mamoe:mirai-core:${Versions.core}")
import0(kotlin("stdlib")) // embedded by core
compileAndRuntime(project(":mirai-console"))
compileAndRuntime("net.mamoe:mirai-core:${Versions.core}")
compileAndRuntime(kotlin("stdlib")) // embedded by core
testApi("net.mamoe:mirai-core-qqandroid:${Versions.core}")
testApi(project(":mirai-console"))

View File

@ -20,13 +20,12 @@
package net.mamoe.mirai.console.pure
import net.mamoe.mirai.console.MiraiConsoleInitializer
import net.mamoe.mirai.console.command.CommandExecuteStatus
import net.mamoe.mirai.console.command.ConsoleCommandSender
import net.mamoe.mirai.console.command.executeCommand
import net.mamoe.mirai.message.data.Message
import net.mamoe.mirai.message.data.PlainText
import net.mamoe.mirai.utils.DefaultLogger
import net.mamoe.mirai.utils.PlatformLogger
import org.fusesource.jansi.Ansi
import kotlin.concurrent.thread
object MiraiConsolePureLoader {
@ -49,12 +48,18 @@ internal fun startConsoleThread() {
while (true) {
val next = MiraiConsoleFrontEndPure.requestInput("")
consoleLogger.debug("INPUT> $next")
kotlin.runCatching {
if (!ConsoleCS.executeCommand(PlainText(next))) { // No such command
consoleLogger.warning("Unknown command: " + next.split(' ')[0])
val result = ConsoleCS.executeCommand(PlainText(next))
when (result.status) {
CommandExecuteStatus.SUCCESSFUL -> {
}
CommandExecuteStatus.EMPTY_COMMAND -> {
}
CommandExecuteStatus.FAILED -> {
consoleLogger.error("An error occurred while executing the command: $next", result.exception)
}
CommandExecuteStatus.COMMAND_NOT_FOUND -> {
consoleLogger.warning("Unknown command: ${result.commandName}")
}
}.onFailure {
consoleLogger.error("Exception in executing command: $next", it)
}
}
}