chore: use kotlin's reflection
This commit is contained in:
1 file changed
+4
-6
@@ -13,6 +13,8 @@ import cn.rtast.rob.entity.GroupMessage
|
|||||||
import cn.rtast.rob.util.BaseCommand
|
import cn.rtast.rob.util.BaseCommand
|
||||||
import cn.rtast.rob.util.ob.MessageChain
|
import cn.rtast.rob.util.ob.MessageChain
|
||||||
import cn.rtast.rob.util.ob.OneBotListener
|
import cn.rtast.rob.util.ob.OneBotListener
|
||||||
|
import kotlin.reflect.full.findAnnotation
|
||||||
|
import kotlin.reflect.full.hasAnnotation
|
||||||
|
|
||||||
@CommandDescription("帮助")
|
@CommandDescription("帮助")
|
||||||
class HelpCommand : BaseCommand() {
|
class HelpCommand : BaseCommand() {
|
||||||
@@ -21,12 +23,8 @@ class HelpCommand : BaseCommand() {
|
|||||||
override suspend fun executeGroup(listener: OneBotListener, message: GroupMessage, args: List<String>) {
|
override suspend fun executeGroup(listener: OneBotListener, message: GroupMessage, args: List<String>) {
|
||||||
val msg = MessageChain.Builder()
|
val msg = MessageChain.Builder()
|
||||||
commands.sortedBy { it::class.simpleName }.forEach {
|
commands.sortedBy { it::class.simpleName }.forEach {
|
||||||
val description =
|
val description = if (!it::class.hasAnnotation<CommandDescription>()) "暂无描述"
|
||||||
if (!it.javaClass.isAnnotationPresent(CommandDescription::class.java)) {
|
else it::class.findAnnotation<CommandDescription>()?.description!!
|
||||||
"暂无描述"
|
|
||||||
} else {
|
|
||||||
it.javaClass.getAnnotation(CommandDescription::class.java)?.description!!
|
|
||||||
}
|
|
||||||
val commandName = it::class.simpleName?.replace("Command", "")!!
|
val commandName = it::class.simpleName?.replace("Command", "")!!
|
||||||
val commandNames = it.commandNames.joinToString(",")
|
val commandNames = it.commandNames.joinToString(",")
|
||||||
msg.addText("[$commandName] [$description] 命令: $commandNames")
|
msg.addText("[$commandName] [$description] 命令: $commandNames")
|
||||||
|
|||||||
Reference in New Issue
Block a user