Skip to content

Commit

Permalink
feat(common): add option to map suggestion providers (#48)
Browse files Browse the repository at this point in the history
Co-authored-by: Alexander Söderberg <alexander.soderberg@incendo.org>
  • Loading branch information
BlueTree242 and Citymonstret authored May 31, 2024
1 parent 9fe7d4e commit f763bb4
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
//
package org.incendo.cloud.discord.slash;

import java.util.function.Function;
import org.apiguardian.api.API;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.incendo.cloud.internal.CommandNode;
import org.incendo.cloud.suggestion.SuggestionProvider;

@API(status = API.Status.STABLE, since = "1.0.0")
public interface DiscordCommandFactory<C> {
Expand All @@ -37,4 +39,25 @@ public interface DiscordCommandFactory<C> {
* @return the option
*/
@NonNull DiscordCommand<C> create(@NonNull CommandNode<C> node);

/**
* Sets the suggestion registration mapper.
*
* <p>The suggestion registration mapper determines how {@link SuggestionProvider}s are handled during registration of the
* command. {@link DiscordChoices} can be returned to use native Discord choices, or
* {@link SuggestionProvider#noSuggestions()} can be returned to disable auto-complete for the argument.</p>
*
* @param suggestionRegistrationMapper The function to map suggestion providers to other suggestion provider during
* registration of the command to discord.
*/
void suggestionRegistrationMapper(@NonNull Function<SuggestionProvider<C>,
SuggestionProvider<C>> suggestionRegistrationMapper);

/**
* Returns the current suggestion registration mapper.
*
* @see DiscordCommandFactory#suggestionRegistrationMapper(Function)
* @return The current set suggestion registration mapper.
*/
@NonNull Function<SuggestionProvider<C>, SuggestionProvider<C>> suggestionRegistrationMapper();
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.apiguardian.api.API;
import org.checkerframework.checker.nullness.qual.NonNull;
Expand All @@ -58,6 +59,8 @@ public class StandardDiscordCommandFactory<C> implements DiscordCommandFactory<C
private final OptionRegistry<C> optionRegistry;
private final Map<Class<?>, RangeMapper<C, ?, ?>> rangeMappers = new HashMap<>();

private Function<SuggestionProvider<C>, SuggestionProvider<C>> suggestionRegistrationMapper = provider -> provider;

/**
* Creates a new factory instance.
*
Expand Down Expand Up @@ -147,6 +150,18 @@ public <T extends Number, P extends ArgumentParser<C, T>> void registerRangeMapp
.build();
}

@Override
public void suggestionRegistrationMapper(
final @NonNull Function<SuggestionProvider<C>, SuggestionProvider<C>> suggestionRegistrationMapper
) {
this.suggestionRegistrationMapper = Objects.requireNonNull(suggestionRegistrationMapper);
}

@Override
public @NonNull Function<SuggestionProvider<C>, SuggestionProvider<C>> suggestionRegistrationMapper() {
return this.suggestionRegistrationMapper;
}

@SuppressWarnings({"rawtypes", "unchecked"})
private @NonNull List<DiscordOption<C>> createOptions(final @NonNull CommandNode<C> node) {
final CommandComponent<C> component = node.component();
Expand Down Expand Up @@ -201,13 +216,17 @@ public <T extends Number, P extends ArgumentParser<C, T>> void registerRangeMapp

return components.stream()
.map(innerComponent -> {
final SuggestionProvider<C> suggestionProvider = this.suggestionRegistrationMapper.apply(
innerComponent.suggestionProvider()
);
final DiscordOptionType optionType = this.optionRegistry.getOption(innerComponent.valueType());
final Collection choices = this.extractChoices(innerComponent.suggestionProvider());
final Collection choices = this.extractChoices(suggestionProvider);
final Range<?> range = this.extractRange(innerComponent.parser());

final boolean autoComplete;
if (choices.isEmpty()) {
autoComplete = DiscordOptionType.AUTOCOMPLETE.contains(optionType);
autoComplete = DiscordOptionType.AUTOCOMPLETE.contains(optionType)
&& !suggestionProvider.equals(SuggestionProvider.noSuggestions());
} else {
autoComplete = false;
}
Expand Down

0 comments on commit f763bb4

Please sign in to comment.