finish first version of javacord wrapper with implementation of:

- getInteractionType()
- convertCommandOption()
- getCommandDefinition
This commit is contained in:
tueem 2024-10-31 16:06:23 +01:00
parent b0abb423d3
commit 6bd6021b86
No known key found for this signature in database
GPG Key ID: 819A0F7C36B9CF07
3 changed files with 69 additions and 8 deletions
gradle
wrapper/javacord
build.gradle.kts
src/main/java/net/tomatentum/marinare/wrapper/javacord

@ -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"}

@ -22,6 +22,7 @@ dependencies {
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
implementation(libs.log4j)
implementation(libs.javacord)
implementation(libs.geantyref)
implementation(project(":lib"))
}

@ -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<SlashCommandInteractionOption> 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) {
@ -98,4 +132,28 @@ public class JavacordWrapper extends LibraryWrapper {
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;
}
}
}