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 411c0bd00..394015122 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 @@ -15,6 +15,7 @@ import net.mamoe.mirai.console.internal.command.fuzzySearchMember 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.getFriendOrNull import net.mamoe.mirai.getGroupOrNull import net.mamoe.mirai.message.data.At @@ -180,6 +181,57 @@ public object ExistingGroupArgumentParser : InternalCommandArgumentParserExtensi } } +public object ExistingUserArgumentParser : InternalCommandArgumentParserExtensions { + private val syntax: String = """ + - `botId.group.memberId` + - `botId.group.memberCard` (模糊搜索, 寻找最优匹配) + - `~` (指代指令调用人自己. 仅聊天环境下) + - `$` (随机成员. 仅聊天环境下) + - `botId.friendId + + 当只登录了一个 [Bot] 时, 无需上述 `botId` 参数即可 + """.trimIndent() + + override fun parse(raw: String, sender: CommandSender): User { + return parseImpl(sender, raw, ExistingMemberArgumentParser::parse, ExistingFriendArgumentParser::parse) + } + + override fun parse(raw: SingleMessage, sender: CommandSender): User { + return parseImpl(sender, raw, ExistingMemberArgumentParser::parse, ExistingFriendArgumentParser::parse) + } + + private fun parseImpl( + sender: CommandSender, + raw: T, + parseFunction: (T, CommandSender) -> User, + parseFunction2: (T, CommandSender) -> User, + ): User { + if (sender.inferGroup() != null) { + kotlin.runCatching { + return parseFunction(raw, sender) + }.recoverCatching { + return parseFunction2(raw, sender) + }.getOrElse { + illegalArgument("无法推断目标好友或群员. \n$syntax") + } + } + if (Bot.botInstancesSequence.count() == 1) { + + kotlin.runCatching { + + }.getOrElse { + illegalArgument("无法推断目标好友或群员. \n$syntax") + } + } + kotlin.runCatching { + return parseFunction2(raw, sender) + }.getOrElse { + illegalArgument("无法推断目标好友或群员. \n$syntax") + } + } +} + + /** * 解析任意一个群成员. * @@ -190,7 +242,7 @@ public object ExistingGroupArgumentParser : InternalCommandArgumentParserExtensi * * 当只登录了一个 [Bot] 时, 无需上述 `botId` 参数即可 */ -public object ExistMemberArgumentParser : InternalCommandArgumentParserExtensions { +public object ExistingMemberArgumentParser : InternalCommandArgumentParserExtensions { private val syntax: String = """ - `botId.group.memberId` - `botId.group.memberCard` (模糊搜索, 寻找最优匹配) @@ -265,7 +317,9 @@ internal interface InternalCommandArgumentParserExtensions : CommandArg fun CommandSender.inferBotOrFail(): Bot = (this as? BotAwareCommandSender)?.bot ?: illegalArgument("当前语境下无法推断目标群员") fun CommandSender.inferGroupOrFail(): Group = - (this as? GroupAwareCommandSender)?.group ?: illegalArgument("当前语境下无法推断目标群") + inferGroup() ?: illegalArgument("当前语境下无法推断目标群") + + fun CommandSender.inferGroup(): Group? = (this as? GroupAwareCommandSender)?.group fun CommandSender.inferFriendOrFail(): Friend = (this as? FriendCommandSender)?.user ?: illegalArgument("当前语境下无法推断目标好友") 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 43435d7c3..ef2630235 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 @@ -20,6 +20,7 @@ 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 kotlin.internal.LowPriorityInOverloadResolution import kotlin.reflect.KClass import kotlin.reflect.full.isSubclassOf @@ -75,7 +76,8 @@ public interface CommandArgumentContext { Double::class with DoubleArgumentParser Float::class with FloatArgumentParser - Member::class with ExistMemberArgumentParser + User::class with ExistingUserArgumentParser + Member::class with ExistingMemberArgumentParser Group::class with ExistingGroupArgumentParser Friend::class with ExistingFriendArgumentParser Bot::class with ExistingBotArgumentParser @@ -137,7 +139,8 @@ public class SimpleCommandArgumentContext( public val list: List> ) : CommandArgumentContext { override fun get(klass: KClass): CommandArgumentParser? = - this.list.firstOrNull { klass.isSubclassOf(it.klass) }?.parser as CommandArgumentParser? + (this.list.firstOrNull { klass == it.klass }?.parser + ?: this.list.firstOrNull { klass.isSubclassOf(it.klass) }?.parser) as CommandArgumentParser? override fun toList(): List> = list } diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/description/CommandArgumentParser.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/description/CommandArgumentParser.kt index 83351d1e3..e84a75171 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/description/CommandArgumentParser.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/description/CommandArgumentParser.kt @@ -19,6 +19,7 @@ import net.mamoe.mirai.console.command.SimpleCommand 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.message.data.SingleMessage import net.mamoe.mirai.message.data.content import kotlin.contracts.InvocationKind @@ -43,7 +44,8 @@ import kotlin.contracts.contract * - [Bot]: [ExistingBotArgumentParser] * - [Friend]: [ExistingFriendArgumentParser] * - [Group]: [ExistingGroupArgumentParser] - * - [Member]: [ExistMemberArgumentParser] + * - [Member]: [ExistingMemberArgumentParser] + * - [User]: [ExistingUserArgumentParser] * * * @see SimpleCommand 简单指令