From 38f8773b988639d81bf6a1ded7b7d97cd66654dd Mon Sep 17 00:00:00 2001 From: Him188 Date: Wed, 18 Nov 2020 11:16:15 +0800 Subject: [PATCH] Remove chat context commandListener from backend --- .../MiraiConsoleImplementationBridge.kt | 3 +- .../internal/command/CommandManagerImpl.kt | 34 +++++++++++++------ 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/backend/mirai-console/src/internal/MiraiConsoleImplementationBridge.kt b/backend/mirai-console/src/internal/MiraiConsoleImplementationBridge.kt index b86024a88..7ead9dae3 100644 --- a/backend/mirai-console/src/internal/MiraiConsoleImplementationBridge.kt +++ b/backend/mirai-console/src/internal/MiraiConsoleImplementationBridge.kt @@ -29,7 +29,6 @@ import net.mamoe.mirai.console.extensions.PermissionServiceProvider import net.mamoe.mirai.console.extensions.PostStartupExtension import net.mamoe.mirai.console.extensions.SingletonExtensionSelector import net.mamoe.mirai.console.internal.command.CommandConfig -import net.mamoe.mirai.console.internal.command.CommandManagerImpl import net.mamoe.mirai.console.internal.data.builtins.AutoLoginConfig import net.mamoe.mirai.console.internal.data.builtins.ConsoleDataScope import net.mamoe.mirai.console.internal.data.builtins.LoggerConfig @@ -201,7 +200,7 @@ internal object MiraiConsoleImplementationBridge : CoroutineScope, MiraiConsoleI BuiltInCommands.registerAll() mainLogger.verbose { "Prepared built-in commands: ${BuiltInCommands.all.joinToString { it.primaryName }}" } CommandManager - CommandManagerImpl.commandListener // start + // CommandManagerImpl.commandListener // start } phase `enable plugins`@{ diff --git a/backend/mirai-console/src/internal/command/CommandManagerImpl.kt b/backend/mirai-console/src/internal/command/CommandManagerImpl.kt index 92944888b..6d5dcb02f 100644 --- a/backend/mirai-console/src/internal/command/CommandManagerImpl.kt +++ b/backend/mirai-console/src/internal/command/CommandManagerImpl.kt @@ -28,6 +28,7 @@ import net.mamoe.mirai.console.internal.util.ifNull import net.mamoe.mirai.console.permission.PermissionService.Companion.testPermission import net.mamoe.mirai.console.util.CoroutineScopeUtils.childScope import net.mamoe.mirai.event.Listener +import net.mamoe.mirai.event.Listener.ConcurrencyKind.CONCURRENT import net.mamoe.mirai.event.subscribeAlways import net.mamoe.mirai.message.MessageEvent import net.mamoe.mirai.message.data.Message @@ -67,35 +68,48 @@ internal object CommandManagerImpl : CommandManager, CoroutineScope by MiraiCons return optionalPrefixCommandMap[commandName.toLowerCase()] } + @Deprecated("Deprecated since 1.0.0, to be extended by plugin.") internal val commandListener: Listener by lazy { subscribeAlways( coroutineContext = CoroutineExceptionHandler { _, throwable -> logger.error(throwable) }, - concurrency = Listener.ConcurrencyKind.CONCURRENT, - priority = Listener.EventPriority.HIGH + concurrency = CONCURRENT, + priority = Listener.EventPriority.NORMAL ) { val sender = this.toCommandSender() + fun isDebugging(command: Command?): Boolean { + if (command?.prefixOptional == false || message.content.startsWith(CommandManager.commandPrefix)) { + if (MiraiConsoleImplementationBridge.loggerController.shouldLog("console.debug", SimpleLogger.LogPriority.DEBUG)) { + return true + } + } + return false + } + when (val result = executeCommand(sender, message)) { is CommandExecuteResult.PermissionDenied -> { - if (!result.command.prefixOptional || message.content.startsWith(CommandManager.commandPrefix)) { - if (MiraiConsoleImplementationBridge.loggerController.shouldLog("console.debug", SimpleLogger.LogPriority.DEBUG)) { - sender.sendMessage("权限不足") - } - intercept() + if (isDebugging(result.command)) { + sender.sendMessage("权限不足. ${CommandManager.commandPrefix}${result.command.primaryName} 需要权限 ${result.command.permission.id}.") + // intercept() } } is CommandExecuteResult.IllegalArgument -> { result.exception.message?.let { sender.sendMessage(it) } - intercept() + // intercept() } is CommandExecuteResult.Success -> { - intercept() + // intercept() } is CommandExecuteResult.ExecutionFailed -> { sender.catchExecutionException(result.exception) - intercept() + // intercept() + } + is CommandExecuteResult.Intercepted -> { + if (isDebugging(result.command)) { + sender.sendMessage("指令执行被拦截, 原因: ${result.reason}") + } } is CommandExecuteResult.UnmatchedSignature, is CommandExecuteResult.UnresolvedCommand,