From 81c81b57d90835a27aa3ae18c3709bc2dc6f9ca1 Mon Sep 17 00:00:00 2001 From: Project_IO Date: Tue, 3 Sep 2024 20:58:09 +0900 Subject: [PATCH] feat: middle save --- build.gradle.kts | 6 ++++- gradle.properties | 1 + src/main/java/net/projecttl/p/x32/Px32.java | 13 +++++++---- .../net/projecttl/p/x32/command/Ping.java | 20 ++++++++--------- .../p/x32/handler/CommandHandler.java | 22 +++++++++++++------ .../{Command.kt => CommandExecutor.kt} | 2 +- 6 files changed, 41 insertions(+), 23 deletions(-) rename src/main/kotlin/net/projecttl/p/x32/handler/{Command.kt => CommandExecutor.kt} (89%) diff --git a/build.gradle.kts b/build.gradle.kts index ab3f554..016d403 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -10,6 +10,7 @@ plugins { group = "net.wh64" version = "1.0-SNAPSHOT" +val ktor_version: String by project val log4j_version: String by project val exposed_version: String by project @@ -30,11 +31,14 @@ dependencies { implementation(kotlin("stdlib")) implementation(kotlin("reflect")) implementation("net.dv8tion:JDA:5.1.0") + implementation("io.ktor:ktor-client-cio:$ktor_version") + implementation("io.ktor:ktor-client-core:$ktor_version") implementation("org.apache.logging.log4j:log4j-api:$log4j_version") implementation("org.apache.logging.log4j:log4j-core:$log4j_version") - implementation("org.apache.logging.log4j:log4j-slf4j2-impl:$log4j_version") implementation("org.jetbrains.exposed:exposed-core:$exposed_version") implementation("org.jetbrains.exposed:exposed-jdbc:$exposed_version") + implementation("org.apache.logging.log4j:log4j-slf4j2-impl:$log4j_version") + implementation("io.ktor:ktor-client-okhttp-jvm:2.3.12") // testImplementation(platform("org.junit:junit-bom:5.10.0")) // testImplementation("org.junit.jupiter:junit-jupiter") } diff --git a/gradle.properties b/gradle.properties index 7707aaf..b3b29dc 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,2 +1,3 @@ +ktor_version=2.3.12 log4j_version=2.23.1 exposed_version=0.54.0 diff --git a/src/main/java/net/projecttl/p/x32/Px32.java b/src/main/java/net/projecttl/p/x32/Px32.java index 7d6eb2e..555995d 100644 --- a/src/main/java/net/projecttl/p/x32/Px32.java +++ b/src/main/java/net/projecttl/p/x32/Px32.java @@ -2,8 +2,10 @@ package net.projecttl.p.x32; import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.JDABuilder; +import net.dv8tion.jda.api.interactions.commands.Command; +import net.dv8tion.jda.api.interactions.commands.build.Commands; import net.projecttl.p.x32.command.Ping; -import net.projecttl.p.x32.handler.Command; +import net.projecttl.p.x32.handler.CommandExecutor; import net.projecttl.p.x32.handler.CommandHandler; import net.projecttl.p.x32.handler.Ready; import org.slf4j.Logger; @@ -14,12 +16,15 @@ import java.util.ArrayList; public class Px32 { public static final Logger log = LoggerFactory.getLogger(Px32.class); private JDA jda; - private final ArrayList commands = new ArrayList<>(); + private final ArrayList commands = new ArrayList<>(); private void register() { commands.forEach(command -> { - jda.updateCommands().addCommands(command.getData()).queue(); - log.info("Registered command: {}", command.getData().getName()); + jda.upsertCommand(command.getData()).queue(); + jda.updateCommands().addCommands( + Commands.context(Command.Type.USER, command.getData().getName()) + ).queue(); + log.info("registered command: {}", command.getData().getName()); }); } diff --git a/src/main/java/net/projecttl/p/x32/command/Ping.java b/src/main/java/net/projecttl/p/x32/command/Ping.java index 963118b..6fe25e2 100644 --- a/src/main/java/net/projecttl/p/x32/command/Ping.java +++ b/src/main/java/net/projecttl/p/x32/command/Ping.java @@ -5,7 +5,7 @@ import net.dv8tion.jda.api.entities.MessageEmbed; 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.handler.Command; +import net.projecttl.p.x32.handler.CommandExecutor; import org.jetbrains.annotations.NotNull; import java.util.Random; @@ -13,15 +13,15 @@ import java.util.concurrent.atomic.AtomicReference; import static java.lang.String.format; -public class Ping implements Command { - @NotNull - @Override - public CommandData getData() { - return CommandData.fromData(new CommandDataImpl( - "ping", - "Discord API 레이턴시를 확인 합니다." - ).toData()); - } +public class Ping implements CommandExecutor { + @NotNull + @Override + public CommandData getData() { + return CommandData.fromData(new CommandDataImpl( + "ping", + "Discord API 레이턴시를 확인 합니다." + ).toData()); + } @Override public void execute(SlashCommandInteractionEvent ev) { diff --git a/src/main/java/net/projecttl/p/x32/handler/CommandHandler.java b/src/main/java/net/projecttl/p/x32/handler/CommandHandler.java index 00ad55f..aab83c7 100644 --- a/src/main/java/net/projecttl/p/x32/handler/CommandHandler.java +++ b/src/main/java/net/projecttl/p/x32/handler/CommandHandler.java @@ -1,16 +1,16 @@ package net.projecttl.p.x32.handler; import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; +import net.dv8tion.jda.api.events.interaction.command.UserContextInteractionEvent; import net.dv8tion.jda.api.hooks.ListenerAdapter; -import net.dv8tion.jda.api.interactions.commands.SlashCommandInteraction; import net.projecttl.p.x32.Px32; import java.util.List; public class CommandHandler extends ListenerAdapter { - private final List commands; + private final List commands; - public CommandHandler(List commands) { + public CommandHandler(List commands) { this.commands = commands; } @@ -20,15 +20,23 @@ public class CommandHandler extends ListenerAdapter { return; } - for (Command command : commands) { + for (CommandExecutor command : commands) { if (!command.getData().getName().equals(ev.getName())) { continue; } - command.execute(ev); - - Px32.log.info("user {} execute command: {}", ev.getUser().getId(), ev.getName()); + try { + command.execute(ev); + Px32.log.info("user {} execute command: {}", ev.getUser().getId(), ev.getName()); + } catch (Exception ex) { + Px32.log.error("user {} execute command {} failed", ev.getUser().getId(), ev.getName(), ex); + } break; } } + + @Override + public void onUserContextInteraction(UserContextInteractionEvent ev) { + Px32.log.info("user {} execute context: {}", ev.getUser().getId(), ev.getName()); + } } diff --git a/src/main/kotlin/net/projecttl/p/x32/handler/Command.kt b/src/main/kotlin/net/projecttl/p/x32/handler/CommandExecutor.kt similarity index 89% rename from src/main/kotlin/net/projecttl/p/x32/handler/Command.kt rename to src/main/kotlin/net/projecttl/p/x32/handler/CommandExecutor.kt index 4e2bdbd..2403160 100644 --- a/src/main/kotlin/net/projecttl/p/x32/handler/Command.kt +++ b/src/main/kotlin/net/projecttl/p/x32/handler/CommandExecutor.kt @@ -3,7 +3,7 @@ package net.projecttl.p.x32.handler import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent import net.dv8tion.jda.api.interactions.commands.build.CommandData -interface Command { +interface CommandExecutor { val data: CommandData fun execute(ev: SlashCommandInteractionEvent) } \ No newline at end of file