From 7a6e00d4d29f5c5a85e69ab1f99dbd3dfbf77254 Mon Sep 17 00:00:00 2001 From: RTAkland Date: Thu, 26 Sep 2024 05:44:47 +0800 Subject: [PATCH] chore: use kotlin's reflection --- .../kotlin/cn/rtast/fancybot/commands/HelpCommand.kt | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/main/kotlin/cn/rtast/fancybot/commands/HelpCommand.kt b/src/main/kotlin/cn/rtast/fancybot/commands/HelpCommand.kt index a7ebbf1..6c32b42 100644 --- a/src/main/kotlin/cn/rtast/fancybot/commands/HelpCommand.kt +++ b/src/main/kotlin/cn/rtast/fancybot/commands/HelpCommand.kt @@ -13,6 +13,8 @@ import cn.rtast.rob.entity.GroupMessage import cn.rtast.rob.util.BaseCommand import cn.rtast.rob.util.ob.MessageChain import cn.rtast.rob.util.ob.OneBotListener +import kotlin.reflect.full.findAnnotation +import kotlin.reflect.full.hasAnnotation @CommandDescription("帮助") class HelpCommand : BaseCommand() { @@ -21,12 +23,8 @@ class HelpCommand : BaseCommand() { override suspend fun executeGroup(listener: OneBotListener, message: GroupMessage, args: List) { val msg = MessageChain.Builder() commands.sortedBy { it::class.simpleName }.forEach { - val description = - if (!it.javaClass.isAnnotationPresent(CommandDescription::class.java)) { - "暂无描述" - } else { - it.javaClass.getAnnotation(CommandDescription::class.java)?.description!! - } + val description = if (!it::class.hasAnnotation()) "暂无描述" + else it::class.findAnnotation()?.description!! val commandName = it::class.simpleName?.replace("Command", "")!! val commandNames = it.commandNames.joinToString(",") msg.addText("[$commandName] [$description] 命令: $commandNames")