Remove chat context commandListener from backend

This commit is contained in:
Him188 2020-11-18 11:16:15 +08:00
parent bb64c3b999
commit 38f8773b98
2 changed files with 25 additions and 12 deletions

View File

@ -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`@{

View File

@ -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<MessageEvent> 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,