1
1
package org .togetherjava .tjbot .commands .tags ;
2
2
3
3
import net .dv8tion .jda .api .EmbedBuilder ;
4
+ import net .dv8tion .jda .api .events .interaction .command .CommandAutoCompleteInteractionEvent ;
4
5
import net .dv8tion .jda .api .events .interaction .command .SlashCommandInteractionEvent ;
6
+ import net .dv8tion .jda .api .interactions .AutoCompleteQuery ;
7
+ import net .dv8tion .jda .api .interactions .commands .Command ;
5
8
import net .dv8tion .jda .api .interactions .commands .OptionMapping ;
6
9
import net .dv8tion .jda .api .interactions .commands .OptionType ;
10
+ import net .dv8tion .jda .api .interactions .commands .build .OptionData ;
7
11
import net .dv8tion .jda .api .requests .restaction .interactions .ReplyCallbackAction ;
8
12
9
13
import org .togetherjava .tjbot .commands .CommandVisibility ;
10
14
import org .togetherjava .tjbot .commands .SlashCommandAdapter ;
15
+ import org .togetherjava .tjbot .commands .utils .StringDistances ;
11
16
12
17
import java .time .Instant ;
13
- import java .util .Objects ;
18
+ import java .util .Collection ;
14
19
15
20
/**
16
21
* Implements the {@code /tag} command which lets the bot respond content of a tag that has been
21
26
*/
22
27
public final class TagCommand extends SlashCommandAdapter {
23
28
private final TagSystem tagSystem ;
24
-
29
+ private static final int MAX_SUGGESTIONS = 5 ;
25
30
static final String ID_OPTION = "id" ;
26
31
static final String REPLY_TO_USER_OPTION = "reply-to" ;
27
32
@@ -35,16 +40,16 @@ public TagCommand(TagSystem tagSystem) {
35
40
36
41
this .tagSystem = tagSystem ;
37
42
38
- // TODO Think about adding an ephemeral selection menu with pagination support
39
- // if the user calls this without id or similar
40
- getData (). addOption ( OptionType . STRING , ID_OPTION , "The id of the tag to display" , true )
41
- . addOption (OptionType .USER , REPLY_TO_USER_OPTION ,
42
- "Optionally, the user who you want to reply to" , false );
43
+ getData (). addOptions (
44
+ new OptionData ( OptionType . STRING , ID_OPTION , "The id of the tag to display" , true ,
45
+ true ),
46
+ new OptionData (OptionType .USER , REPLY_TO_USER_OPTION ,
47
+ "Optionally, the user who you want to reply to" , false ) );
43
48
}
44
49
45
50
@ Override
46
51
public void onSlashCommand (SlashCommandInteractionEvent event ) {
47
- String id = Objects . requireNonNull ( event .getOption (ID_OPTION ) ).getAsString ();
52
+ String id = event .getOption (ID_OPTION ).getAsString ();
48
53
OptionMapping replyToUserOption = event .getOption (REPLY_TO_USER_OPTION );
49
54
50
55
if (tagSystem .handleIsUnknownTag (id , event )) {
@@ -63,4 +68,22 @@ public void onSlashCommand(SlashCommandInteractionEvent event) {
63
68
}
64
69
message .queue ();
65
70
}
71
+
72
+ @ Override
73
+ public void onAutoComplete (CommandAutoCompleteInteractionEvent event ) {
74
+ AutoCompleteQuery focusedOption = event .getFocusedOption ();
75
+
76
+ if (!focusedOption .getName ().equals (ID_OPTION )) {
77
+ throw new IllegalArgumentException (
78
+ "Unexpected option, was: " + focusedOption .getName ());
79
+ }
80
+
81
+ Collection <Command .Choice > choices = StringDistances
82
+ .closeMatches (focusedOption .getValue (), tagSystem .getAllIds (), MAX_SUGGESTIONS )
83
+ .stream ()
84
+ .map (id -> new Command .Choice (id , id ))
85
+ .toList ();
86
+
87
+ event .replyChoices (choices ).queue ();
88
+ }
66
89
}
0 commit comments