mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-10 18:40:15 +08:00
Fix flatten
This commit is contained in:
parent
ee8be9799f
commit
3eb5a0a4cf
@ -68,7 +68,7 @@ class CommandDescriptor(
|
||||
/**
|
||||
* `fullName + aliases`
|
||||
*/
|
||||
val allNames = arrayOf(fullName, *aliases)
|
||||
val allNames: Array<CommandFullName> = arrayOf(fullName, *aliases)
|
||||
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (this === other) return true
|
||||
@ -117,7 +117,7 @@ internal fun Any.flattenCommandComponents(): Sequence<Any> = when (this) {
|
||||
is String -> splitToSequence(' ').filterNot { it.isBlank() }
|
||||
is PlainText -> content.flattenCommandComponents()
|
||||
is SingleMessage -> sequenceOf(this)
|
||||
is MessageChain -> this.asSequence().map { it.flattenCommandComponents() }
|
||||
is MessageChain -> this.asSequence().flatMap { it.flattenCommandComponents() }
|
||||
else -> throw IllegalArgumentException("Illegal component: $this")
|
||||
}
|
||||
|
||||
@ -134,11 +134,14 @@ internal fun CommandFullName.checkFullName(errorHint: String): CommandFullName {
|
||||
inline fun CommandDescriptor(
|
||||
vararg fullName: Any,
|
||||
block: CommandDescriptorBuilder.() -> Unit = {}
|
||||
): CommandDescriptor = CommandDescriptorBuilder(fullName).apply(block).build()
|
||||
): CommandDescriptor = CommandDescriptorBuilder(*fullName).apply(block).build()
|
||||
|
||||
class CommandDescriptorBuilder(
|
||||
vararg val fullName: Any
|
||||
vararg fullName: Any
|
||||
) {
|
||||
@PublishedApi
|
||||
internal var fullName: CommandFullName = fullName.checkFullName("fullName")
|
||||
|
||||
@PublishedApi
|
||||
internal var context: CommandParserContext = CommandParserContext.Builtins
|
||||
|
||||
|
@ -25,16 +25,14 @@ fun CommandOwner.unregisterAllCommands() {
|
||||
* 注册一个指令. 若此指令已经注册或有已经注册的指令与 [allNames] 重名, 返回 `false`
|
||||
*/
|
||||
fun Command.register(): Boolean = InternalCommandManager.modifyLock.withLock {
|
||||
with(descriptor) {
|
||||
if (findDuplicate() != null) {
|
||||
return false
|
||||
}
|
||||
InternalCommandManager.registeredCommands.add(this@register)
|
||||
for (name in this.allNames) {
|
||||
InternalCommandManager.nameToCommandMap[name] = this@register
|
||||
}
|
||||
return true
|
||||
if (findDuplicate() != null) {
|
||||
return false
|
||||
}
|
||||
InternalCommandManager.registeredCommands.add(this@register)
|
||||
for (name in this.allNames) {
|
||||
InternalCommandManager.nameToCommandMap[name] = this@register
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
/**
|
||||
@ -85,8 +83,7 @@ suspend fun CommandSender.execute(command: Command, args: CommandArgs): Boolean
|
||||
}
|
||||
|
||||
suspend fun Command.execute(sender: CommandSender, args: CommandArgs): Boolean = sender.execute(this, args)
|
||||
suspend fun Command.execute(sender: CommandSender, vararg args: Any): Boolean = sender.execute(this, args)
|
||||
suspend fun CommandSender.execute(command: Command, vararg args: Any): Boolean = command.execute(this, args)
|
||||
suspend fun CommandSender.execute(vararg args: Any): Boolean = args.toList().executeCommand(this)
|
||||
|
||||
|
||||
internal suspend fun List<Any>.executeCommand(sender: CommandSender): Boolean {
|
||||
|
@ -10,24 +10,14 @@
|
||||
package net.mamoe.mirai.console.command
|
||||
|
||||
suspend fun main() {
|
||||
ConsoleCommandSender.execute(DefaultCommands.Test, "test")
|
||||
}
|
||||
|
||||
internal object DefaultCommands {
|
||||
|
||||
object Test : ConsoleCommand(
|
||||
CommandDescriptor("test") {
|
||||
param<String>()
|
||||
}
|
||||
) {
|
||||
override suspend fun onCommand(sender: CommandSender, args: CommandArgs): Boolean {
|
||||
val s = args.getReified<String>()
|
||||
sender.sendMessage(s)
|
||||
return true
|
||||
}
|
||||
if (ConsoleCommandSender.execute("test")) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
internal object DefaultCommands
|
||||
|
||||
/*
|
||||
|
||||
/**
|
||||
|
@ -0,0 +1,75 @@
|
||||
/*
|
||||
* Copyright 2020 Mamoe Technologies and contributors.
|
||||
*
|
||||
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
|
||||
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
|
||||
*
|
||||
* https://github.com/mamoe/mirai/blob/master/LICENSE
|
||||
*/
|
||||
|
||||
@file:Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
|
||||
|
||||
package net.mamoe.mirai.console.command
|
||||
|
||||
import net.mamoe.mirai.console.plugins.PluginBase
|
||||
import net.mamoe.mirai.message.data.PlainText
|
||||
import net.mamoe.mirai.message.data.messageChainOf
|
||||
import net.mamoe.mirai.message.data.toMessage
|
||||
import org.junit.jupiter.api.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertFalse
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
|
||||
val plugin: PluginBase = object : PluginBase() {
|
||||
|
||||
}
|
||||
|
||||
internal object TestCommand : PluginCommand(
|
||||
plugin,
|
||||
CommandDescriptor("test") {
|
||||
param<String>()
|
||||
}
|
||||
) {
|
||||
override suspend fun onCommand(sender: CommandSender, args: CommandArgs): Boolean {
|
||||
val s = args.getReified<String>()
|
||||
sender.sendMessage(s)
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
internal class TestCommands {
|
||||
@Test
|
||||
fun testFlatten() {
|
||||
assertEquals(listOf("test", "v1"), "test v1".flattenCommandComponents().toList())
|
||||
assertEquals(listOf("test", "v1"), PlainText("test v1").flattenCommandComponents().toList())
|
||||
assertEquals(listOf("test", "v1"), arrayOf("test ", "v1", " ").flattenCommandComponents().toList())
|
||||
assertEquals(
|
||||
listOf("test", "v1"),
|
||||
messageChainOf("test v1".toMessage(), " ".toMessage()).flattenCommandComponents().toList()
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testRegister() {
|
||||
assertTrue(TestCommand.register())
|
||||
assertEquals(listOf("test"), TestCommand.allNames.single().toList())
|
||||
|
||||
assertFalse(TestCommand.register())
|
||||
assertFalse(
|
||||
object : PluginCommand(
|
||||
plugin,
|
||||
CommandDescriptor("test") {
|
||||
param<String>()
|
||||
}
|
||||
) {
|
||||
override suspend fun onCommand(sender: CommandSender, args: CommandArgs): Boolean {
|
||||
val s = args.getReified<String>()
|
||||
sender.sendMessage(s)
|
||||
return true
|
||||
}
|
||||
}.register()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user