Add ExistingContactArgumentParser for Contact

This commit is contained in:
Him188 2020-09-01 20:46:12 +08:00
parent bd91d93fc9
commit e676117744
2 changed files with 40 additions and 5 deletions

View File

@ -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<Contact> {
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 <T> 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")
}
}
}
/**
* 解析任意一个群成员.
*/

View File

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