Install built-in CommandArgumentParsers for PermissionId and PermissibleIdentifier

This commit is contained in:
Him188 2020-09-12 16:25:24 +08:00
parent 8f1cc48c6b
commit f24042f31e

View File

@ -16,6 +16,9 @@ import net.mamoe.mirai.console.command.CommandSender
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.permission.ExperimentalPermission
import net.mamoe.mirai.console.permission.PermissibleIdentifier
import net.mamoe.mirai.console.permission.PermissionId
import net.mamoe.mirai.console.util.ConsoleExperimentalApi
import net.mamoe.mirai.contact.*
import kotlin.internal.LowPriorityInOverloadResolution
@ -43,7 +46,7 @@ public interface CommandArgumentContext {
*/
public data class ParserPair<T : Any>(
val klass: KClass<T>,
val parser: CommandArgumentParser<T>
val parser: CommandArgumentParser<T>,
)
public operator fun <T : Any> get(klass: KClass<out T>): CommandArgumentParser<T>?
@ -63,6 +66,7 @@ public interface CommandArgumentContext {
/**
* 内建的默认 [CommandArgumentParser]
*/
@OptIn(ExperimentalPermission::class)
public object Builtins : CommandArgumentContext by (buildCommandArgumentContext {
Int::class with IntArgumentParser
Byte::class with ByteArgumentParser
@ -79,6 +83,9 @@ public interface CommandArgumentContext {
Group::class with ExistingGroupArgumentParser
Friend::class with ExistingFriendArgumentParser
Bot::class with ExistingBotArgumentParser
PermissionId::class with PermissionIdArgumentParser
PermissibleIdentifier::class with PermissibleIdentifierArgumentParser
})
}
@ -134,7 +141,7 @@ public operator fun CommandArgumentContext.plus(replacer: List<ParserPair<*>>):
*/
@Suppress("UNCHECKED_CAST")
public class SimpleCommandArgumentContext(
public val list: List<ParserPair<*>>
public val list: List<ParserPair<*>>,
) : CommandArgumentContext {
override fun <T : Any> get(klass: KClass<out T>): CommandArgumentParser<T>? =
(this.list.firstOrNull { klass == it.klass }?.parser
@ -208,7 +215,7 @@ public class CommandArgumentContextBuilder : MutableList<ParserPair<*>> by mutab
@JvmSynthetic
@LowPriorityInOverloadResolution
public inline infix fun <T : Any> KClass<T>.with(
crossinline parser: CommandArgumentParser<T>.(s: String, sender: CommandSender) -> T
crossinline parser: CommandArgumentParser<T>.(s: String, sender: CommandSender) -> T,
): CommandArgumentContextBuilder {
add(ParserPair(this, object : CommandArgumentParser<T> {
override fun parse(raw: String, sender: CommandSender): T = parser(raw, sender)
@ -221,7 +228,7 @@ public class CommandArgumentContextBuilder : MutableList<ParserPair<*>> by mutab
*/
@JvmSynthetic
public inline infix fun <T : Any> KClass<T>.with(
crossinline parser: CommandArgumentParser<T>.(s: String) -> T
crossinline parser: CommandArgumentParser<T>.(s: String) -> T,
): CommandArgumentContextBuilder {
add(ParserPair(this, object : CommandArgumentParser<T> {
override fun parse(raw: String, sender: CommandSender): T = parser(raw)
@ -241,7 +248,7 @@ public class CommandArgumentContextBuilder : MutableList<ParserPair<*>> by mutab
@ConsoleExperimentalApi
@JvmSynthetic
public inline infix fun <reified T : Any> add(
crossinline parser: CommandArgumentParser<*>.(s: String) -> T
crossinline parser: CommandArgumentParser<*>.(s: String) -> T,
): CommandArgumentContextBuilder = T::class with object : CommandArgumentParser<T> {
override fun parse(raw: String, sender: CommandSender): T = parser(raw)
}
@ -253,7 +260,7 @@ public class CommandArgumentContextBuilder : MutableList<ParserPair<*>> by mutab
@JvmSynthetic
@LowPriorityInOverloadResolution
public inline infix fun <reified T : Any> add(
crossinline parser: CommandArgumentParser<*>.(s: String, sender: CommandSender) -> T
crossinline parser: CommandArgumentParser<*>.(s: String, sender: CommandSender) -> T,
): CommandArgumentContextBuilder = T::class with object : CommandArgumentParser<T> {
override fun parse(raw: String, sender: CommandSender): T = parser(raw, sender)
}