mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-10 18:40:15 +08:00
Introduce default type variants
This commit is contained in:
parent
58af1b3354
commit
3e2a5c382e
@ -211,6 +211,7 @@ public suspend fun Command.execute(
|
||||
): CommandExecuteResult {
|
||||
// TODO: 2020/10/18 net.mamoe.mirai.console.command.CommandManager.execute
|
||||
val chain = buildMessageChain {
|
||||
append(CommandManager.commandPrefix)
|
||||
append(this@execute.primaryName)
|
||||
append(' ')
|
||||
append(arguments)
|
||||
|
@ -12,7 +12,10 @@ package net.mamoe.mirai.console.command.descriptor
|
||||
import net.mamoe.mirai.console.command.parse.CommandCall
|
||||
import net.mamoe.mirai.console.command.parse.CommandCallParser
|
||||
import net.mamoe.mirai.console.command.parse.RawCommandArgument
|
||||
import net.mamoe.mirai.message.data.MessageChain
|
||||
import net.mamoe.mirai.message.data.MessageContent
|
||||
import net.mamoe.mirai.message.data.asMessageChain
|
||||
import net.mamoe.mirai.message.data.content
|
||||
import kotlin.reflect.KType
|
||||
import kotlin.reflect.typeOf
|
||||
|
||||
@ -45,6 +48,20 @@ public interface TypeVariant<out OutType> {
|
||||
@ExperimentalCommandDescriptors
|
||||
public object MessageContentTypeVariant : TypeVariant<MessageContent> {
|
||||
@OptIn(ExperimentalStdlibApi::class)
|
||||
override val outType: KType = typeOf<String>()
|
||||
override val outType: KType = typeOf<MessageContent>()
|
||||
override fun mapValue(valueParameter: MessageContent): MessageContent = valueParameter
|
||||
}
|
||||
|
||||
@ExperimentalCommandDescriptors
|
||||
public object MessageChainTypeVariant : TypeVariant<MessageChain> {
|
||||
@OptIn(ExperimentalStdlibApi::class)
|
||||
override val outType: KType = typeOf<MessageChain>()
|
||||
override fun mapValue(valueParameter: MessageContent): MessageChain = valueParameter.asMessageChain()
|
||||
}
|
||||
|
||||
@ExperimentalCommandDescriptors
|
||||
public object ContentStringTypeVariant : TypeVariant<String> {
|
||||
@OptIn(ExperimentalStdlibApi::class)
|
||||
override val outType: KType = typeOf<String>()
|
||||
override fun mapValue(valueParameter: MessageContent): String = valueParameter.content
|
||||
}
|
||||
|
@ -9,10 +9,8 @@
|
||||
|
||||
package net.mamoe.mirai.console.command.parse
|
||||
|
||||
import net.mamoe.mirai.console.command.descriptor.ExperimentalCommandDescriptors
|
||||
import net.mamoe.mirai.console.command.descriptor.MessageContentTypeVariant
|
||||
import net.mamoe.mirai.console.command.descriptor.NoValueArgumentMappingException
|
||||
import net.mamoe.mirai.console.command.descriptor.TypeVariant
|
||||
import net.mamoe.mirai.console.command.descriptor.*
|
||||
import net.mamoe.mirai.console.util.ConsoleExperimentalApi
|
||||
import net.mamoe.mirai.message.data.MessageContent
|
||||
import kotlin.reflect.KType
|
||||
import kotlin.reflect.full.isSubtypeOf
|
||||
@ -31,7 +29,7 @@ public typealias RawCommandArgument = MessageContent
|
||||
public interface CommandArgument
|
||||
|
||||
/**
|
||||
* @see InvariantCommandValueArgument
|
||||
* @see DefaultCommandValueArgument
|
||||
*/
|
||||
@ExperimentalCommandDescriptors
|
||||
public interface CommandValueArgument : CommandArgument {
|
||||
@ -43,13 +41,18 @@ public interface CommandValueArgument : CommandArgument {
|
||||
/**
|
||||
* The [CommandValueArgument] that doesn't vary in type (remaining [MessageContent]).
|
||||
*/
|
||||
@ConsoleExperimentalApi
|
||||
@ExperimentalCommandDescriptors
|
||||
public data class InvariantCommandValueArgument(
|
||||
public data class DefaultCommandValueArgument(
|
||||
public override val value: RawCommandArgument,
|
||||
) : CommandValueArgument {
|
||||
@OptIn(ExperimentalStdlibApi::class)
|
||||
override val type: KType = typeOf<MessageContent>()
|
||||
override val typeVariants: List<TypeVariant<*>> = listOf(MessageContentTypeVariant)
|
||||
override val typeVariants: List<TypeVariant<*>> = listOf(
|
||||
MessageContentTypeVariant,
|
||||
MessageChainTypeVariant,
|
||||
ContentStringTypeVariant,
|
||||
)
|
||||
}
|
||||
|
||||
@ExperimentalCommandDescriptors
|
||||
|
@ -18,7 +18,7 @@ public object SpaceSeparatedCommandCallParser : CommandCallParser {
|
||||
return CommandCallImpl(
|
||||
caller = caller,
|
||||
calleeName = flatten.first().content,
|
||||
valueArguments = flatten.drop(1).map(::InvariantCommandValueArgument)
|
||||
valueArguments = flatten.drop(1).map(::DefaultCommandValueArgument)
|
||||
)
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user