mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-10 18:40:15 +08:00
Add ExistingContactArgumentParser for Contact
This commit is contained in:
parent
bd91d93fc9
commit
e676117744
@ -176,7 +176,7 @@ public object ExistingUserArgumentParser : InternalCommandArgumentParserExtensio
|
|||||||
- `botId.groupId.memberId`
|
- `botId.groupId.memberId`
|
||||||
- `botId.groupId.memberCard` (模糊搜索, 寻找最优匹配)
|
- `botId.groupId.memberCard` (模糊搜索, 寻找最优匹配)
|
||||||
- `~` (指代指令调用人自己. 仅聊天环境下)
|
- `~` (指代指令调用人自己. 仅聊天环境下)
|
||||||
- `groupId.$` (随机成员. 仅聊天环境下)
|
- `botId.groupId.$` (随机成员. )
|
||||||
- `botId.friendId
|
- `botId.friendId
|
||||||
|
|
||||||
当处于一个群内时, `botId` 和 `groupId` 参数都可省略
|
当处于一个群内时, `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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 解析任意一个群成员.
|
* 解析任意一个群成员.
|
||||||
*/
|
*/
|
||||||
|
@ -17,10 +17,7 @@ import net.mamoe.mirai.console.command.CompositeCommand
|
|||||||
import net.mamoe.mirai.console.command.SimpleCommand
|
import net.mamoe.mirai.console.command.SimpleCommand
|
||||||
import net.mamoe.mirai.console.command.description.CommandArgumentContext.ParserPair
|
import net.mamoe.mirai.console.command.description.CommandArgumentContext.ParserPair
|
||||||
import net.mamoe.mirai.console.util.ConsoleExperimentalAPI
|
import net.mamoe.mirai.console.util.ConsoleExperimentalAPI
|
||||||
import net.mamoe.mirai.contact.Friend
|
import net.mamoe.mirai.contact.*
|
||||||
import net.mamoe.mirai.contact.Group
|
|
||||||
import net.mamoe.mirai.contact.Member
|
|
||||||
import net.mamoe.mirai.contact.User
|
|
||||||
import kotlin.internal.LowPriorityInOverloadResolution
|
import kotlin.internal.LowPriorityInOverloadResolution
|
||||||
import kotlin.reflect.KClass
|
import kotlin.reflect.KClass
|
||||||
import kotlin.reflect.full.isSubclassOf
|
import kotlin.reflect.full.isSubclassOf
|
||||||
@ -76,6 +73,7 @@ public interface CommandArgumentContext {
|
|||||||
Double::class with DoubleArgumentParser
|
Double::class with DoubleArgumentParser
|
||||||
Float::class with FloatArgumentParser
|
Float::class with FloatArgumentParser
|
||||||
|
|
||||||
|
Contact::class with ExistingContactArgumentParser
|
||||||
User::class with ExistingUserArgumentParser
|
User::class with ExistingUserArgumentParser
|
||||||
Member::class with ExistingMemberArgumentParser
|
Member::class with ExistingMemberArgumentParser
|
||||||
Group::class with ExistingGroupArgumentParser
|
Group::class with ExistingGroupArgumentParser
|
||||||
|
Loading…
Reference in New Issue
Block a user