Add ImageArgumentParser, fix #183

This commit is contained in:
Him188 2020-09-20 19:32:00 +08:00
parent 704674698f
commit 453ad8f9e2
2 changed files with 28 additions and 11 deletions

View File

@ -20,6 +20,7 @@ import net.mamoe.mirai.console.permission.PermissionId
import net.mamoe.mirai.console.permission.PermitteeId import net.mamoe.mirai.console.permission.PermitteeId
import net.mamoe.mirai.console.util.ConsoleExperimentalApi import net.mamoe.mirai.console.util.ConsoleExperimentalApi
import net.mamoe.mirai.contact.* import net.mamoe.mirai.contact.*
import net.mamoe.mirai.message.data.Image
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
@ -74,6 +75,7 @@ public interface CommandArgumentContext {
Long::class with LongArgumentParser Long::class with LongArgumentParser
Double::class with DoubleArgumentParser Double::class with DoubleArgumentParser
Float::class with FloatArgumentParser Float::class with FloatArgumentParser
Image::class with ImageArgumentParser
Contact::class with ExistingContactArgumentParser Contact::class with ExistingContactArgumentParser
User::class with ExistingUserArgumentParser User::class with ExistingUserArgumentParser

View File

@ -19,10 +19,7 @@ import net.mamoe.mirai.console.permission.PermitteeId
import net.mamoe.mirai.contact.* import net.mamoe.mirai.contact.*
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.*
import net.mamoe.mirai.message.data.MessageContent
import net.mamoe.mirai.message.data.SingleMessage
import net.mamoe.mirai.message.data.content
/** /**
@ -80,15 +77,33 @@ public object StringArgumentParser : InternalCommandArgumentParserExtensions<Str
public override fun parse(raw: String, sender: CommandSender): String = raw public override fun parse(raw: String, sender: CommandSender): String = raw
} }
/**
* 解析 [String] 通过 [Image].
*/
public object ImageArgumentParser : InternalCommandArgumentParserExtensions<Image> {
public override fun parse(raw: String, sender: CommandSender): Image {
return kotlin.runCatching {
Image(raw)
}.getOrElse {
illegalArgument("无法解析 $raw 为图片.")
}
}
override fun parse(raw: MessageContent, sender: CommandSender): Image {
if (raw is Image) return raw
return super.parse(raw, sender)
}
}
/** /**
* 当字符串内容为(不区分大小写) "true", "yes", "enabled" * 当字符串内容为(不区分大小写) "true", "yes", "enabled"
*/ */
public object BooleanArgumentParser : InternalCommandArgumentParserExtensions<Boolean> { public object BooleanArgumentParser : InternalCommandArgumentParserExtensions<Boolean> {
public override fun parse(raw: String, sender: CommandSender): Boolean = raw.trim().let { str -> public 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)
|| str.equals("enabled", ignoreCase = true) || str.equals("enabled", ignoreCase = true)
|| str.equals("on", ignoreCase = true) || str.equals("on", ignoreCase = true)
} }
} }
@ -365,10 +380,10 @@ internal interface InternalCommandArgumentParserExtensions<T : Any> : CommandArg
} else { } else {
var index = 1 var index = 1
illegalArgument("无法找到成员 $idOrCard。 多个成员满足搜索结果或匹配度不足: \n\n" + illegalArgument("无法找到成员 $idOrCard。 多个成员满足搜索结果或匹配度不足: \n\n" +
candidates.joinToString("\n", limit = 6) { candidates.joinToString("\n", limit = 6) {
val percentage = (it.second * 100).toDecimalPlace(0) val percentage = (it.second * 100).toDecimalPlace(0)
"#${index++}(${percentage}%)${it.first.nameCardOrNick.truncate(10)}(${it.first.id})" // #1 15.4% "#${index++}(${percentage}%)${it.first.nameCardOrNick.truncate(10)}(${it.first.id})" // #1 15.4%
} }
) )
} }
} }