From ef1ac80707e2737a1565ebc517c5ce3f078f8e46 Mon Sep 17 00:00:00 2001 From: granny Date: Sun, 1 Sep 2024 01:40:56 -0700 Subject: [PATCH 1/6] add autocomplete support to docker commands --- .../marina/feature/docker/DockerFeature.java | 37 ++++++++++++++++++- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/src/main/java/me/scarsz/marina/feature/docker/DockerFeature.java b/src/main/java/me/scarsz/marina/feature/docker/DockerFeature.java index 70d3b0d..e936aef 100644 --- a/src/main/java/me/scarsz/marina/feature/docker/DockerFeature.java +++ b/src/main/java/me/scarsz/marina/feature/docker/DockerFeature.java @@ -14,7 +14,9 @@ import me.scarsz.marina.exception.InsufficientPermissionException; import me.scarsz.marina.feature.AbstractFeature; import net.dv8tion.jda.api.entities.ISnowflake; +import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent; import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; +import net.dv8tion.jda.api.interactions.AutoCompleteQuery; import net.dv8tion.jda.api.interactions.commands.OptionMapping; import net.dv8tion.jda.api.interactions.commands.OptionType; import net.dv8tion.jda.api.interactions.commands.build.SubcommandData; @@ -29,6 +31,8 @@ import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; +import static net.dv8tion.jda.api.interactions.commands.Command.Choice; + public class DockerFeature extends AbstractFeature { private final DockerClient dockerClient; @@ -42,10 +46,10 @@ public DockerFeature() { .addOption(OptionType.STRING, "name", "Name to filter containers by") ) .addSubcommands(new SubcommandData("restart", "Restart a container") - .addOption(OptionType.STRING, "container", "Container name to restart") + .addOption(OptionType.STRING, "container", "Container name to restart", false, true) ) .addSubcommands(new SubcommandData("update", "Update a container") - .addOption(OptionType.STRING, "container", "Container name to update", true) + .addOption(OptionType.STRING, "container", "Container name to update", true, true) ) .queue(); } @@ -138,6 +142,35 @@ public void onComplete() { }); } + @Override + public void onCommandAutoCompleteInteraction(CommandAutoCompleteInteractionEvent event) { + String[] commandPath = event.getFullCommandName().split(" "); + AutoCompleteQuery focusedOption = event.getFocusedOption(); + + if (!"container".equals(commandPath[0])) { + return; // not under the "container" command + } + + if (!"update".equals(commandPath[1])) { + return; // not the "update" subcommand + } + + if (!"restart".equals(commandPath[1])) { + return; // not the "restart" subcommand + } + + if (!"container".equals(focusedOption)) { + return; // not the "container" option + } + + List options = listContainers(null, event.getUser()).stream() + .map(this::getContainerName) + .filter(word -> word.startsWith(focusedOption.getValue())) // only display words that start with the user's current input + .map(word -> new Choice(word, word)) // map the words to choices + .collect(Collectors.toList()); + event.replyChoices(options).queue(); + } + // @Override // public void onSelectionMenu(@NotNull SelectionMenuEvent event) { // for (String value : event.getValues()) { From bf70d7188c3548c939cd727e40bedb2503b62414 Mon Sep 17 00:00:00 2001 From: granny Date: Sun, 1 Sep 2024 01:49:38 -0700 Subject: [PATCH 2/6] use dockerjava's name filter for filtering --- .../java/me/scarsz/marina/feature/docker/DockerFeature.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/me/scarsz/marina/feature/docker/DockerFeature.java b/src/main/java/me/scarsz/marina/feature/docker/DockerFeature.java index e936aef..7470f0b 100644 --- a/src/main/java/me/scarsz/marina/feature/docker/DockerFeature.java +++ b/src/main/java/me/scarsz/marina/feature/docker/DockerFeature.java @@ -163,9 +163,8 @@ public void onCommandAutoCompleteInteraction(CommandAutoCompleteInteractionEvent return; // not the "container" option } - List options = listContainers(null, event.getUser()).stream() + List options = listContainers(focusedOption.getValue(), event.getUser()).stream() .map(this::getContainerName) - .filter(word -> word.startsWith(focusedOption.getValue())) // only display words that start with the user's current input .map(word -> new Choice(word, word)) // map the words to choices .collect(Collectors.toList()); event.replyChoices(options).queue(); From 45d0d1d30744a60a4c56e6cab974bce550c11548 Mon Sep 17 00:00:00 2001 From: granny Date: Sun, 1 Sep 2024 01:52:29 -0700 Subject: [PATCH 3/6] properly check option value --- .../java/me/scarsz/marina/feature/docker/DockerFeature.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/me/scarsz/marina/feature/docker/DockerFeature.java b/src/main/java/me/scarsz/marina/feature/docker/DockerFeature.java index 7470f0b..420a50a 100644 --- a/src/main/java/me/scarsz/marina/feature/docker/DockerFeature.java +++ b/src/main/java/me/scarsz/marina/feature/docker/DockerFeature.java @@ -145,7 +145,7 @@ public void onComplete() { @Override public void onCommandAutoCompleteInteraction(CommandAutoCompleteInteractionEvent event) { String[] commandPath = event.getFullCommandName().split(" "); - AutoCompleteQuery focusedOption = event.getFocusedOption(); + String option = event.getFocusedOption().getValue(); if (!"container".equals(commandPath[0])) { return; // not under the "container" command @@ -159,11 +159,11 @@ public void onCommandAutoCompleteInteraction(CommandAutoCompleteInteractionEvent return; // not the "restart" subcommand } - if (!"container".equals(focusedOption)) { + if (!"container".equals(option)) { return; // not the "container" option } - List options = listContainers(focusedOption.getValue(), event.getUser()).stream() + List options = listContainers(option, event.getUser()).stream() .map(this::getContainerName) .map(word -> new Choice(word, word)) // map the words to choices .collect(Collectors.toList()); From 16b68393a89bbe215f08a1463587e40537a54c5e Mon Sep 17 00:00:00 2001 From: granny Date: Sun, 1 Sep 2024 01:52:53 -0700 Subject: [PATCH 4/6] remove unused import --- src/main/java/me/scarsz/marina/feature/docker/DockerFeature.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/me/scarsz/marina/feature/docker/DockerFeature.java b/src/main/java/me/scarsz/marina/feature/docker/DockerFeature.java index 420a50a..bbc1c32 100644 --- a/src/main/java/me/scarsz/marina/feature/docker/DockerFeature.java +++ b/src/main/java/me/scarsz/marina/feature/docker/DockerFeature.java @@ -16,7 +16,6 @@ import net.dv8tion.jda.api.entities.ISnowflake; import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent; import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; -import net.dv8tion.jda.api.interactions.AutoCompleteQuery; import net.dv8tion.jda.api.interactions.commands.OptionMapping; import net.dv8tion.jda.api.interactions.commands.OptionType; import net.dv8tion.jda.api.interactions.commands.build.SubcommandData; From 6a8dd39d2bc959fad8f44d0faef92af48ab00e75 Mon Sep 17 00:00:00 2001 From: granny Date: Sun, 1 Sep 2024 01:58:40 -0700 Subject: [PATCH 5/6] i need to pay attention more and commit less --- .../me/scarsz/marina/feature/docker/DockerFeature.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/main/java/me/scarsz/marina/feature/docker/DockerFeature.java b/src/main/java/me/scarsz/marina/feature/docker/DockerFeature.java index bbc1c32..123a94e 100644 --- a/src/main/java/me/scarsz/marina/feature/docker/DockerFeature.java +++ b/src/main/java/me/scarsz/marina/feature/docker/DockerFeature.java @@ -16,6 +16,7 @@ import net.dv8tion.jda.api.entities.ISnowflake; import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent; import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; +import net.dv8tion.jda.api.interactions.AutoCompleteQuery; import net.dv8tion.jda.api.interactions.commands.OptionMapping; import net.dv8tion.jda.api.interactions.commands.OptionType; import net.dv8tion.jda.api.interactions.commands.build.SubcommandData; @@ -144,7 +145,7 @@ public void onComplete() { @Override public void onCommandAutoCompleteInteraction(CommandAutoCompleteInteractionEvent event) { String[] commandPath = event.getFullCommandName().split(" "); - String option = event.getFocusedOption().getValue(); + AutoCompleteQuery focusedOption = event.getFocusedOption(); if (!"container".equals(commandPath[0])) { return; // not under the "container" command @@ -158,11 +159,12 @@ public void onCommandAutoCompleteInteraction(CommandAutoCompleteInteractionEvent return; // not the "restart" subcommand } - if (!"container".equals(option)) { + if (!"container".equals(focusedOption.getName())) { return; // not the "container" option } - List options = listContainers(option, event.getUser()).stream() + String optionValue = focusedOption.getValue().isBlank() ? null : focusedOption.getValue(); + List options = listContainers(optionValue, event.getUser()).stream() .map(this::getContainerName) .map(word -> new Choice(word, word)) // map the words to choices .collect(Collectors.toList()); From 1315b48e9602a34b208cebad14d130e38d441647 Mon Sep 17 00:00:00 2001 From: granny Date: Sun, 1 Sep 2024 08:56:20 -0700 Subject: [PATCH 6/6] fix subcommand name check --- .../me/scarsz/marina/feature/docker/DockerFeature.java | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/main/java/me/scarsz/marina/feature/docker/DockerFeature.java b/src/main/java/me/scarsz/marina/feature/docker/DockerFeature.java index 123a94e..24d9170 100644 --- a/src/main/java/me/scarsz/marina/feature/docker/DockerFeature.java +++ b/src/main/java/me/scarsz/marina/feature/docker/DockerFeature.java @@ -151,12 +151,8 @@ public void onCommandAutoCompleteInteraction(CommandAutoCompleteInteractionEvent return; // not under the "container" command } - if (!"update".equals(commandPath[1])) { - return; // not the "update" subcommand - } - - if (!"restart".equals(commandPath[1])) { - return; // not the "restart" subcommand + if (!("update".equals(commandPath[1]) || "restart".equals(commandPath[1]))) { + return; // not the "update" or "restart" subcommands } if (!"container".equals(focusedOption.getName())) {