Files
FancyBot/src/main/kotlin/cn/rtast/fancybot/commands/HelpCommand.kt
T

59 lines
2.4 KiB
Kotlin
Raw Normal View History

2024-09-03 16:15:26 +08:00
/*
* Copyright © 2024 RTAkland
* Author: RTAkland
* Date: 2024/9/3
*/
package cn.rtast.fancybot.commands
import cn.rtast.fancybot.annotations.CommandDescription
2024-09-03 16:15:26 +08:00
import cn.rtast.fancybot.commands
import cn.rtast.rob.entity.GroupMessage
import cn.rtast.rob.util.BaseCommand
import cn.rtast.rob.util.ob.MessageChain
2024-09-26 14:01:26 +08:00
import cn.rtast.rob.util.ob.NodeMessageChain
import cn.rtast.rob.util.ob.OneBotListener
2024-09-26 05:44:47 +08:00
import kotlin.reflect.full.findAnnotation
import kotlin.reflect.full.hasAnnotation
2024-09-03 16:15:26 +08:00
2024-09-26 05:21:42 +08:00
@CommandDescription("帮助")
2024-09-03 16:15:26 +08:00
class HelpCommand : BaseCommand() {
override val commandNames = listOf("/help", "/帮助")
override suspend fun executeGroup(listener: OneBotListener, message: GroupMessage, args: List<String>) {
if (args.isEmpty()) {
val node = NodeMessageChain.Builder()
val msg = MessageChain.Builder()
commands.sortedBy { it::class.simpleName }.forEach {
val description = if (!it::class.hasAnnotation<CommandDescription>()) "暂无描述"
else it::class.findAnnotation<CommandDescription>()?.description!!
val commandName = it::class.simpleName?.replace("Command", "")!!
val commandNames = it.commandNames.joinToString(",")
msg.addText("[$commandName] [$description] 命令: $commandNames")
.addNewLine()
}
msg.addText("共计${commands.size}条命令")
node.addMessageChain(msg.build(), message.sender.userId)
message.reply(node.build())
} else {
val commandName = args.first().replace("/", "")
val matchedCommand = commands.find {
it.commandNames.map { map -> map.replace("/", "") }.contains(commandName)
}
if (matchedCommand == null) {
message.reply("未查询到该命令")
return
}
val description = matchedCommand::class.findAnnotation<CommandDescription>()?.description ?: "暂无描述"
val msg = MessageChain.Builder()
.addText("命令名称: ${matchedCommand::class.simpleName}")
.addNewLine()
.addText("指令别名: ${matchedCommand.commandNames.joinToString(",")}")
.addNewLine()
.addText("指令描述: $description")
.build()
message.reply(msg)
}
2024-09-03 16:15:26 +08:00
}
}