feat(core): add InteractionProcessors and InteractionExecutors and improve Autocomplete Interaction
This commit is contained in:
@@ -2,10 +2,12 @@ package net.tomatentum.marinara.wrapper.javacord;
|
||||
|
||||
import org.javacord.api.interaction.AutocompleteInteraction;
|
||||
import org.javacord.api.interaction.ButtonInteraction;
|
||||
import org.javacord.api.interaction.InteractionBase;
|
||||
import org.javacord.api.interaction.SlashCommandInteraction;
|
||||
import org.javacord.api.interaction.SlashCommandInteractionOption;
|
||||
import org.javacord.api.interaction.SlashCommandOptionType;
|
||||
|
||||
import net.tomatentum.marinara.interaction.commands.option.AutocompleteOptionData;
|
||||
import net.tomatentum.marinara.wrapper.ContextObjectProvider;
|
||||
|
||||
public class JavacordContextObjectProvider implements ContextObjectProvider {
|
||||
@@ -16,14 +18,14 @@ public class JavacordContextObjectProvider implements ContextObjectProvider {
|
||||
return null;
|
||||
SlashCommandInteraction interaction = (SlashCommandInteraction) context;
|
||||
if (!interaction.getArguments().isEmpty())
|
||||
return getOptionValue(interaction.getOptionByName(optionName).get());
|
||||
return getOptionValue(interaction.getOptionByName(optionName).orElse(null));
|
||||
|
||||
SlashCommandInteractionOption subCommandOption = interaction.getOptions().getFirst();
|
||||
|
||||
if (!subCommandOption.getOptions().isEmpty())
|
||||
subCommandOption = subCommandOption.getOptions().getFirst();
|
||||
|
||||
return getOptionValue(subCommandOption.getOptionByName(optionName).get());
|
||||
return getOptionValue(subCommandOption.getOptionByName(optionName).orElse(null));
|
||||
}
|
||||
|
||||
private Object getOptionValue(SlashCommandInteractionOption option) {
|
||||
@@ -89,7 +91,7 @@ public class JavacordContextObjectProvider implements ContextObjectProvider {
|
||||
|
||||
@Override
|
||||
public Object getInteractionContextObject(Object context, Class<?> type) {
|
||||
ButtonInteraction button = (ButtonInteraction) context;
|
||||
InteractionBase button = (InteractionBase) context;
|
||||
switch (type.getName()) {
|
||||
case "org.javacord.api.entity.channel.TextChannel":
|
||||
return button.getChannel().orElse(null);
|
||||
@@ -102,9 +104,9 @@ public class JavacordContextObjectProvider implements ContextObjectProvider {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getAutocompleteFocusedOption(Object context) {
|
||||
AutocompleteInteraction interaction = (AutocompleteInteraction) context;
|
||||
return getOptionValue(interaction.getFocusedOption());
|
||||
public AutocompleteOptionData getAutocompleteFocusedOption(Object context) {
|
||||
SlashCommandInteractionOption option = ((AutocompleteInteraction) context).getFocusedOption();
|
||||
return new AutocompleteOptionData(option.getName(), getOptionValue(option));
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -50,10 +50,10 @@ public class JavacordConverterSpec implements CommandConverter.Spec<SlashCommand
|
||||
.setName(option.name())
|
||||
.setDescription(option.description())
|
||||
.setRequired(option.required())
|
||||
.setAutocompletable(option.autocomplete())
|
||||
.setMinLength(Double.valueOf(option.range().min()).longValue())
|
||||
.setAutocompletable(option.autocompletes().length > 0)
|
||||
.setMinLength((long) option.range().min())
|
||||
.setDecimalMinValue(option.range().min())
|
||||
.setMaxLength(Double.valueOf(option.range().max()).longValue())
|
||||
.setMaxLength((long) option.range().max())
|
||||
.setDecimalMaxValue(option.range().max())
|
||||
.setChoices(choices)
|
||||
.build();
|
||||
|
@@ -1,7 +1,11 @@
|
||||
package net.tomatentum.marinara.wrapper.javacord;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.javacord.api.DiscordApi;
|
||||
import org.javacord.api.interaction.AutocompleteInteraction;
|
||||
import org.javacord.api.interaction.SlashCommandBuilder;
|
||||
import org.javacord.api.interaction.SlashCommandOptionChoice;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import net.tomatentum.marinara.wrapper.CommandConverter;
|
||||
@@ -27,7 +31,7 @@ public class JavacordWrapper extends LibraryWrapper {
|
||||
|
||||
if (api != null) {
|
||||
this.commandRegisterer = CommandRegisterer.of(new JavacordRegistererStrategy(api), converter);
|
||||
api.addInteractionCreateListener((e) -> handleInteraction(e.getInteraction()));
|
||||
api.addInteractionCreateListener(e -> handleInteraction(e.getInteraction()));
|
||||
}else
|
||||
logger.warn("DiscordApi was null so no Events were subscribed to.");
|
||||
logger.info("Javacord wrapper loaded!");
|
||||
@@ -52,4 +56,15 @@ public class JavacordWrapper extends LibraryWrapper {
|
||||
return contextObjectProvider;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void respondAutocomplete(Object context, List<Object> options) {
|
||||
if (context instanceof AutocompleteInteraction interaction) {
|
||||
List<SlashCommandOptionChoice> choices = options.stream()
|
||||
.filter(SlashCommandOptionChoice.class::isInstance)
|
||||
.map(o -> (SlashCommandOptionChoice)o)
|
||||
.toList();
|
||||
interaction.respondWithChoices(choices);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,53 +1,39 @@
|
||||
package net.tomatentum.marinara.wrapper.javacord.identifierconverter;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.javacord.api.interaction.AutocompleteInteraction;
|
||||
import org.javacord.api.interaction.SlashCommandInteractionOption;
|
||||
|
||||
import net.tomatentum.marinara.interaction.InteractionType;
|
||||
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
||||
import net.tomatentum.marinara.interaction.ident.SlashCommandIdentifier;
|
||||
import net.tomatentum.marinara.registry.InteractionEntry;
|
||||
import net.tomatentum.marinara.registry.InteractionRegistry;
|
||||
import net.tomatentum.marinara.wrapper.IdentifierProvider;
|
||||
|
||||
public class AutocompleteIdentifierConverter implements IdentifierProvider.Converter<AutocompleteInteraction> {
|
||||
|
||||
@Override
|
||||
public InteractionIdentifier convert(AutocompleteInteraction context, InteractionRegistry registry) {
|
||||
public InteractionIdentifier convert(AutocompleteInteraction context) {
|
||||
List<SlashCommandInteractionOption> options = context.getOptions();
|
||||
String commandName = context.getCommandName();
|
||||
InteractionIdentifier ident;
|
||||
|
||||
if (!options.isEmpty()) {
|
||||
List<SlashCommandInteractionOption> sub_options = context.getOptions().getFirst().getOptions();
|
||||
if (!sub_options.isEmpty())
|
||||
ident = InteractionIdentifier.createHierarchy(
|
||||
InteractionType.COMMAND,
|
||||
List<SlashCommandInteractionOption> subOptions = context.getOptions().getFirst().getOptions();
|
||||
if (!subOptions.isEmpty())
|
||||
return InteractionIdentifier.createHierarchy(
|
||||
InteractionType.AUTOCOMPLETE,
|
||||
commandName,
|
||||
options.getFirst().getName(),
|
||||
sub_options.getFirst().getName());
|
||||
subOptions.getFirst().getName());
|
||||
else
|
||||
ident = InteractionIdentifier.createHierarchy(
|
||||
InteractionType.COMMAND,
|
||||
return InteractionIdentifier.createHierarchy(
|
||||
InteractionType.AUTOCOMPLETE,
|
||||
commandName,
|
||||
options.getFirst().getName());
|
||||
}else
|
||||
ident = InteractionIdentifier.createHierarchy(
|
||||
InteractionType.COMMAND,
|
||||
return InteractionIdentifier.createHierarchy(
|
||||
InteractionType.AUTOCOMPLETE,
|
||||
commandName);
|
||||
|
||||
Optional<InteractionEntry> entry = registry.findFor(ident);
|
||||
if (entry.isPresent() && entry.get().identifier() instanceof SlashCommandIdentifier) {
|
||||
SlashCommandIdentifier sIdent = (SlashCommandIdentifier) entry.get().identifier();
|
||||
return InteractionIdentifier.builder()
|
||||
.type(InteractionType.AUTOCOMPLETE)
|
||||
.name(sIdent.autocompleteRef()[0])
|
||||
.build();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -4,13 +4,12 @@ import org.javacord.api.interaction.ButtonInteraction;
|
||||
|
||||
import net.tomatentum.marinara.interaction.InteractionType;
|
||||
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
||||
import net.tomatentum.marinara.registry.InteractionRegistry;
|
||||
import net.tomatentum.marinara.wrapper.IdentifierProvider;
|
||||
|
||||
public class ButtonIdentifierConverter implements IdentifierProvider.Converter<ButtonInteraction> {
|
||||
|
||||
@Override
|
||||
public InteractionIdentifier convert(ButtonInteraction context, InteractionRegistry registry) {
|
||||
public InteractionIdentifier convert(ButtonInteraction context) {
|
||||
return InteractionIdentifier.builder().name(context.getCustomId()).type(InteractionType.BUTTON).build();
|
||||
}
|
||||
|
||||
|
@@ -7,13 +7,12 @@ import org.javacord.api.interaction.SlashCommandInteractionOption;
|
||||
|
||||
import net.tomatentum.marinara.interaction.InteractionType;
|
||||
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
||||
import net.tomatentum.marinara.registry.InteractionRegistry;
|
||||
import net.tomatentum.marinara.wrapper.IdentifierProvider;
|
||||
|
||||
public class SlashCommandIdentifierConverter implements IdentifierProvider.Converter<SlashCommandInteraction> {
|
||||
|
||||
@Override
|
||||
public InteractionIdentifier convert(SlashCommandInteraction context, InteractionRegistry registry) {
|
||||
public InteractionIdentifier convert(SlashCommandInteraction context) {
|
||||
List<SlashCommandInteractionOption> options = context.getOptions();
|
||||
String commandName = context.getCommandName();
|
||||
if (!options.isEmpty()) {
|
||||
|
@@ -6,17 +6,27 @@ import java.util.Collections;
|
||||
|
||||
import org.javacord.api.event.interaction.SlashCommandCreateEvent;
|
||||
import org.javacord.api.interaction.AutocompleteInteraction;
|
||||
import org.javacord.api.interaction.SlashCommandInteraction;
|
||||
|
||||
import net.tomatentum.marinara.interaction.InteractionHandler;
|
||||
import net.tomatentum.marinara.interaction.annotation.AutoComplete;
|
||||
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommand;
|
||||
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommandOption;
|
||||
import net.tomatentum.marinara.interaction.commands.option.SlashCommandOptionType;
|
||||
|
||||
public class TestAutocomplete implements InteractionHandler {
|
||||
|
||||
@SlashCommand(name = "test")
|
||||
@SlashCommand(
|
||||
name = "test",
|
||||
options = @SlashCommandOption(
|
||||
name = "foo",
|
||||
type = SlashCommandOptionType.STRING,
|
||||
autocompletes = @AutoComplete("testAuto")
|
||||
)
|
||||
)
|
||||
@AutoComplete("testAuto")
|
||||
public void exec(SlashCommandCreateEvent context) {
|
||||
|
||||
public void exec(SlashCommandInteraction context) {
|
||||
|
||||
}
|
||||
|
||||
@AutoComplete("testAuto")
|
||||
|
Reference in New Issue
Block a user