Fix build and tests

This commit is contained in:
Him188 2020-06-26 20:13:09 +08:00
parent 2ba18ab7e4
commit d4f80b44fe
4 changed files with 27 additions and 28 deletions

View File

@ -34,7 +34,7 @@ import kotlin.coroutines.CoroutineContext
* *
* @see INSTANCE * @see INSTANCE
*/ */
interface MiraiConsole { interface MiraiConsole : CoroutineScope {
/** /**
* Console 运行路径 * Console 运行路径
*/ */

View File

@ -21,8 +21,8 @@ import kotlin.reflect.KClass
/** /**
* 功能最集中的Commend * 功能最集中的 Commend
* 支持且只支持有sub的指令 * 只支持有sub的指令
* : * :
* /mute add * /mute add
* /mute remove * /mute remove
@ -40,9 +40,7 @@ abstract class CompositeCommand @JvmOverloads constructor(
final override val description = description.trimIndent() final override val description = description.trimIndent()
final override val names: Array<out String> = final override val names: Array<out String> =
names.map(String::trim).filterNot(String::isEmpty).map(String::toLowerCase).also { list -> names.map(String::trim).filterNot(String::isEmpty).map(String::toLowerCase).also { list ->
list.firstOrNull { !it.isValidSubName() }?.let { list.firstOrNull { !it.isValidSubName() }?.let { error("Name is not valid: $it") }
error("Name is not valid: $it")
}
}.toTypedArray() }.toTypedArray()
/** /**
@ -72,9 +70,9 @@ abstract class CompositeCommand @JvmOverloads constructor(
@Target(AnnotationTarget.VALUE_PARAMETER) @Target(AnnotationTarget.VALUE_PARAMETER)
annotation class Name(val name: String) annotation class Name(val name: String)
final override suspend fun onCommand(sender: CommandSender, args: Array<out Any>) { final override suspend fun CommandSender.onCommand(args: Array<out Any>) {
matchSubCommand(args)?.parseAndExecute(sender, args) ?: kotlin.run { matchSubCommand(args)?.parseAndExecute(this, args) ?: kotlin.run {
defaultSubCommand.onCommand(sender, args) defaultSubCommand.onCommand(this, args)
} }
} }
} }

View File

@ -23,44 +23,44 @@ import net.mamoe.mirai.message.data.SingleMessage
import net.mamoe.mirai.message.data.content import net.mamoe.mirai.message.data.content
object IntArgParser : CommandArgParser<Int>() { object IntArgParser : CommandArgParser<Int> {
override fun parse(raw: String, sender: CommandSender): Int = override fun parse(raw: String, sender: CommandSender): Int =
raw.toIntOrNull() ?: illegalArgument("无法解析 $raw 为整数") raw.toIntOrNull() ?: illegalArgument("无法解析 $raw 为整数")
} }
object LongArgParser : CommandArgParser<Long>() { object LongArgParser : CommandArgParser<Long> {
override fun parse(raw: String, sender: CommandSender): Long = override fun parse(raw: String, sender: CommandSender): Long =
raw.toLongOrNull() ?: illegalArgument("无法解析 $raw 为长整数") raw.toLongOrNull() ?: illegalArgument("无法解析 $raw 为长整数")
} }
object ShortArgParser : CommandArgParser<Short>() { object ShortArgParser : CommandArgParser<Short> {
override fun parse(raw: String, sender: CommandSender): Short = override fun parse(raw: String, sender: CommandSender): Short =
raw.toShortOrNull() ?: illegalArgument("无法解析 $raw 为短整数") raw.toShortOrNull() ?: illegalArgument("无法解析 $raw 为短整数")
} }
object ByteArgParser : CommandArgParser<Byte>() { object ByteArgParser : CommandArgParser<Byte> {
override fun parse(raw: String, sender: CommandSender): Byte = override fun parse(raw: String, sender: CommandSender): Byte =
raw.toByteOrNull() ?: illegalArgument("无法解析 $raw 为字节") raw.toByteOrNull() ?: illegalArgument("无法解析 $raw 为字节")
} }
object DoubleArgParser : CommandArgParser<Double>() { object DoubleArgParser : CommandArgParser<Double> {
override fun parse(raw: String, sender: CommandSender): Double = override fun parse(raw: String, sender: CommandSender): Double =
raw.toDoubleOrNull() ?: illegalArgument("无法解析 $raw 为小数") raw.toDoubleOrNull() ?: illegalArgument("无法解析 $raw 为小数")
} }
object FloatArgParser : CommandArgParser<Float>() { object FloatArgParser : CommandArgParser<Float> {
override fun parse(raw: String, sender: CommandSender): Float = override fun parse(raw: String, sender: CommandSender): Float =
raw.toFloatOrNull() ?: illegalArgument("无法解析 $raw 为小数") raw.toFloatOrNull() ?: illegalArgument("无法解析 $raw 为小数")
} }
object StringArgParser : CommandArgParser<String>() { object StringArgParser : CommandArgParser<String> {
override fun parse(raw: String, sender: CommandSender): String { override fun parse(raw: String, sender: CommandSender): String {
println("STRING PARSER! $raw") println("STRING PARSER! $raw")
return raw return raw
} }
} }
object BooleanArgParser : CommandArgParser<Boolean>() { object BooleanArgParser : CommandArgParser<Boolean> {
override fun parse(raw: String, sender: CommandSender): Boolean = raw.trim().let { str -> override fun parse(raw: String, sender: CommandSender): Boolean = raw.trim().let { str ->
str.equals("true", ignoreCase = true) str.equals("true", ignoreCase = true)
|| str.equals("yes", ignoreCase = true) || str.equals("yes", ignoreCase = true)
@ -74,7 +74,7 @@ object BooleanArgParser : CommandArgParser<Boolean>() {
* output: Bot * output: Bot
* errors: String->Int convert, Bot Not Exist * errors: String->Int convert, Bot Not Exist
*/ */
object ExistBotArgParser : CommandArgParser<Bot>() { object ExistBotArgParser : CommandArgParser<Bot> {
override fun parse(raw: String, sender: CommandSender): Bot { override fun parse(raw: String, sender: CommandSender): Bot {
val uin = try { val uin = try {
raw.toLong() raw.toLong()
@ -89,7 +89,7 @@ object ExistBotArgParser : CommandArgParser<Bot>() {
} }
} }
object ExistFriendArgParser : CommandArgParser<Friend>() { object ExistFriendArgParser : CommandArgParser<Friend> {
//Bot.friend //Bot.friend
//friend //friend
//~ = self //~ = self
@ -142,7 +142,7 @@ object ExistFriendArgParser : CommandArgParser<Friend>() {
} }
} }
object ExistGroupArgParser : CommandArgParser<Group>() { object ExistGroupArgParser : CommandArgParser<Group> {
override fun parse(raw: String, sender: CommandSender): Group { override fun parse(raw: String, sender: CommandSender): Group {
//by default //by default
if ((raw == "" || raw == "~") && sender is MemberCommandSender) { if ((raw == "" || raw == "~") && sender is MemberCommandSender) {
@ -177,7 +177,7 @@ object ExistGroupArgParser : CommandArgParser<Group>() {
} }
} }
object ExistMemberArgParser : CommandArgParser<Member>() { object ExistMemberArgParser : CommandArgParser<Member> {
//后台: Bot.Group.Member[QQ/名片] //后台: Bot.Group.Member[QQ/名片]
//私聊: Group.Member[QQ/名片] //私聊: Group.Member[QQ/名片]
//群内: Q号 //群内: Q号

View File

@ -114,8 +114,8 @@ internal abstract class CompositeCommandImpl : Command {
return block return block
} }
@JvmField internal val bakedCommandNameToSubDescriptorArray: Map<Array<String>, SubCommandDescriptor> by lazy {
internal val bakedCommandNameToSubDescriptorArray: Map<Array<String>, SubCommandDescriptor> = kotlin.run { kotlin.run {
val map = LinkedHashMap<Array<String>, SubCommandDescriptor>(subCommands.size * 2) val map = LinkedHashMap<Array<String>, SubCommandDescriptor>(subCommands.size * 2)
for (descriptor in subCommands) { for (descriptor in subCommands) {
for (name in descriptor.bakedSubNames) { for (name in descriptor.bakedSubNames) {
@ -124,6 +124,7 @@ internal abstract class CompositeCommandImpl : Command {
} }
map.toSortedMap(Comparator { o1, o2 -> o1!!.contentHashCode() - o2!!.contentHashCode() }) map.toSortedMap(Comparator { o1, o2 -> o1!!.contentHashCode() - o2!!.contentHashCode() })
} }
}
internal class DefaultSubCommandDescriptor( internal class DefaultSubCommandDescriptor(
val description: String, val description: String,