|
1 | 1 | package net.javadiscord.javabot.systems.configuration; |
2 | 2 |
|
3 | 3 | import xyz.dynxsty.dih4jda.interactions.commands.application.SlashCommand; |
| 4 | +import xyz.dynxsty.dih4jda.util.AutoCompleteUtils; |
| 5 | +import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent; |
4 | 6 | import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; |
| 7 | +import net.dv8tion.jda.api.interactions.commands.Command; |
| 8 | +import net.dv8tion.jda.api.interactions.commands.Command.Choice; |
5 | 9 | import net.dv8tion.jda.api.requests.restaction.interactions.InteractionCallbackAction; |
6 | 10 | import net.javadiscord.javabot.data.config.BotConfig; |
7 | 11 | import net.javadiscord.javabot.data.config.GuildConfig; |
|
10 | 14 | import net.javadiscord.javabot.util.Responses; |
11 | 15 | import org.jetbrains.annotations.NotNull; |
12 | 16 |
|
| 17 | +import java.lang.reflect.Field; |
| 18 | +import java.lang.reflect.Modifier; |
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.List; |
| 21 | +import java.util.stream.Collectors; |
| 22 | + |
13 | 23 | import javax.annotation.Nonnull; |
14 | 24 |
|
15 | 25 | /** |
@@ -45,4 +55,46 @@ public void execute(@NotNull SlashCommandInteractionEvent event) { |
45 | 55 | } |
46 | 56 |
|
47 | 57 | protected abstract InteractionCallbackAction<?> handleConfigSubcommand(@Nonnull SlashCommandInteractionEvent event, @Nonnull GuildConfig config) throws UnknownPropertyException; |
| 58 | + |
| 59 | + /** |
| 60 | + * autocompletes a property. |
| 61 | + * @param event the {@link CommandAutoCompleteInteractionEvent} used for sending the autocomplete information |
| 62 | + * @param partialText the entered text |
| 63 | + */ |
| 64 | + protected void handlePropertyAutocomplete(CommandAutoCompleteInteractionEvent event, String partialText) { |
| 65 | + int lastDot = partialText.lastIndexOf('.'); |
| 66 | + String parentPropertyName; |
| 67 | + String childPropertyName; |
| 68 | + if (lastDot == -1) { |
| 69 | + parentPropertyName = ""; |
| 70 | + childPropertyName = partialText; |
| 71 | + } else { |
| 72 | + parentPropertyName = partialText.substring(0,lastDot); |
| 73 | + childPropertyName = partialText.substring(lastDot+1); |
| 74 | + } |
| 75 | + |
| 76 | + GuildConfig guildConfig = botConfig.get(event.getGuild()); |
| 77 | + try { |
| 78 | + Object resolved; |
| 79 | + if(parentPropertyName.isEmpty()) { |
| 80 | + resolved = guildConfig; |
| 81 | + } else { |
| 82 | + resolved = guildConfig.resolve(parentPropertyName); |
| 83 | + } |
| 84 | + if (resolved == null || !resolved.getClass().getPackageName().startsWith(GuildConfig.class.getPackageName())) { |
| 85 | + event.replyChoices().queue(); |
| 86 | + return; |
| 87 | + } |
| 88 | + List<Choice> choices = Arrays.stream(resolved.getClass().getDeclaredFields()) |
| 89 | + .filter(f -> !Modifier.isTransient(f.getModifiers())) |
| 90 | + .map(Field::getName) |
| 91 | + .map(name -> parentPropertyName.isEmpty() ? name : parentPropertyName+"."+name) |
| 92 | + .map(name -> new Command.Choice(name, name)) |
| 93 | + .collect(Collectors.toList()); |
| 94 | + |
| 95 | + event.replyChoices(AutoCompleteUtils.filterChoices(childPropertyName, choices)).queue(); |
| 96 | + } catch (UnknownPropertyException e) { |
| 97 | + event.replyChoices().queue(); |
| 98 | + } |
| 99 | + } |
48 | 100 | } |
0 commit comments