From 1cb6cd0e0557a4c2813548d100a8dd56f35c7da0 Mon Sep 17 00:00:00 2001 From: Tueem Date: Sun, 15 Dec 2024 23:15:29 +0100 Subject: [PATCH] clean up code and switch to request instead of getting from cache --- .../interaction/annotation/AutoComplete.java | 7 ++++ .../JavacordContextObjectProvider.java | 34 ++++++++++--------- .../wrapper/javacord/JavacordWrapper.java | 17 +++++----- 3 files changed, 34 insertions(+), 24 deletions(-) diff --git a/lib/src/main/java/net/tomatentum/marinara/interaction/annotation/AutoComplete.java b/lib/src/main/java/net/tomatentum/marinara/interaction/annotation/AutoComplete.java index 69e80e0..46aa6aa 100644 --- a/lib/src/main/java/net/tomatentum/marinara/interaction/annotation/AutoComplete.java +++ b/lib/src/main/java/net/tomatentum/marinara/interaction/annotation/AutoComplete.java @@ -1,5 +1,12 @@ package net.tomatentum.marinara.interaction.annotation; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target({ElementType.METHOD}) +@Retention(RetentionPolicy.RUNTIME) public @interface AutoComplete { } diff --git a/wrapper/javacord/src/main/java/net/tomatentum/marinara/wrapper/javacord/JavacordContextObjectProvider.java b/wrapper/javacord/src/main/java/net/tomatentum/marinara/wrapper/javacord/JavacordContextObjectProvider.java index f1b8f49..0812f0c 100644 --- a/wrapper/javacord/src/main/java/net/tomatentum/marinara/wrapper/javacord/JavacordContextObjectProvider.java +++ b/wrapper/javacord/src/main/java/net/tomatentum/marinara/wrapper/javacord/JavacordContextObjectProvider.java @@ -4,6 +4,7 @@ import org.javacord.api.interaction.AutocompleteInteraction; import org.javacord.api.interaction.ButtonInteraction; import org.javacord.api.interaction.SlashCommandInteraction; import org.javacord.api.interaction.SlashCommandInteractionOption; +import org.javacord.api.interaction.SlashCommandOptionType; import net.tomatentum.marinara.wrapper.ContextObjectProvider; @@ -26,7 +27,8 @@ public class JavacordContextObjectProvider implements ContextObjectProvider { } private Object getOptionValue(SlashCommandInteractionOption option) { - switch (getOptionType(option)) { + SlashCommandOptionType type = getOptionType(option); + switch (type) { case ATTACHMENT: return option.getAttachmentValue().get(); case BOOLEAN: @@ -38,40 +40,40 @@ public class JavacordContextObjectProvider implements ContextObjectProvider { case LONG: return option.getLongValue().get(); case MENTIONABLE: - return option.getMentionableValue().get(); + return option.requestMentionableValue().get(); case ROLE: return option.getRoleValue().get(); case STRING: return option.getStringValue().get(); case USER: - return option.getUserValue().get(); + return option.requestUserValue().get(); default: return null; } } - private org.javacord.api.interaction.SlashCommandOptionType getOptionType(SlashCommandInteractionOption option) { + private SlashCommandOptionType getOptionType(SlashCommandInteractionOption option) { if (option.getAttachmentValue().isPresent()) - return org.javacord.api.interaction.SlashCommandOptionType.ATTACHMENT; + return SlashCommandOptionType.ATTACHMENT; if (option.getBooleanValue().isPresent()) - return org.javacord.api.interaction.SlashCommandOptionType.BOOLEAN; + return SlashCommandOptionType.BOOLEAN; if (option.getChannelValue().isPresent()) - return org.javacord.api.interaction.SlashCommandOptionType.CHANNEL; + return SlashCommandOptionType.CHANNEL; if (option.getDecimalValue().isPresent()) - return org.javacord.api.interaction.SlashCommandOptionType.DECIMAL; + return SlashCommandOptionType.DECIMAL; if (option.getLongValue().isPresent()) - return org.javacord.api.interaction.SlashCommandOptionType.LONG; - if (option.getMentionableValue().isPresent()) - return org.javacord.api.interaction.SlashCommandOptionType.MENTIONABLE; + return SlashCommandOptionType.LONG; + if (option.requestMentionableValue().isPresent()) + return SlashCommandOptionType.MENTIONABLE; if (option.getRoleValue().isPresent()) - return org.javacord.api.interaction.SlashCommandOptionType.ROLE; + return SlashCommandOptionType.ROLE; if (option.getStringValue().isPresent()) - return org.javacord.api.interaction.SlashCommandOptionType.ATTACHMENT; - if (option.getUserValue().isPresent()) - return org.javacord.api.interaction.SlashCommandOptionType.USER; + return SlashCommandOptionType.STRING; + if (option.requestUserValue().isPresent()) + return SlashCommandOptionType.USER; - return org.javacord.api.interaction.SlashCommandOptionType.UNKNOWN; + return SlashCommandOptionType.UNKNOWN; } @Override diff --git a/wrapper/javacord/src/main/java/net/tomatentum/marinara/wrapper/javacord/JavacordWrapper.java b/wrapper/javacord/src/main/java/net/tomatentum/marinara/wrapper/javacord/JavacordWrapper.java index 1bb3697..b3a3da4 100644 --- a/wrapper/javacord/src/main/java/net/tomatentum/marinara/wrapper/javacord/JavacordWrapper.java +++ b/wrapper/javacord/src/main/java/net/tomatentum/marinara/wrapper/javacord/JavacordWrapper.java @@ -42,13 +42,12 @@ public class JavacordWrapper extends LibraryWrapper { @Override public InteractionType getInteractionType(Class clazz) { + if (AutocompleteInteraction.class.isAssignableFrom(clazz)) + return InteractionType.AUTOCOMPLETE; if (ApplicationCommandInteraction.class.isAssignableFrom(clazz)) return InteractionType.COMMAND; if (ButtonInteraction.class.isAssignableFrom(clazz)) return InteractionType.BUTTON; - if (AutocompleteInteraction.class.isAssignableFrom(clazz)) - return InteractionType.AUTOCOMPLETE; - return null; } @@ -83,11 +82,13 @@ public class JavacordWrapper extends LibraryWrapper { 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()))); + if (!options.isEmpty()) { + 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(); }