1
1
/*
2
- * Copyright (c) 2020-2022 GeyserMC. http://geysermc.org
2
+ * Copyright (c) 2020-2024 GeyserMC. http://geysermc.org
3
3
*
4
4
* Permission is hereby granted, free of charge, to any person obtaining a copy
5
5
* of this software and associated documentation files (the "Software"), to deal
28
28
import com .jagrosh .jdautilities .command .SlashCommand ;
29
29
import com .jagrosh .jdautilities .command .SlashCommandEvent ;
30
30
import net .dv8tion .jda .api .EmbedBuilder ;
31
+ import net .dv8tion .jda .api .events .interaction .command .CommandAutoCompleteInteractionEvent ;
32
+ import net .dv8tion .jda .api .interactions .commands .Command ;
31
33
import net .dv8tion .jda .api .interactions .commands .OptionType ;
32
34
import net .dv8tion .jda .api .interactions .commands .build .OptionData ;
33
35
import org .geysermc .discordbot .tags .SlashTag ;
34
36
import org .geysermc .discordbot .tags .TagsManager ;
35
37
import org .geysermc .discordbot .util .BotColors ;
38
+ import org .geysermc .discordbot .util .DicesCoefficient ;
36
39
40
+ import java .util .ArrayList ;
37
41
import java .util .Collections ;
42
+ import java .util .List ;
38
43
39
44
public class TagCommand extends SlashCommand {
40
45
@@ -46,6 +51,7 @@ public TagCommand() {
46
51
47
52
this .options = Collections .singletonList (
48
53
new OptionData (OptionType .STRING , "name" , "The tag to get (Supports aliases)" , true )
54
+ .setAutoComplete (true )
49
55
);
50
56
}
51
57
@@ -79,7 +85,60 @@ protected void execute(SlashCommandEvent event) {
79
85
.setDescription ("Missing requested tag" )
80
86
.build ()).queue ();
81
87
}
88
+ }
89
+
90
+ @ Override
91
+ public void onAutoComplete (CommandAutoCompleteInteractionEvent event ) {
92
+ // Get the query
93
+ String query = event .getFocusedOption ().getValue ();
94
+
95
+ // Get the providers
96
+ List <String > tags = potentialTags (query );
97
+
98
+ event .replyChoices (tags .stream ()
99
+ .distinct ()
100
+ .map (tag -> new Command .Choice (tag , tag ))
101
+ .limit (25 )
102
+ .toArray (Command .Choice []::new ))
103
+ .queue ();
104
+ }
105
+
106
+ List <String > potentialTags (String query ) {
107
+ List <String > potential = new ArrayList <>();
108
+
109
+ for (SlashTag slashTag : TagsManager .getEmbedTags ()) {
110
+ // Check if the name starts with the query
111
+ if (slashTag .getName ().toLowerCase ().startsWith (query .toLowerCase ())) {
112
+ potential .add (slashTag .getName ());
113
+ continue ;
114
+ }
115
+
116
+ // Check if the name is similar
117
+ double similar = DicesCoefficient .diceCoefficientOptimized (query .toLowerCase (), slashTag .getName ().toLowerCase ());
118
+ if (similar > 0.2d ) {
119
+ potential .add (slashTag .getName ());
120
+ continue ;
121
+ }
122
+
123
+ // Check the aliases
124
+ if (slashTag .getAliases () != null && !slashTag .getAliases ().isEmpty ()) {
125
+ for (String alias : slashTag .getAliases ().split ("," )) {
126
+ // Check if the alias starts with the query
127
+ if (alias .toLowerCase ().startsWith (query .toLowerCase ())) {
128
+ potential .add (alias );
129
+ break ;
130
+ }
82
131
132
+ // Check if the alias is similar
133
+ double aliasSimilar = DicesCoefficient .diceCoefficientOptimized (query .toLowerCase (), alias .toLowerCase ());
134
+ if (aliasSimilar > 0.2d ) {
135
+ potential .add (alias );
136
+ break ;
137
+ }
138
+ }
139
+ }
140
+ }
83
141
142
+ return potential ;
84
143
}
85
144
}
0 commit comments