feat(core): add InteractionProcessors and InteractionExecutors and improve Autocomplete Interaction
This commit is contained in:
@@ -5,7 +5,9 @@ import java.util.List;
|
||||
import discord4j.core.event.domain.interaction.ChatInputAutoCompleteEvent;
|
||||
import discord4j.core.event.domain.interaction.ChatInputInteractionEvent;
|
||||
import discord4j.core.event.domain.interaction.ComponentInteractionEvent;
|
||||
import discord4j.core.event.domain.interaction.InteractionCreateEvent;
|
||||
import discord4j.core.object.command.ApplicationCommandInteractionOption;
|
||||
import net.tomatentum.marinara.interaction.commands.option.AutocompleteOptionData;
|
||||
import net.tomatentum.marinara.interaction.commands.option.SlashCommandOptionType;
|
||||
import net.tomatentum.marinara.wrapper.ContextObjectProvider;
|
||||
|
||||
@@ -79,24 +81,25 @@ public class Discord4JContextObjectProvider implements ContextObjectProvider {
|
||||
|
||||
@Override
|
||||
public Object getInteractionContextObject(Object context, Class<?> type) {
|
||||
ComponentInteractionEvent componentInteractionEvent = (ComponentInteractionEvent) context;
|
||||
InteractionCreateEvent interactionEvent = (InteractionCreateEvent) context;
|
||||
switch (type.getName()) {
|
||||
case "discord4j.core.object.entity.channel.MessageChannel":
|
||||
return componentInteractionEvent.getInteraction().getChannel().block();
|
||||
return interactionEvent.getInteraction().getChannel().block();
|
||||
case "discord4j.core.object.entity.Guild":
|
||||
return componentInteractionEvent.getInteraction().getGuild().block();
|
||||
return interactionEvent.getInteraction().getGuild().block();
|
||||
case "discord4j.core.object.entity.Member":
|
||||
return componentInteractionEvent.getInteraction().getMember().orElse(null);
|
||||
return interactionEvent.getInteraction().getMember().orElse(null);
|
||||
case "discord4j.core.object.entity.User":
|
||||
return componentInteractionEvent.getInteraction().getUser();
|
||||
return interactionEvent.getInteraction().getUser();
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getAutocompleteFocusedOption(Object context) {
|
||||
ChatInputAutoCompleteEvent interaction = (ChatInputAutoCompleteEvent) context;
|
||||
return getOptionValue(interaction.getFocusedOption());
|
||||
public AutocompleteOptionData getAutocompleteFocusedOption(Object context) {
|
||||
ApplicationCommandInteractionOption option = ((ChatInputAutoCompleteEvent) context).getFocusedOption();
|
||||
return new AutocompleteOptionData(option.getName(), getOptionValue(option));
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -55,10 +55,10 @@ public class Discord4JConverterSpec implements CommandConverter.Spec<Application
|
||||
.name(option.name())
|
||||
.description(option.description())
|
||||
.required(option.required())
|
||||
.autocomplete(option.autocomplete())
|
||||
.minLength(Double.valueOf(option.range().min()).intValue())
|
||||
.autocomplete(option.autocompletes().length > 0)
|
||||
.minLength((int) option.range().min())
|
||||
.minValue(option.range().min())
|
||||
.maxLength(Double.valueOf(option.range().max()).intValue())
|
||||
.maxLength((int)option.range().max())
|
||||
.maxValue(option.range().max())
|
||||
.choices(choices)
|
||||
.build();
|
||||
|
@@ -1,14 +1,16 @@
|
||||
package net.tomatentum.marinara.wrapper.discord4j;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.UnaryOperator;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import discord4j.core.GatewayDiscordClient;
|
||||
import discord4j.core.event.domain.interaction.ChatInputAutoCompleteEvent;
|
||||
import discord4j.core.event.domain.interaction.InteractionCreateEvent;
|
||||
import discord4j.core.object.command.ApplicationCommandInteractionOption;
|
||||
import discord4j.core.object.command.ApplicationCommandOption.Type;
|
||||
import discord4j.discordjson.json.ApplicationCommandOptionChoiceData;
|
||||
import discord4j.discordjson.json.ApplicationCommandRequest;
|
||||
|
||||
import net.tomatentum.marinara.util.LoggerUtil;
|
||||
@@ -22,13 +24,12 @@ import net.tomatentum.marinara.wrapper.discord4j.identifierconverter.ButtonIdent
|
||||
import net.tomatentum.marinara.wrapper.discord4j.identifierconverter.SlashCommandIdentifierConverter;
|
||||
|
||||
public class Discord4JWrapper extends LibraryWrapper {
|
||||
|
||||
public static final Function<List<ApplicationCommandInteractionOption>, List<ApplicationCommandInteractionOption>> SUB_FILTER = (i) ->
|
||||
public static final UnaryOperator<List<ApplicationCommandInteractionOption>> SUB_FILTER = i ->
|
||||
i.stream()
|
||||
.filter(o -> o.getType().equals(Type.SUB_COMMAND) || o.getType().equals(Type.SUB_COMMAND_GROUP))
|
||||
.toList();
|
||||
|
||||
public static final Function<List<ApplicationCommandInteractionOption>, List<ApplicationCommandInteractionOption>> ARG_FILTER = (i) ->
|
||||
public static final UnaryOperator<List<ApplicationCommandInteractionOption>> ARG_FILTER = i ->
|
||||
i.stream()
|
||||
.filter(o -> !o.getType().equals(Type.SUB_COMMAND) && !o.getType().equals(Type.SUB_COMMAND_GROUP))
|
||||
.toList();
|
||||
@@ -71,4 +72,15 @@ public class Discord4JWrapper extends LibraryWrapper {
|
||||
return this.contextObjectProvider;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void respondAutocomplete(Object context, List<Object> options) {
|
||||
if (context instanceof ChatInputAutoCompleteEvent event) {
|
||||
List<ApplicationCommandOptionChoiceData> choices = options.stream()
|
||||
.filter(ApplicationCommandOptionChoiceData.class::isInstance)
|
||||
.map(o -> (ApplicationCommandOptionChoiceData)o)
|
||||
.toList();
|
||||
event.respondWithSuggestions(choices);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,53 +1,39 @@
|
||||
package net.tomatentum.marinara.wrapper.discord4j.identifierconverter;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import discord4j.core.event.domain.interaction.ChatInputAutoCompleteEvent;
|
||||
import discord4j.core.object.command.ApplicationCommandInteractionOption;
|
||||
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;
|
||||
import net.tomatentum.marinara.wrapper.discord4j.Discord4JWrapper;
|
||||
|
||||
public class AutocompleteIdentifierConverter implements IdentifierProvider.Converter<ChatInputAutoCompleteEvent> {
|
||||
|
||||
@Override
|
||||
public InteractionIdentifier convert(ChatInputAutoCompleteEvent context, InteractionRegistry registry) {
|
||||
public InteractionIdentifier convert(ChatInputAutoCompleteEvent context) {
|
||||
List<ApplicationCommandInteractionOption> options = Discord4JWrapper.SUB_FILTER.apply(context.getOptions());
|
||||
String commandName = context.getCommandName();
|
||||
InteractionIdentifier ident;
|
||||
|
||||
if (!options.isEmpty()) {
|
||||
List<ApplicationCommandInteractionOption> sub_options = Discord4JWrapper.SUB_FILTER.apply(options.getFirst().getOptions());
|
||||
if (!sub_options.isEmpty())
|
||||
ident = InteractionIdentifier.createHierarchy(
|
||||
InteractionType.COMMAND,
|
||||
List<ApplicationCommandInteractionOption> subOptions = Discord4JWrapper.SUB_FILTER.apply(options.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;
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -3,13 +3,12 @@ package net.tomatentum.marinara.wrapper.discord4j.identifierconverter;
|
||||
import discord4j.core.event.domain.interaction.ButtonInteractionEvent;
|
||||
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<ButtonInteractionEvent> {
|
||||
|
||||
@Override
|
||||
public InteractionIdentifier convert(ButtonInteractionEvent context, InteractionRegistry registry) {
|
||||
public InteractionIdentifier convert(ButtonInteractionEvent context) {
|
||||
return InteractionIdentifier.builder().name(context.getCustomId()).type(InteractionType.BUTTON).build();
|
||||
}
|
||||
|
||||
|
@@ -6,14 +6,13 @@ import discord4j.core.event.domain.interaction.ChatInputInteractionEvent;
|
||||
import discord4j.core.object.command.ApplicationCommandInteractionOption;
|
||||
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;
|
||||
import net.tomatentum.marinara.wrapper.discord4j.Discord4JWrapper;
|
||||
|
||||
public class SlashCommandIdentifierConverter implements IdentifierProvider.Converter<ChatInputInteractionEvent> {
|
||||
|
||||
@Override
|
||||
public InteractionIdentifier convert(ChatInputInteractionEvent context, InteractionRegistry registry) {
|
||||
public InteractionIdentifier convert(ChatInputInteractionEvent context) {
|
||||
List<ApplicationCommandInteractionOption> options = Discord4JWrapper.SUB_FILTER.apply(context.getOptions());
|
||||
String commandName = context.getCommandName();
|
||||
|
||||
|
@@ -18,10 +18,10 @@ import net.tomatentum.marinara.Marinara;
|
||||
import net.tomatentum.marinara.wrapper.LibraryWrapper;
|
||||
import net.tomatentum.marinara.wrapper.discord4j.Discord4JWrapper;
|
||||
|
||||
public class AutoCompleteTest {
|
||||
class AutoCompleteTest {
|
||||
|
||||
@Test
|
||||
public void testAutocomplete() {
|
||||
void testAutocomplete() {
|
||||
ApplicationCommandInteractionOption optionMock = mock();
|
||||
ChatInputAutoCompleteEvent autoCompleteEventMock = mock();
|
||||
|
||||
|
@@ -6,23 +6,35 @@ import java.util.Collections;
|
||||
|
||||
import discord4j.core.event.domain.interaction.ChatInputAutoCompleteEvent;
|
||||
import discord4j.core.event.domain.interaction.ChatInputInteractionEvent;
|
||||
import discord4j.discordjson.json.ApplicationCommandOptionChoiceData;
|
||||
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(ChatInputInteractionEvent context) {
|
||||
|
||||
}
|
||||
|
||||
@AutoComplete("testAuto")
|
||||
public void autocomplete(ChatInputAutoCompleteEvent context, String value) {
|
||||
public ApplicationCommandOptionChoiceData[] autocomplete(ChatInputAutoCompleteEvent context, String value) {
|
||||
System.out.println("Success!");
|
||||
assertEquals(value, "test");
|
||||
context.respondWithSuggestions(Collections.emptyList());
|
||||
assertEquals("test", value);
|
||||
return new ApplicationCommandOptionChoiceData[]{
|
||||
ApplicationCommandOptionChoiceData.builder().name("TestValue").value("test").build()
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user