finish first version of javacord wrapper with implementation of:
- getInteractionType() - convertCommandOption() - getCommandDefinition
This commit is contained in:
		| @@ -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) { | ||||
| @@ -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; | ||||
|         } | ||||
|     } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user