Fix checkSubCommand

This commit is contained in:
Him188 2020-06-28 11:46:06 +08:00
parent 6b23d5ba27
commit 108e425082
5 changed files with 35 additions and 30 deletions

View File

@ -41,8 +41,8 @@ abstract class SimpleCommand @JvmOverloads constructor(
final override val context: CommandParserContext = CommandParserContext.Builtins + overrideContext
override fun checkSubCommand() {
super.checkSubCommand()
override fun checkSubCommand(subCommands: Array<SubCommandDescriptor>) {
super.checkSubCommand(subCommands)
check(subCommands.size == 1) { "There can only be exactly one function annotated with Handler at this moment as overloading is not yet supported." }
}

View File

@ -55,7 +55,6 @@ object FloatArgParser : CommandArgParser<Float> {
object StringArgParser : CommandArgParser<String> {
override fun parse(raw: String, sender: CommandSender): String {
println("STRING PARSER! $raw")
return raw
}
}
@ -76,16 +75,8 @@ object BooleanArgParser : CommandArgParser<Boolean> {
*/
object ExistBotArgParser : CommandArgParser<Bot> {
override fun parse(raw: String, sender: CommandSender): Bot {
val uin = try {
raw.toLong()
} catch (e: Exception) {
illegalArgument("无法识别QQ UIN$raw")
}
return try {
Bot.getInstance(uin)
} catch (e: NoSuchElementException) {
illegalArgument("无法找到Bot $uin")
}
val uin = raw.toLongOrNull() ?: illegalArgument("无法识别 QQ ID: $raw")
return Bot.getInstanceOrNull(uin) ?: illegalArgument("无法找到 Bot $uin")
}
}
@ -102,20 +93,12 @@ object ExistFriendArgParser : CommandArgParser<Friend> {
is UserCommandSender -> sender.user.id
else -> illegalArgument("无法解析~作为默认")
}
return try {
sender.bot.friends[targetID]
} catch (e: NoSuchElementException) {
illegalArgument("无法解析~作为默认")
}
return sender.bot.friends.getOrNull(targetID) ?: illegalArgument("无法解析~作为默认")
}
if (sender is BotAwareCommandSender) {
return try {
sender.bot.friends[raw.toLong()]
} catch (e: NoSuchElementException) {
illegalArgument("无法找到" + raw + "这个好友")
} catch (e: NumberFormatException) {
illegalArgument("无法解析$raw")
}
return sender.bot.friends.getOrNull(raw.toLongOrNull() ?: illegalArgument("无法解析 $raw 为整数"))
?: illegalArgument("无法找到" + raw + "这个好友")
} else {
raw.split(".").let { args ->
if (args.size != 2) {

View File

@ -69,7 +69,7 @@ internal abstract class AbstractReflectionCommand @JvmOverloads constructor(
)
}
internal open fun checkSubCommand() {
internal open fun checkSubCommand(subCommands: Array<SubCommandDescriptor>) {
}
@ -90,7 +90,7 @@ internal abstract class AbstractReflectionCommand @JvmOverloads constructor(
createSubCommand(function, context)
}.toTypedArray().also {
_usage = it.firstOrNull()?.usage ?: description
}.also { checkSubCommand() }
}.also { checkSubCommand(it) }
}
internal val bakedCommandNameToSubDescriptorArray: Map<Array<String>, SubCommandDescriptor> by lazy {
@ -252,7 +252,7 @@ internal fun AbstractReflectionCommand.createSubCommand(
}
val commandName =
function.findAnnotation<CompositeCommand.SubCommand>()!!.value
subCommandAnnotationResolver.getSubCommandNames(function)
.let { namesFromAnnotation ->
if (namesFromAnnotation.isNotEmpty()) {
namesFromAnnotation

View File

@ -15,6 +15,7 @@ import kotlinx.coroutines.suspendCancellableCoroutine
import kotlinx.coroutines.withTimeout
import net.mamoe.mirai.Bot
import net.mamoe.mirai.console.command.ConsoleCommandSender
import net.mamoe.mirai.console.plugin.DeferredPluginLoader
import net.mamoe.mirai.console.plugin.PluginLoader
import net.mamoe.mirai.console.plugin.jvm.JarPluginLoader
import net.mamoe.mirai.console.setting.MemorySettingStorage
@ -24,6 +25,7 @@ import net.mamoe.mirai.message.data.Message
import net.mamoe.mirai.utils.DefaultLogger
import net.mamoe.mirai.utils.LoginSolver
import net.mamoe.mirai.utils.MiraiLogger
import net.mamoe.mirai.utils.PlatformLogger
import java.io.File
import kotlin.coroutines.Continuation
import kotlin.coroutines.CoroutineContext
@ -35,13 +37,15 @@ fun initTestEnvironment() {
MiraiConsoleInitializer.init(object : IMiraiConsole {
override val rootDir: File = createTempDir()
override val frontEnd: MiraiConsoleFrontEnd = object : MiraiConsoleFrontEnd {
override fun loggerFor(identity: String?): MiraiLogger = DefaultLogger(identity)
override val name: String get() = "Test"
override val version: String get() = "1.0.0"
override fun loggerFor(identity: String?): MiraiLogger = PlatformLogger(identity)
override fun pushBot(bot: Bot) = println("pushBot: $bot")
override suspend fun requestInput(hint: String): String = readLine()!!
override fun createLoginSolver(): LoginSolver = LoginSolver.Default
}
override val mainLogger: MiraiLogger = DefaultLogger("main")
override val builtInPluginLoaders: List<PluginLoader<*, *>> = listOf(JarPluginLoader)
override val builtInPluginLoaders: List<PluginLoader<*, *>> = listOf(DeferredPluginLoader { JarPluginLoader })
override val consoleCommandSender: ConsoleCommandSender = object : ConsoleCommandSender() {
override suspend fun sendMessage(message: Message) = println(message)
}

View File

@ -193,4 +193,22 @@ internal class TestCommand {
}
}
}
@Test
fun `test simple command`() {
runBlocking {
val simple = object : SimpleCommand(owner, "test") {
@Handler
fun onCommand(string: String) {
Testing.ok(string)
}
}
simple.withRegistration {
assertEquals("xxx", withTesting { simple.execute(sender, "xxx") })
assertEquals("xxx", withTesting { sender.executeCommand("/test xxx") })
}
}
}
}