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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.interactions.InteractionHook;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import org.kohsuke.github.GHIssue;
import org.slf4j.Logger;
Expand Down Expand Up @@ -44,6 +45,7 @@ public final class GitHubCommand extends SlashCommandAdapter {

private static final String TITLE_OPTION = "title";
private static final Logger logger = LoggerFactory.getLogger(GitHubCommand.class);
private static final int MAX_SUGGESTED_CHOICES = 25;

private final GitHubReference reference;

Expand Down Expand Up @@ -98,28 +100,35 @@ public void onSlashCommand(SlashCommandInteractionEvent event) {
String[] issueData = titleOption.split(" ", 2);
String targetIssueTitle = issueData[1];

event.deferReply().queue();
InteractionHook hook = event.getHook();

reference.findIssue(issueId, targetIssueTitle)
.ifPresentOrElse(issue -> event.replyEmbeds(reference.generateReply(issue)).queue(),
() -> event.reply("Could not find the issue you are looking for.")
.setEphemeral(true)
.ifPresentOrElse(
issue -> hook.editOriginalEmbeds(reference.generateReply(issue)).queue(),
() -> hook.editOriginal("Could not find the issue you are looking for.")
.queue());
}

@Override
public void onAutoComplete(CommandAutoCompleteInteractionEvent event) {
String title = event.getOption(TITLE_OPTION).getAsString();

List<String> choices;
if (title.isEmpty()) {
event.replyChoiceStrings(autocompleteGHIssueCache.stream().limit(25).toList()).queue();
choices = autocompleteGHIssueCache.stream().limit(MAX_SUGGESTED_CHOICES).toList();
} else {
Queue<String> closestSuggestions =
new PriorityQueue<>(Comparator.comparingInt(suggestionScorer(title)));

closestSuggestions.addAll(autocompleteGHIssueCache);
choices =
Stream.generate(closestSuggestions::poll).limit(MAX_SUGGESTED_CHOICES).toList();
}

List<String> choices = Stream.generate(closestSuggestions::poll).limit(25).toList();
event.replyChoiceStrings(choices).queue();
if (choices.isEmpty()) {
choices = List.of("no issue found");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this expression is supposed to be shown on UI, I believe it should start with Uppercase

}
event.replyChoiceStrings(choices).queue();

if (isCacheExpired()) {
updateCache();
Expand Down
Loading