CompositeCommand

abstract class CompositeCommand(owner: CommandOwner, primaryName: String, secondaryNames: String, description: String, parentPermission: Permission, overrideContext: CommandArgumentContext) : AbstractCommand, Command, CommandArgumentContextAware

复合指令. 指令注册时候会通过反射构造指令解析器.

Java 示例查看 JCompositeCommand.

Kotlin 示例:

@OptIn(ConsoleExperimentalAPI::class)
object MyCompositeCommand : CompositeCommand(
MyPluginMain, "manage", // "manage" 是主指令名
description = "示例指令", permission = MyCustomPermission,
// prefixOptional = true // 还有更多参数可填, 此处忽略
) {

// [参数智能解析]
//
// 在控制台执行 "/manage <群号>.<群员> <持续时间>",
// 或在聊天群内发送 "/manage <@一个群员> <持续时间>",
// 或在聊天群内发送 "/manage <目标群员的群名> <持续时间>",
// 或在聊天群内发送 "/manage <目标群员的账号> <持续时间>"
// 时调用这个函数
@SubCommand // 表示这是一个子指令,使用函数名作为子指令名称
suspend fun CommandSender.mute(target: Member, duration: Int) { // 通过 /manage mute <target> <duration> 调用
sendMessage("/manage mute 被调用了, 参数为: $target, $duration")

val result = kotlin.runCatching {
target.mute(duration).toString()
}.getOrElse {
it.stackTraceToString()
} // 失败时返回堆栈信息

sendMessage("结果: $result")
}

@SubCommand
suspend fun ConsoleCommandSender.foo() {
// 使用 ConsoleCommandSender 作为接收者,表示指令只能由控制台执行。
// 当用户尝试在聊天环境执行时将会收到错误提示。
}

@SubCommand("list", "查看列表") // 可以设置多个子指令名。此时函数名会被忽略。
suspend fun CommandSender.list() { // 执行 "/manage list" 时调用这个函数
sendMessage("/manage list 被调用了")
}

// 支持 Image 类型, 需在聊天中执行此指令.
@SubCommand
suspend fun UserCommandSender.test(image: Image) { // 执行 "/manage test <一张图片>" 时调用这个函数
// 由于 Image 类型消息只可能在聊天环境,可以直接使用 UserCommandSender。
sendMessage("/manage image 被调用了, 图片是 ${image.imageId}")
}
}

See also

Constructors

CompositeCommand
Link copied to clipboard
fun CompositeCommand(owner: CommandOwner, primaryName: String, vararg secondaryNames: String, description: String = "no description available", parentPermission: Permission = owner.parentPermission, overrideContext: CommandArgumentContext = EmptyCommandArgumentContext)

Properties

context
Link copied to clipboard
override val context: CommandArgumentContext
description
Link copied to clipboard
abstract val description: String

描述, 用于显示在 BuiltInCommands.HelpCommand

overloads
Link copied to clipboard

指令可能的参数列表.

owner
Link copied to clipboard
abstract val owner: CommandOwner

指令拥有者.

permission
Link copied to clipboard
abstract val permission: Permission

为此指令分配的权限.

prefixOptional
Link copied to clipboard

true 时表示 指令前缀 可选.

primaryName
Link copied to clipboard
abstract val primaryName: String

主指令名. 将会参与构成 Permission.id.

secondaryNames
Link copied to clipboard
abstract val secondaryNames: Array<out String>

次要指令名

usage
Link copied to clipboard
open override val usage: String

自动根据带有 SubCommand 注解的函数签名生成 usage. 也可以被覆盖.

Inheritors

PermissionCommand
Link copied to clipboard
AutoLoginCommand
Link copied to clipboard
JCompositeCommand
Link copied to clipboard