From 8fc4da127b1352043c603ea31615e739f587cfe9 Mon Sep 17 00:00:00 2001 From: Him188 Date: Sat, 12 Sep 2020 16:25:58 +0800 Subject: [PATCH] Extract executeCommandInternal --- .../command/executeCommandInternal.kt | 64 +++++++++++++++++++ .../console/internal/command/internal.kt | 53 +-------------- 2 files changed, 65 insertions(+), 52 deletions(-) create mode 100644 backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/command/executeCommandInternal.kt 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 new file mode 100644 index 000000000..57414b6b5 --- /dev/null +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/command/executeCommandInternal.kt @@ -0,0 +1,64 @@ +/* + * Copyright 2019-2020 Mamoe Technologies and contributors. + * + * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. + * Use of this source code is governed by the GNU AFFERO GENERAL PUBLIC LICENSE version 3 license that can be found via the following link. + * + * https://github.com/mamoe/mirai/blob/master/LICENSE + */ + +package net.mamoe.mirai.console.internal.command + +import net.mamoe.mirai.console.command.* +import net.mamoe.mirai.console.permission.ExperimentalPermission +import net.mamoe.mirai.console.permission.PermissionService.Companion.testPermission +import net.mamoe.mirai.message.data.MessageChain +import net.mamoe.mirai.message.data.asMessageChain + +@OptIn(ExperimentalPermission::class) +@JvmSynthetic +@Throws(CommandExecutionException::class) +internal suspend fun CommandSender.executeCommandInternal( + command: Command, + args: MessageChain, + commandName: String, + checkPermission: Boolean, +): CommandExecuteResult { + if (checkPermission && !command.permission.testPermission(this)) { + return CommandExecuteResult.PermissionDenied(command, commandName) + } + + kotlin.runCatching { + command.onCommand(this, args) + }.fold( + onSuccess = { + return CommandExecuteResult.Success( + commandName = commandName, + command = command, + args = args + ) + }, + onFailure = { + return CommandExecuteResult.ExecutionFailed( + commandName = commandName, + command = command, + exception = it, + args = args + ) + } + ) +} + + +@JvmSynthetic +internal suspend fun CommandSender.executeCommandInternal( + messages: Any, + commandName: String, + checkPermission: Boolean, +): CommandExecuteResult { + val command = + CommandManagerImpl.matchCommand(commandName) ?: return CommandExecuteResult.CommandNotFound(commandName) + val args = messages.flattenCommandComponents() + + return executeCommandInternal(command, args.drop(1).asMessageChain(), commandName, checkPermission) +} diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/command/internal.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/command/internal.kt index 650a254f5..876ca8f95 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/command/internal.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/command/internal.kt @@ -9,16 +9,13 @@ package net.mamoe.mirai.console.internal.command -import net.mamoe.mirai.console.command.* +import net.mamoe.mirai.console.command.Command import net.mamoe.mirai.console.command.Command.Companion.primaryName import net.mamoe.mirai.console.permission.ExperimentalPermission import net.mamoe.mirai.console.permission.Permission import net.mamoe.mirai.console.permission.PermissionService -import net.mamoe.mirai.console.permission.PermissionService.Companion.testPermission import net.mamoe.mirai.contact.Group import net.mamoe.mirai.contact.Member -import net.mamoe.mirai.message.data.MessageChain -import net.mamoe.mirai.message.data.asMessageChain import kotlin.math.max import kotlin.math.min @@ -148,51 +145,3 @@ internal fun Command.createOrFindCommandPermission(parent: Permission): Permissi } //// internal - -@OptIn(ExperimentalPermission::class) -@JvmSynthetic -@Throws(CommandExecutionException::class) -internal suspend fun CommandSender.executeCommandInternal( - command: Command, - args: MessageChain, - commandName: String, - checkPermission: Boolean -): CommandExecuteResult { - if (checkPermission && !command.permission.testPermission(this)) { - return CommandExecuteResult.PermissionDenied(command, commandName) - } - - kotlin.runCatching { - command.onCommand(this, args) - }.fold( - onSuccess = { - return CommandExecuteResult.Success( - commandName = commandName, - command = command, - args = args - ) - }, - onFailure = { - return CommandExecuteResult.ExecutionFailed( - commandName = commandName, - command = command, - exception = it, - args = args - ) - } - ) -} - - -@JvmSynthetic -internal suspend fun CommandSender.executeCommandInternal( - messages: Any, - commandName: String, - checkPermission: Boolean -): CommandExecuteResult { - val command = - CommandManagerImpl.matchCommand(commandName) ?: return CommandExecuteResult.CommandNotFound(commandName) - val args = messages.flattenCommandComponents() - - return executeCommandInternal(command, args.drop(1).asMessageChain(), commandName, checkPermission) -}