From 6bd6021b8642d9518310f3486e0afe169ff9a33a Mon Sep 17 00:00:00 2001 From: tueem Date: Thu, 31 Oct 2024 16:06:23 +0100 Subject: [PATCH] finish first version of javacord wrapper with implementation of: - getInteractionType() - convertCommandOption() - getCommandDefinition --- gradle/libs.versions.toml | 2 + wrapper/javacord/build.gradle.kts | 1 + .../wrapper/javacord/JavacordWrapper.java | 74 +++++++++++++++++-- 3 files changed, 69 insertions(+), 8 deletions(-) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 41b1d37..6f2cae6 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -7,6 +7,7 @@ guava = "33.0.0-jre" junit-jupiter = "5.10.2" log4j = "2.24.1" javacord = "3.8.0" +geantyref = "2.0.0" [libraries] commons-math3 = { module = "org.apache.commons:commons-math3", version.ref = "commons-math3" } @@ -14,3 +15,4 @@ guava = { module = "com.google.guava:guava", version.ref = "guava" } junit-jupiter = { module = "org.junit.jupiter:junit-jupiter", version.ref = "junit-jupiter" } log4j = { module = "org.apache.logging.log4j:log4j-core", version.ref = "log4j"} javacord = { module = "org.javacord:javacord", version.ref = "javacord"} +geantyref = { module = "io.leangen.geantyref:geantyref", version.ref = "geantyref"} diff --git a/wrapper/javacord/build.gradle.kts b/wrapper/javacord/build.gradle.kts index 1f1ab84..9ec7322 100644 --- a/wrapper/javacord/build.gradle.kts +++ b/wrapper/javacord/build.gradle.kts @@ -22,6 +22,7 @@ dependencies { testRuntimeOnly("org.junit.platform:junit-platform-launcher") implementation(libs.log4j) implementation(libs.javacord) + implementation(libs.geantyref) implementation(project(":lib")) } diff --git a/wrapper/javacord/src/main/java/net/tomatentum/marinare/wrapper/javacord/JavacordWrapper.java b/wrapper/javacord/src/main/java/net/tomatentum/marinare/wrapper/javacord/JavacordWrapper.java index 41ae182..d50c47c 100644 --- a/wrapper/javacord/src/main/java/net/tomatentum/marinare/wrapper/javacord/JavacordWrapper.java +++ b/wrapper/javacord/src/main/java/net/tomatentum/marinare/wrapper/javacord/JavacordWrapper.java @@ -2,15 +2,20 @@ package net.tomatentum.marinare.wrapper.javacord; import java.util.ArrayList; import java.util.Arrays; -import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.List; +import java.util.Map; import java.util.Set; import org.javacord.api.DiscordApi; +import org.javacord.api.interaction.ApplicationCommandInteraction; import org.javacord.api.interaction.SlashCommandBuilder; +import org.javacord.api.interaction.SlashCommandInteraction; +import org.javacord.api.interaction.SlashCommandInteractionOption; +import io.leangen.geantyref.AnnotationFormatException; +import io.leangen.geantyref.TypeFactory; import net.tomatentum.marinara.interaction.InteractionType; import net.tomatentum.marinara.interaction.commands.ExecutableSlashCommandDefinition; import net.tomatentum.marinara.interaction.commands.SlashCommandDefinition; @@ -27,6 +32,7 @@ public class JavacordWrapper extends LibraryWrapper { public JavacordWrapper(DiscordApi api) { this.api = api; + api.addInteractionCreateListener((e) -> handleInteraction(e.getInteraction())); } @Override @@ -52,20 +58,48 @@ public class JavacordWrapper extends LibraryWrapper { @Override public InteractionType getInteractionType(Class clazz) { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'getInteractionType'"); + if (clazz.isAssignableFrom(ApplicationCommandInteraction.class)) + return InteractionType.COMMAND; + + return null; } @Override public Object convertCommandOption(Object context, SlashCommandOptionType type, String optionName) { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'convertCommandOption'"); + if (!(context instanceof SlashCommandInteraction)) + return null; + SlashCommandInteraction interaction = (SlashCommandInteraction) context; + if (!interaction.getArguments().isEmpty()) + return getOptionValue(interaction.getOptionByName(optionName).get(), type); + + SlashCommandInteractionOption subCommandOption = interaction.getOptions().getFirst(); + + if (!subCommandOption.getOptions().isEmpty()) + subCommandOption = subCommandOption.getOptions().getFirst(); + + return getOptionValue(subCommandOption.getOptionByName(optionName).get(), type); } @Override public ExecutableSlashCommandDefinition getCommandDefinition(Object context) { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'getCommandDefinition'"); + if (!(context instanceof SlashCommandInteraction)) + return null; + + SlashCommandInteraction interaction = (SlashCommandInteraction) context; + ExecutableSlashCommandDefinition.Builder builder = new ExecutableSlashCommandDefinition.Builder(); + List options = interaction.getOptions(); + try { + builder.setApplicationCommand(TypeFactory.annotation(SlashCommand.class, Map.of("name", interaction.getCommandName()))); + if (!options.getFirst().getArguments().isEmpty()) { + builder.setSubCommandGroup(TypeFactory.annotation(SubCommandGroup.class, Map.of("name", options.getFirst().getName()))); + builder.setSubCommand(TypeFactory.annotation(SubCommand.class, Map.of("name", options.getFirst().getOptions().getFirst().getName()))); + }else + builder.setSubCommand(TypeFactory.annotation(SubCommand.class, Map.of("name", options.getFirst().getName()))); + } catch (AnnotationFormatException e) { + e.printStackTrace(); + } + + return builder.build(); } private SlashCommandBuilder convertSlashCommand(SlashCommandDefinition def) { @@ -97,5 +131,29 @@ public class JavacordWrapper extends LibraryWrapper { org.javacord.api.interaction.SlashCommandOptionType type = Enum.valueOf(org.javacord.api.interaction.SlashCommandOptionType.class, option.type().toString()); return org.javacord.api.interaction.SlashCommandOption.create(type, option.name(), option.description(), option.required()); } - + + private Object getOptionValue(SlashCommandInteractionOption option, SlashCommandOptionType type) { + switch (type) { + case ATTACHMENT: + return option.getAttachmentValue().get(); + case BOOLEAN: + return option.getBooleanValue().get(); + case CHANNEL: + return option.getChannelValue().get(); + case DECIMAL: + return option.getDecimalValue().get(); + case LONG: + return option.getLongValue().get(); + case MENTIONABLE: + return option.getMentionableValue().get(); + case ROLE: + return option.getRoleValue().get(); + case STRING: + return option.getStringValue().get(); + case USER: + return option.getUserValue().get(); + default: + return null; + } + } }