diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/MiraiConsole.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/MiraiConsole.kt index 65fd1eda2..c3576ca47 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/MiraiConsole.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/MiraiConsole.kt @@ -34,7 +34,7 @@ import kotlin.coroutines.CoroutineContext * * @see INSTANCE */ -interface MiraiConsole { +interface MiraiConsole : CoroutineScope { /** * Console 运行路径 */ diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/CompositeCommand.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/CompositeCommand.kt index a4ef6361f..d797c9ae5 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/CompositeCommand.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/CompositeCommand.kt @@ -21,8 +21,8 @@ import kotlin.reflect.KClass /** - * 功能最集中的Commend - * 支持且只支持有sub的指令 + * 功能最集中的 Commend + * 只支持有sub的指令 * 例: * /mute add * /mute remove @@ -40,9 +40,7 @@ abstract class CompositeCommand @JvmOverloads constructor( final override val description = description.trimIndent() final override val names: Array = names.map(String::trim).filterNot(String::isEmpty).map(String::toLowerCase).also { list -> - list.firstOrNull { !it.isValidSubName() }?.let { - error("Name is not valid: $it") - } + list.firstOrNull { !it.isValidSubName() }?.let { error("Name is not valid: $it") } }.toTypedArray() /** @@ -72,9 +70,9 @@ abstract class CompositeCommand @JvmOverloads constructor( @Target(AnnotationTarget.VALUE_PARAMETER) annotation class Name(val name: String) - final override suspend fun onCommand(sender: CommandSender, args: Array) { - matchSubCommand(args)?.parseAndExecute(sender, args) ?: kotlin.run { - defaultSubCommand.onCommand(sender, args) + final override suspend fun CommandSender.onCommand(args: Array) { + matchSubCommand(args)?.parseAndExecute(this, args) ?: kotlin.run { + defaultSubCommand.onCommand(this, args) } } } diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/description/CommandArgParserBuiltins.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/description/CommandArgParserBuiltins.kt index 49936f429..48a5c65ca 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/description/CommandArgParserBuiltins.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/description/CommandArgParserBuiltins.kt @@ -23,44 +23,44 @@ import net.mamoe.mirai.message.data.SingleMessage import net.mamoe.mirai.message.data.content -object IntArgParser : CommandArgParser() { +object IntArgParser : CommandArgParser { override fun parse(raw: String, sender: CommandSender): Int = raw.toIntOrNull() ?: illegalArgument("无法解析 $raw 为整数") } -object LongArgParser : CommandArgParser() { +object LongArgParser : CommandArgParser { override fun parse(raw: String, sender: CommandSender): Long = raw.toLongOrNull() ?: illegalArgument("无法解析 $raw 为长整数") } -object ShortArgParser : CommandArgParser() { +object ShortArgParser : CommandArgParser { override fun parse(raw: String, sender: CommandSender): Short = raw.toShortOrNull() ?: illegalArgument("无法解析 $raw 为短整数") } -object ByteArgParser : CommandArgParser() { +object ByteArgParser : CommandArgParser { override fun parse(raw: String, sender: CommandSender): Byte = raw.toByteOrNull() ?: illegalArgument("无法解析 $raw 为字节") } -object DoubleArgParser : CommandArgParser() { +object DoubleArgParser : CommandArgParser { override fun parse(raw: String, sender: CommandSender): Double = raw.toDoubleOrNull() ?: illegalArgument("无法解析 $raw 为小数") } -object FloatArgParser : CommandArgParser() { +object FloatArgParser : CommandArgParser { override fun parse(raw: String, sender: CommandSender): Float = raw.toFloatOrNull() ?: illegalArgument("无法解析 $raw 为小数") } -object StringArgParser : CommandArgParser() { +object StringArgParser : CommandArgParser { override fun parse(raw: String, sender: CommandSender): String { println("STRING PARSER! $raw") return raw } } -object BooleanArgParser : CommandArgParser() { +object BooleanArgParser : CommandArgParser { override fun parse(raw: String, sender: CommandSender): Boolean = raw.trim().let { str -> str.equals("true", ignoreCase = true) || str.equals("yes", ignoreCase = true) @@ -74,7 +74,7 @@ object BooleanArgParser : CommandArgParser() { * output: Bot * errors: String->Int convert, Bot Not Exist */ -object ExistBotArgParser : CommandArgParser() { +object ExistBotArgParser : CommandArgParser { override fun parse(raw: String, sender: CommandSender): Bot { val uin = try { raw.toLong() @@ -89,7 +89,7 @@ object ExistBotArgParser : CommandArgParser() { } } -object ExistFriendArgParser : CommandArgParser() { +object ExistFriendArgParser : CommandArgParser { //Bot.friend //friend //~ = self @@ -142,7 +142,7 @@ object ExistFriendArgParser : CommandArgParser() { } } -object ExistGroupArgParser : CommandArgParser() { +object ExistGroupArgParser : CommandArgParser { override fun parse(raw: String, sender: CommandSender): Group { //by default if ((raw == "" || raw == "~") && sender is MemberCommandSender) { @@ -177,7 +177,7 @@ object ExistGroupArgParser : CommandArgParser() { } } -object ExistMemberArgParser : CommandArgParser() { +object ExistMemberArgParser : CommandArgParser { //后台: Bot.Group.Member[QQ/名片] //私聊: Group.Member[QQ/名片] //群内: Q号 diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/internal/CompositeCommandImpl.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/internal/CompositeCommandImpl.kt index cc4a145b8..8e52c9e2e 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/internal/CompositeCommandImpl.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/internal/CompositeCommandImpl.kt @@ -114,15 +114,16 @@ internal abstract class CompositeCommandImpl : Command { return block } - @JvmField - internal val bakedCommandNameToSubDescriptorArray: Map, SubCommandDescriptor> = kotlin.run { - val map = LinkedHashMap, SubCommandDescriptor>(subCommands.size * 2) - for (descriptor in subCommands) { - for (name in descriptor.bakedSubNames) { - map[name] = descriptor + internal val bakedCommandNameToSubDescriptorArray: Map, SubCommandDescriptor> by lazy { + kotlin.run { + val map = LinkedHashMap, SubCommandDescriptor>(subCommands.size * 2) + for (descriptor in subCommands) { + for (name in descriptor.bakedSubNames) { + map[name] = descriptor + } } + map.toSortedMap(Comparator { o1, o2 -> o1!!.contentHashCode() - o2!!.contentHashCode() }) } - map.toSortedMap(Comparator { o1, o2 -> o1!!.contentHashCode() - o2!!.contentHashCode() }) } internal class DefaultSubCommandDescriptor(