Compare commits

..

No commits in common. "aaf4f3297a99409fddf36ef19b7971841db6ef91" and "9d3a6b8b8501f311b6d30355d62500287e9b3792" have entirely different histories.

7 changed files with 28 additions and 86 deletions

@ -40,13 +40,12 @@ public record EnumChoices(Class<? extends Enum<?>> enumClass, ChoiceType type, S
if (!(typeParam instanceof Class<?>)) if (!(typeParam instanceof Class<?>))
throw new IllegalArgumentException("ChoiceValueProvider need either a String or Number type parameter."); throw new IllegalArgumentException("ChoiceValueProvider need either a String or Number type parameter.");
if (Long.class.isAssignableFrom((Class<?>) typeParam))
return ChoiceType.INTEGER;
if (Double.class.isAssignableFrom((Class<?>) typeParam))
return ChoiceType.DOUBLE;
if (String.class.isAssignableFrom((Class<?>) typeParam)) if (String.class.isAssignableFrom((Class<?>) typeParam))
return ChoiceType.String; return ChoiceType.String;
throw new IllegalArgumentException("ChoiceValueProvider need either a String, Number or Decimal type parameter."); else if (Number.class.isAssignableFrom((Class<?>) typeParam))
return ChoiceType.Number;
else
throw new IllegalArgumentException("ChoiceValueProvider need either a String or Number type parameter.");
} }
private static SlashCommandOptionChoice[] parseChoices(Class<? extends Enum<?>> enumClass, ChoiceType type) { private static SlashCommandOptionChoice[] parseChoices(Class<? extends Enum<?>> enumClass, ChoiceType type) {
@ -56,12 +55,10 @@ public record EnumChoices(Class<? extends Enum<?>> enumClass, ChoiceType type, S
Object value; Object value;
try { try {
value = method.invoke(enumInstance); value = method.invoke(enumInstance);
if (type.equals(ChoiceType.INTEGER))
choices.add(TypeFactory.annotation(SlashCommandOptionChoice.class, Map.of("name", enumInstance.name(), "longValue", value)));
if (type.equals(ChoiceType.DOUBLE))
choices.add(TypeFactory.annotation(SlashCommandOptionChoice.class, Map.of("name", enumInstance.name(), "doubleValue", value)));
if (type.equals(ChoiceType.String)) if (type.equals(ChoiceType.String))
choices.add(TypeFactory.annotation(SlashCommandOptionChoice.class, Map.of("name", enumInstance.name(), "stringValue", value))); choices.add(TypeFactory.annotation(SlashCommandOptionChoice.class, Map.of("name", enumInstance.name(), "stringValue", value)));
else if (type.equals(ChoiceType.Number))
choices.add(TypeFactory.annotation(SlashCommandOptionChoice.class, Map.of("name", enumInstance.name(), "longValue", value)));
} catch (IllegalAccessException | InvocationTargetException | AnnotationFormatException e) { } catch (IllegalAccessException | InvocationTargetException | AnnotationFormatException e) {
e.printStackTrace(); e.printStackTrace();
return null; return null;
@ -72,7 +69,6 @@ public record EnumChoices(Class<? extends Enum<?>> enumClass, ChoiceType type, S
public static enum ChoiceType { public static enum ChoiceType {
String, String,
INTEGER, Number
DOUBLE
} }
} }

@ -14,7 +14,6 @@ public @interface SlashCommandOption {
public String description() default ""; public String description() default "";
public SlashCommandOptionType type() default SlashCommandOptionType.STRING; public SlashCommandOptionType type() default SlashCommandOptionType.STRING;
public boolean required() default false; public boolean required() default false;
public boolean autocomplete() default false;
public SlashCommandOptionChoice[] choices() default {}; public SlashCommandOptionChoice[] choices() default {};
public Class<? extends Enum<?>> choiceEnum() default PlaceHolderEnum.class; public Class<? extends Enum<?>> choiceEnum() default PlaceHolderEnum.class;

@ -3,6 +3,5 @@ package net.tomatentum.marinara.interaction.commands.annotation;
public @interface SlashCommandOptionChoice { public @interface SlashCommandOptionChoice {
public String name(); public String name();
public long longValue() default Long.MAX_VALUE; public long longValue() default Long.MAX_VALUE;
public double doubleValue() default Double.MAX_VALUE;
public String stringValue() default ""; public String stringValue() default "";
} }

@ -1,26 +1,16 @@
package net.tomatentum.marinara.interaction.commands.option; package net.tomatentum.marinara.interaction.commands.option;
public enum SlashCommandOptionType { public enum SlashCommandOptionType {
SUB_COMMAND(1), ATTACHMENT,
SUB_COMMAND_GROUP(2), BOOLEAN,
STRING(3), CHANNEL,
INTEGER(4), DECIMAL,
BOOLEAN(5), LONG,
USER(6), MENTIONABLE,
CHANNEL(7), ROLE,
ROLE(8), STRING,
MENTIONABLE(9), SUB_COMMAND,
DOUBLE(10), SUB_COMMAND_GROUP,
ATTACHMENT(11), UNKNOW,
UNKNOWN(-1); USER
private final int value;
private SlashCommandOptionType(int value) {
this.value = value;
}
public int getValue() {
return value;
}
} }

@ -15,9 +15,6 @@ import org.javacord.api.interaction.ButtonInteraction;
import org.javacord.api.interaction.SlashCommandBuilder; import org.javacord.api.interaction.SlashCommandBuilder;
import org.javacord.api.interaction.SlashCommandInteraction; import org.javacord.api.interaction.SlashCommandInteraction;
import org.javacord.api.interaction.SlashCommandInteractionOption; import org.javacord.api.interaction.SlashCommandInteractionOption;
import org.javacord.api.interaction.SlashCommandOptionBuilder;
import org.javacord.api.interaction.SlashCommandOptionChoiceBuilder;
import org.javacord.api.interaction.SlashCommandOptionType;
import io.leangen.geantyref.AnnotationFormatException; import io.leangen.geantyref.AnnotationFormatException;
import io.leangen.geantyref.TypeFactory; import io.leangen.geantyref.TypeFactory;
@ -133,35 +130,17 @@ public class JavacordWrapper extends LibraryWrapper {
} }
private org.javacord.api.interaction.SlashCommandOption convertOptionDef(SlashCommandOption option) { private org.javacord.api.interaction.SlashCommandOption convertOptionDef(SlashCommandOption option) {
SlashCommandOptionType type = SlashCommandOptionType.fromValue(option.type().getValue()); org.javacord.api.interaction.SlashCommandOptionType type = Enum.valueOf(org.javacord.api.interaction.SlashCommandOptionType.class, option.type().toString());
SlashCommandOptionBuilder builder = new SlashCommandOptionBuilder();
builder
.setType(type)
.setName(option.name())
.setDescription(option.description())
.setRequired(option.required())
.setAutocompletable(option.autocomplete())
.setChoices(convertChoices(option));
return builder.build(); List<org.javacord.api.interaction.SlashCommandOptionChoice> choices = new ArrayList<>();
}
private List<org.javacord.api.interaction.SlashCommandOptionChoice> convertChoices(SlashCommandOption option) {
List<org.javacord.api.interaction.SlashCommandOptionChoice> convertedChoices = new ArrayList<>();
for (SlashCommandOptionChoice choice : ExecutableSlashCommandDefinition.getActualChoices(option)) { for (SlashCommandOptionChoice choice : ExecutableSlashCommandDefinition.getActualChoices(option)) {
SlashCommandOptionChoiceBuilder builder = new SlashCommandOptionChoiceBuilder(); if (choice.stringValue().isEmpty())
builder.setName(choice.name()); choices.add(org.javacord.api.interaction.SlashCommandOptionChoice.create(choice.name(), choice.longValue()));
if (choice.longValue() != Long.MAX_VALUE) else
builder.setValue(choice.longValue()); choices.add(org.javacord.api.interaction.SlashCommandOptionChoice.create(choice.name(), choice.stringValue()));
/*
not yet available
if (choice.doubleValue() != Double.MAX_VALUE)
builder.setValue(choice.doubleValue());
*/
if (!choice.stringValue().isEmpty())
builder.setValue(choice.stringValue());
} }
return convertedChoices;
return org.javacord.api.interaction.SlashCommandOption.createWithChoices(type, option.name(), option.description(), option.required(), choices);
} }
@Override @Override

@ -1,20 +0,0 @@
package net.tomatentum.marinara.test;
import net.tomatentum.marinara.interaction.commands.ChoiceValueProvider;
public enum TestChoiceEnum implements ChoiceValueProvider<String> {
TestValue("testValue"),
FooBar("fooBar"),
Spongebob("spongebob");
private String value;
private TestChoiceEnum(String value) {
this.value = value;
}
@Override
public String getChoiceValue() {
return value;
}
}

@ -20,8 +20,7 @@ public class TestCommand implements InteractionHandler {
@SlashCommandOption( @SlashCommandOption(
name = "foo", name = "foo",
description = "foo bar is very fooby", description = "foo bar is very fooby",
type = SlashCommandOptionType.STRING, type = SlashCommandOptionType.STRING
choiceEnum = TestChoiceEnum.class
) )
} }
) )