mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-27 00:30:17 +08:00
Fix checkSubCommand
This commit is contained in:
parent
6b23d5ba27
commit
108e425082
@ -41,8 +41,8 @@ abstract class SimpleCommand @JvmOverloads constructor(
|
|||||||
|
|
||||||
final override val context: CommandParserContext = CommandParserContext.Builtins + overrideContext
|
final override val context: CommandParserContext = CommandParserContext.Builtins + overrideContext
|
||||||
|
|
||||||
override fun checkSubCommand() {
|
override fun checkSubCommand(subCommands: Array<SubCommandDescriptor>) {
|
||||||
super.checkSubCommand()
|
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." }
|
check(subCommands.size == 1) { "There can only be exactly one function annotated with Handler at this moment as overloading is not yet supported." }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -55,7 +55,6 @@ object FloatArgParser : CommandArgParser<Float> {
|
|||||||
|
|
||||||
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")
|
|
||||||
return raw
|
return raw
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -76,16 +75,8 @@ object BooleanArgParser : CommandArgParser<Boolean> {
|
|||||||
*/
|
*/
|
||||||
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 = raw.toLongOrNull() ?: illegalArgument("无法识别 QQ ID: $raw")
|
||||||
raw.toLong()
|
return Bot.getInstanceOrNull(uin) ?: illegalArgument("无法找到 Bot $uin")
|
||||||
} catch (e: Exception) {
|
|
||||||
illegalArgument("无法识别QQ UIN$raw")
|
|
||||||
}
|
|
||||||
return try {
|
|
||||||
Bot.getInstance(uin)
|
|
||||||
} catch (e: NoSuchElementException) {
|
|
||||||
illegalArgument("无法找到Bot $uin")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -102,20 +93,12 @@ object ExistFriendArgParser : CommandArgParser<Friend> {
|
|||||||
is UserCommandSender -> sender.user.id
|
is UserCommandSender -> sender.user.id
|
||||||
else -> illegalArgument("无法解析~作为默认")
|
else -> illegalArgument("无法解析~作为默认")
|
||||||
}
|
}
|
||||||
return try {
|
|
||||||
sender.bot.friends[targetID]
|
return sender.bot.friends.getOrNull(targetID) ?: illegalArgument("无法解析~作为默认")
|
||||||
} catch (e: NoSuchElementException) {
|
|
||||||
illegalArgument("无法解析~作为默认")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (sender is BotAwareCommandSender) {
|
if (sender is BotAwareCommandSender) {
|
||||||
return try {
|
return sender.bot.friends.getOrNull(raw.toLongOrNull() ?: illegalArgument("无法解析 $raw 为整数"))
|
||||||
sender.bot.friends[raw.toLong()]
|
?: illegalArgument("无法找到" + raw + "这个好友")
|
||||||
} catch (e: NoSuchElementException) {
|
|
||||||
illegalArgument("无法找到" + raw + "这个好友")
|
|
||||||
} catch (e: NumberFormatException) {
|
|
||||||
illegalArgument("无法解析$raw")
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
raw.split(".").let { args ->
|
raw.split(".").let { args ->
|
||||||
if (args.size != 2) {
|
if (args.size != 2) {
|
||||||
|
@ -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)
|
createSubCommand(function, context)
|
||||||
}.toTypedArray().also {
|
}.toTypedArray().also {
|
||||||
_usage = it.firstOrNull()?.usage ?: description
|
_usage = it.firstOrNull()?.usage ?: description
|
||||||
}.also { checkSubCommand() }
|
}.also { checkSubCommand(it) }
|
||||||
}
|
}
|
||||||
|
|
||||||
internal val bakedCommandNameToSubDescriptorArray: Map<Array<String>, SubCommandDescriptor> by lazy {
|
internal val bakedCommandNameToSubDescriptorArray: Map<Array<String>, SubCommandDescriptor> by lazy {
|
||||||
@ -252,7 +252,7 @@ internal fun AbstractReflectionCommand.createSubCommand(
|
|||||||
}
|
}
|
||||||
|
|
||||||
val commandName =
|
val commandName =
|
||||||
function.findAnnotation<CompositeCommand.SubCommand>()!!.value
|
subCommandAnnotationResolver.getSubCommandNames(function)
|
||||||
.let { namesFromAnnotation ->
|
.let { namesFromAnnotation ->
|
||||||
if (namesFromAnnotation.isNotEmpty()) {
|
if (namesFromAnnotation.isNotEmpty()) {
|
||||||
namesFromAnnotation
|
namesFromAnnotation
|
||||||
|
@ -15,6 +15,7 @@ import kotlinx.coroutines.suspendCancellableCoroutine
|
|||||||
import kotlinx.coroutines.withTimeout
|
import kotlinx.coroutines.withTimeout
|
||||||
import net.mamoe.mirai.Bot
|
import net.mamoe.mirai.Bot
|
||||||
import net.mamoe.mirai.console.command.ConsoleCommandSender
|
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.PluginLoader
|
||||||
import net.mamoe.mirai.console.plugin.jvm.JarPluginLoader
|
import net.mamoe.mirai.console.plugin.jvm.JarPluginLoader
|
||||||
import net.mamoe.mirai.console.setting.MemorySettingStorage
|
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.DefaultLogger
|
||||||
import net.mamoe.mirai.utils.LoginSolver
|
import net.mamoe.mirai.utils.LoginSolver
|
||||||
import net.mamoe.mirai.utils.MiraiLogger
|
import net.mamoe.mirai.utils.MiraiLogger
|
||||||
|
import net.mamoe.mirai.utils.PlatformLogger
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import kotlin.coroutines.Continuation
|
import kotlin.coroutines.Continuation
|
||||||
import kotlin.coroutines.CoroutineContext
|
import kotlin.coroutines.CoroutineContext
|
||||||
@ -35,13 +37,15 @@ fun initTestEnvironment() {
|
|||||||
MiraiConsoleInitializer.init(object : IMiraiConsole {
|
MiraiConsoleInitializer.init(object : IMiraiConsole {
|
||||||
override val rootDir: File = createTempDir()
|
override val rootDir: File = createTempDir()
|
||||||
override val frontEnd: MiraiConsoleFrontEnd = object : MiraiConsoleFrontEnd {
|
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 fun pushBot(bot: Bot) = println("pushBot: $bot")
|
||||||
override suspend fun requestInput(hint: String): String = readLine()!!
|
override suspend fun requestInput(hint: String): String = readLine()!!
|
||||||
override fun createLoginSolver(): LoginSolver = LoginSolver.Default
|
override fun createLoginSolver(): LoginSolver = LoginSolver.Default
|
||||||
}
|
}
|
||||||
override val mainLogger: MiraiLogger = DefaultLogger("main")
|
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 val consoleCommandSender: ConsoleCommandSender = object : ConsoleCommandSender() {
|
||||||
override suspend fun sendMessage(message: Message) = println(message)
|
override suspend fun sendMessage(message: Message) = println(message)
|
||||||
}
|
}
|
||||||
|
@ -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") })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user