Compare commits
2 Commits
9d3a6b8b85
...
aaf4f3297a
Author | SHA1 | Date | |
---|---|---|---|
aaf4f3297a | |||
445190db89 |
@ -40,12 +40,13 @@ public record EnumChoices(Class<? extends Enum<?>> enumClass, ChoiceType type, S
|
||||
if (!(typeParam instanceof Class<?>))
|
||||
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))
|
||||
return ChoiceType.String;
|
||||
else if (Number.class.isAssignableFrom((Class<?>) typeParam))
|
||||
return ChoiceType.Number;
|
||||
else
|
||||
throw new IllegalArgumentException("ChoiceValueProvider need either a String or Number type parameter.");
|
||||
throw new IllegalArgumentException("ChoiceValueProvider need either a String, Number or Decimal type parameter.");
|
||||
}
|
||||
|
||||
private static SlashCommandOptionChoice[] parseChoices(Class<? extends Enum<?>> enumClass, ChoiceType type) {
|
||||
@ -55,10 +56,12 @@ public record EnumChoices(Class<? extends Enum<?>> enumClass, ChoiceType type, S
|
||||
Object value;
|
||||
try {
|
||||
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))
|
||||
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) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
@ -69,6 +72,7 @@ public record EnumChoices(Class<? extends Enum<?>> enumClass, ChoiceType type, S
|
||||
|
||||
public static enum ChoiceType {
|
||||
String,
|
||||
Number
|
||||
INTEGER,
|
||||
DOUBLE
|
||||
}
|
||||
}
|
||||
|
@ -14,6 +14,7 @@ public @interface SlashCommandOption {
|
||||
public String description() default "";
|
||||
public SlashCommandOptionType type() default SlashCommandOptionType.STRING;
|
||||
public boolean required() default false;
|
||||
public boolean autocomplete() default false;
|
||||
public SlashCommandOptionChoice[] choices() default {};
|
||||
public Class<? extends Enum<?>> choiceEnum() default PlaceHolderEnum.class;
|
||||
|
||||
|
@ -3,5 +3,6 @@ package net.tomatentum.marinara.interaction.commands.annotation;
|
||||
public @interface SlashCommandOptionChoice {
|
||||
public String name();
|
||||
public long longValue() default Long.MAX_VALUE;
|
||||
public double doubleValue() default Double.MAX_VALUE;
|
||||
public String stringValue() default "";
|
||||
}
|
||||
|
34
lib/src/main/java/net/tomatentum/marinara/interaction/commands/option/SlashCommandOptionType.java
34
lib/src/main/java/net/tomatentum/marinara/interaction/commands/option/SlashCommandOptionType.java
@ -1,16 +1,26 @@
|
||||
package net.tomatentum.marinara.interaction.commands.option;
|
||||
|
||||
public enum SlashCommandOptionType {
|
||||
ATTACHMENT,
|
||||
BOOLEAN,
|
||||
CHANNEL,
|
||||
DECIMAL,
|
||||
LONG,
|
||||
MENTIONABLE,
|
||||
ROLE,
|
||||
STRING,
|
||||
SUB_COMMAND,
|
||||
SUB_COMMAND_GROUP,
|
||||
UNKNOW,
|
||||
USER
|
||||
SUB_COMMAND(1),
|
||||
SUB_COMMAND_GROUP(2),
|
||||
STRING(3),
|
||||
INTEGER(4),
|
||||
BOOLEAN(5),
|
||||
USER(6),
|
||||
CHANNEL(7),
|
||||
ROLE(8),
|
||||
MENTIONABLE(9),
|
||||
DOUBLE(10),
|
||||
ATTACHMENT(11),
|
||||
UNKNOWN(-1);
|
||||
|
||||
private final int value;
|
||||
|
||||
private SlashCommandOptionType(int value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public int getValue() {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
@ -15,6 +15,9 @@ import org.javacord.api.interaction.ButtonInteraction;
|
||||
import org.javacord.api.interaction.SlashCommandBuilder;
|
||||
import org.javacord.api.interaction.SlashCommandInteraction;
|
||||
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.TypeFactory;
|
||||
@ -130,17 +133,35 @@ public class JavacordWrapper extends LibraryWrapper {
|
||||
}
|
||||
|
||||
private org.javacord.api.interaction.SlashCommandOption convertOptionDef(SlashCommandOption option) {
|
||||
org.javacord.api.interaction.SlashCommandOptionType type = Enum.valueOf(org.javacord.api.interaction.SlashCommandOptionType.class, option.type().toString());
|
||||
SlashCommandOptionType type = SlashCommandOptionType.fromValue(option.type().getValue());
|
||||
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)) {
|
||||
if (choice.stringValue().isEmpty())
|
||||
choices.add(org.javacord.api.interaction.SlashCommandOptionChoice.create(choice.name(), choice.longValue()));
|
||||
else
|
||||
choices.add(org.javacord.api.interaction.SlashCommandOptionChoice.create(choice.name(), choice.stringValue()));
|
||||
SlashCommandOptionChoiceBuilder builder = new SlashCommandOptionChoiceBuilder();
|
||||
builder.setName(choice.name());
|
||||
if (choice.longValue() != Long.MAX_VALUE)
|
||||
builder.setValue(choice.longValue());
|
||||
/*
|
||||
not yet available
|
||||
if (choice.doubleValue() != Double.MAX_VALUE)
|
||||
builder.setValue(choice.doubleValue());
|
||||
*/
|
||||
if (!choice.stringValue().isEmpty())
|
||||
builder.setValue(choice.stringValue());
|
||||
}
|
||||
|
||||
return org.javacord.api.interaction.SlashCommandOption.createWithChoices(type, option.name(), option.description(), option.required(), choices);
|
||||
return convertedChoices;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -0,0 +1,20 @@
|
||||
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,7 +20,8 @@ public class TestCommand implements InteractionHandler {
|
||||
@SlashCommandOption(
|
||||
name = "foo",
|
||||
description = "foo bar is very fooby",
|
||||
type = SlashCommandOptionType.STRING
|
||||
type = SlashCommandOptionType.STRING,
|
||||
choiceEnum = TestChoiceEnum.class
|
||||
)
|
||||
}
|
||||
)
|
||||
|
Loading…
x
Reference in New Issue
Block a user