From 34426e59754fc276fcb6468c15ec0ad30df80444 Mon Sep 17 00:00:00 2001 From: Project_IO Date: Thu, 19 Sep 2024 22:31:06 +0900 Subject: [PATCH] fix: command register function fixed --- .../p/x32/api/command/CommandHandler.kt | 22 ++++-------------- .../main/kotlin/net/projecttl/p/x32/Px32.kt | 9 +++++--- .../net/projecttl/p/x32/command/Reload.kt | 23 +++++++++++++++++++ .../net/projecttl/p/x32/kernel/CoreKernel.kt | 20 +++++++++++++++- .../projecttl/p/x32/kernel/PluginLoader.kt | 10 ++++---- .../kotlin/net/projecttl/p/x32/func/Loader.kt | 2 ++ .../projecttl/p/x32/func/command/MsgLength.kt | 15 ++++++++++++ .../projecttl/p/x32/func}/handler/Ready.kt | 5 ++-- 8 files changed, 76 insertions(+), 30 deletions(-) create mode 100644 px32-bot-core/src/main/kotlin/net/projecttl/p/x32/command/Reload.kt create mode 100644 px32-bot-func/src/main/kotlin/net/projecttl/p/x32/func/command/MsgLength.kt rename {px32-bot-core/src/main/kotlin/net/projecttl/p/x32 => px32-bot-func/src/main/kotlin/net/projecttl/p/x32/func}/handler/Ready.kt (59%) diff --git a/px32-bot-api/src/main/kotlin/net/projecttl/p/x32/api/command/CommandHandler.kt b/px32-bot-api/src/main/kotlin/net/projecttl/p/x32/api/command/CommandHandler.kt index 39480b5..28c949b 100644 --- a/px32-bot-api/src/main/kotlin/net/projecttl/p/x32/api/command/CommandHandler.kt +++ b/px32-bot-api/src/main/kotlin/net/projecttl/p/x32/api/command/CommandHandler.kt @@ -92,35 +92,21 @@ class CommandHandler(val guildId: Long = 0L) : ListenerAdapter() { if (command is UserContext) { if (guild == null) { - jda.updateCommands().addCommands( - Commands.context(Command.Type.USER, data.name), - Commands.message(data.name) - ).queue() + jda.upsertCommand(Commands.user(data.name)).queue() println("Register User Context Command: /${data.name}") } else { - guild.updateCommands().addCommands( - Commands.context(Command.Type.USER, data.name), - Commands.message(data.name) - ).queue() + guild.upsertCommand(Commands.user(data.name)).queue() println("Register '${guild.id}' Guild's User Context Command: /${data.name}") } } if (command is MessageContext) { if (guild == null) { - jda.updateCommands().addCommands( - Commands.context(Command.Type.MESSAGE, data.name), - Commands.message(data.name) - ) - + jda.upsertCommand(Commands.message(data.name)) println("Register Message Context Command: /${data.name}") } else { - guild.updateCommands().addCommands( - Commands.context(Command.Type.MESSAGE, data.name), - Commands.message(data.name) - ) - + guild.upsertCommand(Commands.message(data.name)) println("Register '${guild.id}' Guild's Message Context Command: /${data.name}") } } diff --git a/px32-bot-core/src/main/kotlin/net/projecttl/p/x32/Px32.kt b/px32-bot-core/src/main/kotlin/net/projecttl/p/x32/Px32.kt index a0518a3..9a1c145 100644 --- a/px32-bot-core/src/main/kotlin/net/projecttl/p/x32/Px32.kt +++ b/px32-bot-core/src/main/kotlin/net/projecttl/p/x32/Px32.kt @@ -1,22 +1,25 @@ package net.projecttl.p.x32 import net.dv8tion.jda.api.JDA +import net.projecttl.p.x32.command.Reload import net.projecttl.p.x32.config.DefaultConfig import net.projecttl.p.x32.func.loadDefault -import net.projecttl.p.x32.handler.Ready +import net.projecttl.p.x32.func.handler.Ready import net.projecttl.p.x32.kernel.CoreKernel import org.slf4j.Logger import org.slf4j.LoggerFactory lateinit var jda: JDA +lateinit var kernel: CoreKernel val logger: Logger = LoggerFactory.getLogger(Px32::class.java) fun main() { println("Px32 version v${DefaultConfig.version}") - val kernel = CoreKernel(System.getenv("TOKEN")) + kernel = CoreKernel(System.getenv("TOKEN")) + val handler = kernel.getGlobalCommandHandler() kernel.addHandler(Ready) - val handler = kernel.getGlobalCommandHandler() + handler.addCommand(Reload) loadDefault(handler) jda = kernel.build() diff --git a/px32-bot-core/src/main/kotlin/net/projecttl/p/x32/command/Reload.kt b/px32-bot-core/src/main/kotlin/net/projecttl/p/x32/command/Reload.kt new file mode 100644 index 0000000..0ce7d20 --- /dev/null +++ b/px32-bot-core/src/main/kotlin/net/projecttl/p/x32/command/Reload.kt @@ -0,0 +1,23 @@ +package net.projecttl.p.x32.command + +import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent +import net.dv8tion.jda.api.interactions.commands.build.CommandData +import net.dv8tion.jda.internal.interactions.CommandDataImpl +import net.projecttl.p.x32.api.command.GlobalCommand +import net.projecttl.p.x32.kernel + +object Reload : GlobalCommand { + override val data = CommandData.fromData(CommandDataImpl("reload", "플러그인을 다시 불러 옵니다").toData()) + + override suspend fun execute(ev: SlashCommandInteractionEvent) { + try { + kernel.reload() + } catch (ex: Exception) { + ex.printStackTrace() + ev.reply(":warning: 플러그인을 다시 불러오는 도중에 오류가 발생 했어요. 자세한 내용은 콘솔을 확인해 주세요!").queue() + return + } + + ev.reply(":white_check_mark: 플러그인을 다시 불러 왔어요!\n불러온 플러그인 수: ${kernel.plugins().size}").queue() + } +} \ No newline at end of file diff --git a/px32-bot-core/src/main/kotlin/net/projecttl/p/x32/kernel/CoreKernel.kt b/px32-bot-core/src/main/kotlin/net/projecttl/p/x32/kernel/CoreKernel.kt index de16fee..c3b3d5e 100644 --- a/px32-bot-core/src/main/kotlin/net/projecttl/p/x32/kernel/CoreKernel.kt +++ b/px32-bot-core/src/main/kotlin/net/projecttl/p/x32/kernel/CoreKernel.kt @@ -3,6 +3,7 @@ package net.projecttl.p.x32.kernel import net.dv8tion.jda.api.JDA import net.dv8tion.jda.api.JDABuilder import net.dv8tion.jda.api.hooks.ListenerAdapter +import net.projecttl.p.x32.api.Plugin import net.projecttl.p.x32.api.command.CommandHandler import net.projecttl.p.x32.logger @@ -23,6 +24,10 @@ class CoreKernel(token: String) { handlers.remove(handler) } + fun plugins(): List { + return PluginLoader.getPlugins().map { it.value } + } + fun build(): JDA { handlers.map { builder.addEventListeners(it) @@ -54,4 +59,17 @@ class CoreKernel(token: String) { return jda } -} \ No newline at end of file + + fun reload() { + val plugins = PluginLoader.getPlugins() + plugins.forEach { (c, p) -> + logger.info("Reload plugin ${c.name} v${c.version}") + p.destroy() + } + + PluginLoader.load() + plugins.forEach { (_, p) -> + p.onLoad() + } + } +} diff --git a/px32-bot-core/src/main/kotlin/net/projecttl/p/x32/kernel/PluginLoader.kt b/px32-bot-core/src/main/kotlin/net/projecttl/p/x32/kernel/PluginLoader.kt index 7d79648..78d4d93 100644 --- a/px32-bot-core/src/main/kotlin/net/projecttl/p/x32/kernel/PluginLoader.kt +++ b/px32-bot-core/src/main/kotlin/net/projecttl/p/x32/kernel/PluginLoader.kt @@ -17,6 +17,10 @@ object PluginLoader { } } + fun getPlugins(): Map { + return plugins.toMap() + } + fun load() { parentDir.listFiles()?.forEach { file -> if (file.name.endsWith(".jar")) { @@ -45,10 +49,6 @@ object PluginLoader { } } - fun getPlugins(): Map { - return plugins.toMap() - } - fun destroy() { plugins.forEach { (config, plugin) -> logger.info("disable ${config.name} plugin...") @@ -61,4 +61,4 @@ object PluginLoader { } } } -} \ No newline at end of file +} diff --git a/px32-bot-func/src/main/kotlin/net/projecttl/p/x32/func/Loader.kt b/px32-bot-func/src/main/kotlin/net/projecttl/p/x32/func/Loader.kt index 96fc751..fcce0bd 100644 --- a/px32-bot-func/src/main/kotlin/net/projecttl/p/x32/func/Loader.kt +++ b/px32-bot-func/src/main/kotlin/net/projecttl/p/x32/func/Loader.kt @@ -2,9 +2,11 @@ package net.projecttl.p.x32.func import net.projecttl.p.x32.api.command.CommandHandler import net.projecttl.p.x32.func.command.Avatar +import net.projecttl.p.x32.func.command.MsgLength import net.projecttl.p.x32.func.command.Ping fun loadDefault(handler: CommandHandler) = with(handler) { addCommand(Avatar) + addCommand(MsgLength) addCommand(Ping) } diff --git a/px32-bot-func/src/main/kotlin/net/projecttl/p/x32/func/command/MsgLength.kt b/px32-bot-func/src/main/kotlin/net/projecttl/p/x32/func/command/MsgLength.kt new file mode 100644 index 0000000..df29f63 --- /dev/null +++ b/px32-bot-func/src/main/kotlin/net/projecttl/p/x32/func/command/MsgLength.kt @@ -0,0 +1,15 @@ +package net.projecttl.p.x32.func.command + +import net.dv8tion.jda.api.events.interaction.command.MessageContextInteractionEvent +import net.dv8tion.jda.api.interactions.commands.build.CommandData +import net.dv8tion.jda.internal.interactions.CommandDataImpl +import net.projecttl.p.x32.api.command.MessageContext + +object MsgLength : MessageContext { + override val data = CommandData.fromData(CommandDataImpl("length", "메시지의 길이를 확인 합니다.").toData()) + + override suspend fun execute(ev: MessageContextInteractionEvent) { + val target = ev.target + ev.reply("${target.jumpUrl} 메시지의 길이:\n\t${target.contentRaw.split("\\s+").size}").queue() + } +} diff --git a/px32-bot-core/src/main/kotlin/net/projecttl/p/x32/handler/Ready.kt b/px32-bot-func/src/main/kotlin/net/projecttl/p/x32/func/handler/Ready.kt similarity index 59% rename from px32-bot-core/src/main/kotlin/net/projecttl/p/x32/handler/Ready.kt rename to px32-bot-func/src/main/kotlin/net/projecttl/p/x32/func/handler/Ready.kt index 33d7699..edf8a8a 100644 --- a/px32-bot-core/src/main/kotlin/net/projecttl/p/x32/handler/Ready.kt +++ b/px32-bot-func/src/main/kotlin/net/projecttl/p/x32/func/handler/Ready.kt @@ -1,11 +1,10 @@ -package net.projecttl.p.x32.handler +package net.projecttl.p.x32.func.handler import net.dv8tion.jda.api.events.session.ReadyEvent import net.dv8tion.jda.api.hooks.ListenerAdapter -import net.projecttl.p.x32.logger object Ready : ListenerAdapter() { override fun onReady(ev: ReadyEvent) { - logger.info("Logged in as ${ev.jda.selfUser.asTag}") + println("Logged in as ${ev.jda.selfUser.asTag}") } } \ No newline at end of file