Skip to content

Commit ddabd5e

Browse files
authored
Fixed /github-search command failing in some cases (#1435)
* fixed github command * CR firas
1 parent 5d819b3 commit ddabd5e

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

application/src/main/java/org/togetherjava/tjbot/features/github/GitHubCommand.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

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

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

4850
private final GitHubReference reference;
4951

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

103+
event.deferReply().queue();
104+
InteractionHook hook = event.getHook();
105+
101106
reference.findIssue(issueId, targetIssueTitle)
102-
.ifPresentOrElse(issue -> event.replyEmbeds(reference.generateReply(issue)).queue(),
103-
() -> event.reply("Could not find the issue you are looking for.")
104-
.setEphemeral(true)
107+
.ifPresentOrElse(
108+
issue -> hook.editOriginalEmbeds(reference.generateReply(issue)).queue(),
109+
() -> hook.editOriginal("Could not find the issue you are looking for.")
105110
.queue());
106111
}
107112

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

117+
List<String> choices;
112118
if (title.isEmpty()) {
113-
event.replyChoiceStrings(autocompleteGHIssueCache.stream().limit(25).toList()).queue();
119+
choices = autocompleteGHIssueCache.stream().limit(MAX_SUGGESTED_CHOICES).toList();
114120
} else {
115121
Queue<String> closestSuggestions =
116122
new PriorityQueue<>(Comparator.comparingInt(suggestionScorer(title)));
117-
118123
closestSuggestions.addAll(autocompleteGHIssueCache);
124+
choices =
125+
Stream.generate(closestSuggestions::poll).limit(MAX_SUGGESTED_CHOICES).toList();
126+
}
119127

120-
List<String> choices = Stream.generate(closestSuggestions::poll).limit(25).toList();
121-
event.replyChoiceStrings(choices).queue();
128+
if (choices.isEmpty()) {
129+
choices = List.of("No issues found");
122130
}
131+
event.replyChoiceStrings(choices).queue();
123132

124133
if (isCacheExpired()) {
125134
updateCache();

0 commit comments

Comments
 (0)