From f24042f31efbd82bb1275fd1b6ff711b71cca5d0 Mon Sep 17 00:00:00 2001 From: Him188 Date: Sat, 12 Sep 2020 16:25:24 +0800 Subject: [PATCH] Install built-in CommandArgumentParsers for PermissionId and PermissibleIdentifier --- .../description/CommandArgumentContext.kt | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) 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 e071fd0ff..920b7b857 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 @@ -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( val klass: KClass, - val parser: CommandArgumentParser + val parser: CommandArgumentParser, ) public operator fun get(klass: KClass): CommandArgumentParser? @@ -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>): */ @Suppress("UNCHECKED_CAST") public class SimpleCommandArgumentContext( - public val list: List> + public val list: List>, ) : CommandArgumentContext { override fun get(klass: KClass): CommandArgumentParser? = (this.list.firstOrNull { klass == it.klass }?.parser @@ -208,7 +215,7 @@ public class CommandArgumentContextBuilder : MutableList> by mutab @JvmSynthetic @LowPriorityInOverloadResolution public inline infix fun KClass.with( - crossinline parser: CommandArgumentParser.(s: String, sender: CommandSender) -> T + crossinline parser: CommandArgumentParser.(s: String, sender: CommandSender) -> T, ): CommandArgumentContextBuilder { add(ParserPair(this, object : CommandArgumentParser { override fun parse(raw: String, sender: CommandSender): T = parser(raw, sender) @@ -221,7 +228,7 @@ public class CommandArgumentContextBuilder : MutableList> by mutab */ @JvmSynthetic public inline infix fun KClass.with( - crossinline parser: CommandArgumentParser.(s: String) -> T + crossinline parser: CommandArgumentParser.(s: String) -> T, ): CommandArgumentContextBuilder { add(ParserPair(this, object : CommandArgumentParser { override fun parse(raw: String, sender: CommandSender): T = parser(raw) @@ -241,7 +248,7 @@ public class CommandArgumentContextBuilder : MutableList> by mutab @ConsoleExperimentalApi @JvmSynthetic public inline infix fun add( - crossinline parser: CommandArgumentParser<*>.(s: String) -> T + crossinline parser: CommandArgumentParser<*>.(s: String) -> T, ): CommandArgumentContextBuilder = T::class with object : CommandArgumentParser { override fun parse(raw: String, sender: CommandSender): T = parser(raw) } @@ -253,7 +260,7 @@ public class CommandArgumentContextBuilder : MutableList> by mutab @JvmSynthetic @LowPriorityInOverloadResolution public inline infix fun add( - crossinline parser: CommandArgumentParser<*>.(s: String, sender: CommandSender) -> T + crossinline parser: CommandArgumentParser<*>.(s: String, sender: CommandSender) -> T, ): CommandArgumentContextBuilder = T::class with object : CommandArgumentParser { override fun parse(raw: String, sender: CommandSender): T = parser(raw, sender) }