11
22package net .javadiscord .javabot .systems .user_preferences .commands ;
33
4- import com .dynxsty .dih4jda .interactions .commands .AutoCompletable ;
54import com .dynxsty .dih4jda .interactions .commands .SlashCommand ;
6- import com .dynxsty .dih4jda .util .AutoCompleteUtils ;
7- import net .dv8tion .jda .api .events .interaction .command .CommandAutoCompleteInteractionEvent ;
85import net .dv8tion .jda .api .events .interaction .command .SlashCommandInteractionEvent ;
9- import net .dv8tion .jda .api .interactions .AutoCompleteQuery ;
10- import net .dv8tion .jda .api .interactions .commands .Command ;
116import net .dv8tion .jda .api .interactions .commands .OptionMapping ;
127import net .dv8tion .jda .api .interactions .commands .OptionType ;
8+ import net .dv8tion .jda .api .interactions .commands .build .OptionData ;
139import net .dv8tion .jda .api .interactions .commands .build .SubcommandData ;
1410import net .javadiscord .javabot .Bot ;
1511import net .javadiscord .javabot .systems .user_preferences .UserPreferenceManager ;
1612import net .javadiscord .javabot .systems .user_preferences .model .Preference ;
1713import net .javadiscord .javabot .util .Responses ;
1814import org .jetbrains .annotations .NotNull ;
1915
20- import java .util .ArrayList ;
21- import java .util .List ;
16+ import java .util .Arrays ;
2217
2318/**
2419 * <h3>This class represents the /preferences set command.</h3>
2520 */
26- public class PreferencesSetSubcommand extends SlashCommand .Subcommand implements AutoCompletable {
21+ public class PreferencesSetSubcommand extends SlashCommand .Subcommand {
2722 /**
2823 * The constructor of this class, which sets the corresponding {@link net.dv8tion.jda.api.interactions.commands.build.SlashCommandData}.
2924 */
3025 public PreferencesSetSubcommand () {
3126 setSubcommandData (new SubcommandData ("set" , "Allows you to set your preferences!" )
32- .addOption (OptionType .INTEGER , "preference" , "The preference to set." , true , true )
33- .addOption (OptionType .BOOLEAN , "state" , "The state of the specified preference." , true )
27+ .addOptions (
28+ new OptionData (OptionType .INTEGER , "preference" , "The preference to set." , true )
29+ .addChoices (Arrays .stream (Preference .values ()).map (Preference ::toChoice ).toList ()),
30+ new OptionData (OptionType .BOOLEAN , "state" , "The state of the specified preference." , true )
31+ )
3432 );
3533 }
3634
@@ -51,18 +49,4 @@ public void execute(@NotNull SlashCommandInteractionEvent event) {
5149 Responses .error (event , "Could not %s `%s`." , state ? "enable" : "disable" , preference ).queue ();
5250 }
5351 }
54-
55- private @ NotNull List <Command .Choice > getPreferenceChoices (long userId ) {
56- List <Command .Choice > choices = new ArrayList <>(Preference .values ().length );
57- UserPreferenceManager manager = new UserPreferenceManager (Bot .getDataSource ());
58- for (Preference p : Preference .values ()) {
59- choices .add (new Command .Choice (String .format ("%s (%s)" , p , manager .getOrCreate (userId , p ).isEnabled () ? "Enabled" : "Disabled" ), p .ordinal ()));
60- }
61- return choices ;
62- }
63-
64- @ Override
65- public void handleAutoComplete (@ NotNull CommandAutoCompleteInteractionEvent event , @ NotNull AutoCompleteQuery target ) {
66- event .replyChoices (AutoCompleteUtils .handleChoices (event , e -> getPreferenceChoices (e .getUser ().getIdLong ()))).queue ();
67- }
6852}
0 commit comments