mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-11 02:50:15 +08:00
Add ExistingUserArgumentParser
This commit is contained in:
parent
e2e922905b
commit
e0f535e937
@ -15,6 +15,7 @@ import net.mamoe.mirai.console.internal.command.fuzzySearchMember
|
|||||||
import net.mamoe.mirai.contact.Friend
|
import net.mamoe.mirai.contact.Friend
|
||||||
import net.mamoe.mirai.contact.Group
|
import net.mamoe.mirai.contact.Group
|
||||||
import net.mamoe.mirai.contact.Member
|
import net.mamoe.mirai.contact.Member
|
||||||
|
import net.mamoe.mirai.contact.User
|
||||||
import net.mamoe.mirai.getFriendOrNull
|
import net.mamoe.mirai.getFriendOrNull
|
||||||
import net.mamoe.mirai.getGroupOrNull
|
import net.mamoe.mirai.getGroupOrNull
|
||||||
import net.mamoe.mirai.message.data.At
|
import net.mamoe.mirai.message.data.At
|
||||||
@ -180,6 +181,57 @@ public object ExistingGroupArgumentParser : InternalCommandArgumentParserExtensi
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public object ExistingUserArgumentParser : InternalCommandArgumentParserExtensions<User> {
|
||||||
|
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 <T> 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` 参数即可
|
* 当只登录了一个 [Bot] 时, 无需上述 `botId` 参数即可
|
||||||
*/
|
*/
|
||||||
public object ExistMemberArgumentParser : InternalCommandArgumentParserExtensions<Member> {
|
public object ExistingMemberArgumentParser : InternalCommandArgumentParserExtensions<Member> {
|
||||||
private val syntax: String = """
|
private val syntax: String = """
|
||||||
- `botId.group.memberId`
|
- `botId.group.memberId`
|
||||||
- `botId.group.memberCard` (模糊搜索, 寻找最优匹配)
|
- `botId.group.memberCard` (模糊搜索, 寻找最优匹配)
|
||||||
@ -265,7 +317,9 @@ internal interface InternalCommandArgumentParserExtensions<T : Any> : CommandArg
|
|||||||
fun CommandSender.inferBotOrFail(): Bot = (this as? BotAwareCommandSender)?.bot ?: illegalArgument("当前语境下无法推断目标群员")
|
fun CommandSender.inferBotOrFail(): Bot = (this as? BotAwareCommandSender)?.bot ?: illegalArgument("当前语境下无法推断目标群员")
|
||||||
|
|
||||||
fun CommandSender.inferGroupOrFail(): Group =
|
fun CommandSender.inferGroupOrFail(): Group =
|
||||||
(this as? GroupAwareCommandSender)?.group ?: illegalArgument("当前语境下无法推断目标群")
|
inferGroup() ?: illegalArgument("当前语境下无法推断目标群")
|
||||||
|
|
||||||
|
fun CommandSender.inferGroup(): Group? = (this as? GroupAwareCommandSender)?.group
|
||||||
|
|
||||||
fun CommandSender.inferFriendOrFail(): Friend =
|
fun CommandSender.inferFriendOrFail(): Friend =
|
||||||
(this as? FriendCommandSender)?.user ?: illegalArgument("当前语境下无法推断目标好友")
|
(this as? FriendCommandSender)?.user ?: illegalArgument("当前语境下无法推断目标好友")
|
||||||
|
@ -20,6 +20,7 @@ import net.mamoe.mirai.console.util.ConsoleExperimentalAPI
|
|||||||
import net.mamoe.mirai.contact.Friend
|
import net.mamoe.mirai.contact.Friend
|
||||||
import net.mamoe.mirai.contact.Group
|
import net.mamoe.mirai.contact.Group
|
||||||
import net.mamoe.mirai.contact.Member
|
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
|
||||||
@ -75,7 +76,8 @@ public interface CommandArgumentContext {
|
|||||||
Double::class with DoubleArgumentParser
|
Double::class with DoubleArgumentParser
|
||||||
Float::class with FloatArgumentParser
|
Float::class with FloatArgumentParser
|
||||||
|
|
||||||
Member::class with ExistMemberArgumentParser
|
User::class with ExistingUserArgumentParser
|
||||||
|
Member::class with ExistingMemberArgumentParser
|
||||||
Group::class with ExistingGroupArgumentParser
|
Group::class with ExistingGroupArgumentParser
|
||||||
Friend::class with ExistingFriendArgumentParser
|
Friend::class with ExistingFriendArgumentParser
|
||||||
Bot::class with ExistingBotArgumentParser
|
Bot::class with ExistingBotArgumentParser
|
||||||
@ -137,7 +139,8 @@ public class SimpleCommandArgumentContext(
|
|||||||
public val list: List<ParserPair<*>>
|
public val list: List<ParserPair<*>>
|
||||||
) : CommandArgumentContext {
|
) : CommandArgumentContext {
|
||||||
override fun <T : Any> get(klass: KClass<out T>): CommandArgumentParser<T>? =
|
override fun <T : Any> get(klass: KClass<out T>): CommandArgumentParser<T>? =
|
||||||
this.list.firstOrNull { klass.isSubclassOf(it.klass) }?.parser as CommandArgumentParser<T>?
|
(this.list.firstOrNull { klass == it.klass }?.parser
|
||||||
|
?: this.list.firstOrNull { klass.isSubclassOf(it.klass) }?.parser) as CommandArgumentParser<T>?
|
||||||
|
|
||||||
override fun toList(): List<ParserPair<*>> = list
|
override fun toList(): List<ParserPair<*>> = list
|
||||||
}
|
}
|
||||||
|
@ -19,6 +19,7 @@ import net.mamoe.mirai.console.command.SimpleCommand
|
|||||||
import net.mamoe.mirai.contact.Friend
|
import net.mamoe.mirai.contact.Friend
|
||||||
import net.mamoe.mirai.contact.Group
|
import net.mamoe.mirai.contact.Group
|
||||||
import net.mamoe.mirai.contact.Member
|
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.SingleMessage
|
||||||
import net.mamoe.mirai.message.data.content
|
import net.mamoe.mirai.message.data.content
|
||||||
import kotlin.contracts.InvocationKind
|
import kotlin.contracts.InvocationKind
|
||||||
@ -43,7 +44,8 @@ import kotlin.contracts.contract
|
|||||||
* - [Bot]: [ExistingBotArgumentParser]
|
* - [Bot]: [ExistingBotArgumentParser]
|
||||||
* - [Friend]: [ExistingFriendArgumentParser]
|
* - [Friend]: [ExistingFriendArgumentParser]
|
||||||
* - [Group]: [ExistingGroupArgumentParser]
|
* - [Group]: [ExistingGroupArgumentParser]
|
||||||
* - [Member]: [ExistMemberArgumentParser]
|
* - [Member]: [ExistingMemberArgumentParser]
|
||||||
|
* - [User]: [ExistingUserArgumentParser]
|
||||||
*
|
*
|
||||||
*
|
*
|
||||||
* @see SimpleCommand 简单指令
|
* @see SimpleCommand 简单指令
|
||||||
|
Loading…
Reference in New Issue
Block a user