Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions src/main/java/me/scarsz/marina/feature/docker/DockerFeature.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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();
}
Expand Down Expand Up @@ -138,6 +142,31 @@ 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]) || "restart".equals(commandPath[1]))) {
return; // not the "update" or "restart" subcommands
}

if (!"container".equals(focusedOption.getName())) {
return; // not the "container" option
}

String optionValue = focusedOption.getValue().isBlank() ? null : focusedOption.getValue();
List<Choice> options = listContainers(optionValue, event.getUser()).stream()
.map(this::getContainerName)
.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()) {
Expand Down