1
1
package net .javadiscord .javabot .systems .user_preferences .commands ;
2
2
3
+ import com .dynxsty .dih4jda .interactions .commands .AutoCompletable ;
3
4
import com .dynxsty .dih4jda .interactions .commands .SlashCommand ;
5
+ import com .dynxsty .dih4jda .util .AutoCompleteUtils ;
6
+ import net .dv8tion .jda .api .events .interaction .command .CommandAutoCompleteInteractionEvent ;
4
7
import net .dv8tion .jda .api .events .interaction .command .SlashCommandInteractionEvent ;
8
+ import net .dv8tion .jda .api .interactions .AutoCompleteQuery ;
5
9
import net .dv8tion .jda .api .interactions .commands .Command ;
6
10
import net .dv8tion .jda .api .interactions .commands .OptionMapping ;
7
11
import net .dv8tion .jda .api .interactions .commands .OptionType ;
15
19
import org .jetbrains .annotations .NotNull ;
16
20
17
21
import java .util .Arrays ;
22
+ import java .util .List ;
18
23
19
24
/**
20
25
* <h3>This class represents the /preferences set command.</h3>
21
26
*/
22
- public class PreferencesSetSubcommand extends SlashCommand .Subcommand {
27
+ public class PreferencesSetSubcommand extends SlashCommand .Subcommand implements AutoCompletable {
23
28
/**
24
29
* The constructor of this class, which sets the corresponding {@link net.dv8tion.jda.api.interactions.commands.build.SlashCommandData}.
25
30
*/
@@ -28,7 +33,7 @@ public PreferencesSetSubcommand() {
28
33
.addOptions (
29
34
new OptionData (OptionType .INTEGER , "preference" , "The preference to set." , true )
30
35
.addChoices (Arrays .stream (Preference .values ()).map (this ::toChoice ).toList ()),
31
- new OptionData (OptionType .BOOLEAN , "state" , "The state of the specified preference." , true )
36
+ new OptionData (OptionType .STRING , "state" , "The state/value of the specified preference." , true , true )
32
37
)
33
38
);
34
39
}
@@ -42,17 +47,34 @@ public void execute(@NotNull SlashCommandInteractionEvent event) {
42
47
return ;
43
48
}
44
49
Preference preference = Preference .values ()[preferenceMapping .getAsInt ()];
45
- boolean state = stateMapping .getAsBoolean ();
50
+ String state = stateMapping .getAsString ();
51
+ if (Arrays .stream (preference .getType ().getAllowedChoices ()).noneMatch (s -> s .equals (state ))) {
52
+ Responses .error (event , "`%s` is not allowed for this preference! Expected one of the following values:\n %s" ,
53
+ state , String .join (", " , preference .getType ().getAllowedChoices ())
54
+ ).queue ();
55
+ return ;
56
+ }
46
57
UserPreferenceService manager = new UserPreferenceService (Bot .getDataSource ());
47
58
if (manager .setOrCreate (event .getUser ().getIdLong (), preference , state )) {
48
- Responses .info (event , "Preference Updated" , "Successfully %s `%s`!" , state ? "enabled" : "disabled" , preference ).queue ();
59
+ Responses .info (event , "Preference Updated" , "Successfully set %s to `%s`!" , preference , state ).queue ();
49
60
} else {
50
- Responses .error (event , "Could not %s `%s`." , state ? "enable" : "disable" , preference ).queue ();
61
+ Responses .error (event , "Could not set %s to `%s`." , preference , state ).queue ();
51
62
}
52
63
}
53
64
54
65
@ Contract ("_ -> new" )
55
66
private Command .@ NotNull Choice toChoice (@ NotNull Preference preference ) {
56
67
return new Command .Choice (preference .toString (), String .valueOf (preference .ordinal ()));
57
68
}
69
+
70
+ @ Override
71
+ public void handleAutoComplete (@ NotNull CommandAutoCompleteInteractionEvent event , @ NotNull AutoCompleteQuery target ) {
72
+ String preferenceString = event .getOption ("preference" , OptionMapping ::getAsString );
73
+ if (preferenceString != null && Arrays .stream (Preference .values ()).map (Preference ::name ).anyMatch (c -> c .equals (preferenceString ))) {
74
+ Preference preference = Preference .valueOf (preferenceString );
75
+ if (preference .getType ().getDefaultChoices () != null && preference .getType ().getDefaultChoices ().length > 0 ) {
76
+ event .replyChoices (AutoCompleteUtils .filterChoices (event , List .of (preference .getType ().getDefaultChoices ()))).queue ();
77
+ }
78
+ }
79
+ }
58
80
}
0 commit comments