From e67611774459427efa439cd8ee39f18ff9d13a16 Mon Sep 17 00:00:00 2001 From: Him188 Date: Tue, 1 Sep 2020 20:46:12 +0800 Subject: [PATCH] Add ExistingContactArgumentParser for Contact --- .../description/CommandArgParserBuiltins.kt | 39 ++++++++++++++++++- .../description/CommandArgumentContext.kt | 6 +-- 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/description/CommandArgParserBuiltins.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/description/CommandArgParserBuiltins.kt index ac68fe1d2..0e77dacc9 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/description/CommandArgParserBuiltins.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/description/CommandArgParserBuiltins.kt @@ -176,7 +176,7 @@ public object ExistingUserArgumentParser : InternalCommandArgumentParserExtensio - `botId.groupId.memberId` - `botId.groupId.memberCard` (模糊搜索, 寻找最优匹配) - `~` (指代指令调用人自己. 仅聊天环境下) - - `groupId.$` (随机成员. 仅聊天环境下) + - `botId.groupId.$` (随机成员. ) - `botId.friendId 当处于一个群内时, `botId` 和 `groupId` 参数都可省略 @@ -215,6 +215,43 @@ public object ExistingUserArgumentParser : InternalCommandArgumentParserExtensio } +public object ExistingContactArgumentParser : InternalCommandArgumentParserExtensions { + private val syntax: String = """ + - `botId.groupId.memberId` + - `botId.groupId.memberCard` (模糊搜索, 寻找最优匹配) + - `botId.groupId.$` (随机成员. 仅聊天环境下) + - `botId.friendId + - `botId.groupId` + + 当处于一个群内时, `botId` 和 `groupId` 参数都可省略 + 当只登录了一个 [Bot] 时, `botId` 参数可省略 + """.trimIndent() + + override fun parse(raw: String, sender: CommandSender): Contact { + return parseImpl(sender, raw, ExistingUserArgumentParser::parse, ExistingGroupArgumentParser::parse) + } + + override fun parse(raw: SingleMessage, sender: CommandSender): Contact { + return parseImpl(sender, raw, ExistingUserArgumentParser::parse, ExistingGroupArgumentParser::parse) + } + + private fun parseImpl( + sender: CommandSender, + raw: T, + parseFunction: (T, CommandSender) -> Contact, + parseFunction2: (T, CommandSender) -> Contact, + ): Contact { + kotlin.runCatching { + return parseFunction(raw, sender) + }.recoverCatching { + return parseFunction2(raw, sender) + }.getOrElse { + illegalArgument("无法推断目标好友, 群或群员. \n$syntax") + } + } +} + + /** * 解析任意一个群成员. */ diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/description/CommandArgumentContext.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/description/CommandArgumentContext.kt index ef2630235..f02c4e39d 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/description/CommandArgumentContext.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/description/CommandArgumentContext.kt @@ -17,10 +17,7 @@ import net.mamoe.mirai.console.command.CompositeCommand import net.mamoe.mirai.console.command.SimpleCommand import net.mamoe.mirai.console.command.description.CommandArgumentContext.ParserPair import net.mamoe.mirai.console.util.ConsoleExperimentalAPI -import net.mamoe.mirai.contact.Friend -import net.mamoe.mirai.contact.Group -import net.mamoe.mirai.contact.Member -import net.mamoe.mirai.contact.User +import net.mamoe.mirai.contact.* import kotlin.internal.LowPriorityInOverloadResolution import kotlin.reflect.KClass import kotlin.reflect.full.isSubclassOf @@ -76,6 +73,7 @@ public interface CommandArgumentContext { Double::class with DoubleArgumentParser Float::class with FloatArgumentParser + Contact::class with ExistingContactArgumentParser User::class with ExistingUserArgumentParser Member::class with ExistingMemberArgumentParser Group::class with ExistingGroupArgumentParser