Skip to content

Commit 94f7d04

Browse files
Removed autocomplete and replaced with dynamic choices
1 parent 4753d7b commit 94f7d04

File tree

3 files changed

+15
-25
lines changed

3 files changed

+15
-25
lines changed

src/main/java/net/javadiscord/javabot/systems/user_preferences/commands/PreferencesListSubcommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public PreferencesListSubcommand() {
2828
public void execute(@NotNull SlashCommandInteractionEvent event) {
2929
UserPreferenceManager manager = new UserPreferenceManager(Bot.getDataSource());
3030
String preferences = Arrays.stream(Preference.values())
31-
.map(p -> String.format("%s (%s)", p, manager.getOrCreate(event.getUser().getIdLong(), p).isEnabled() ? "Enabled" : "Disabled"))
31+
.map(p -> String.format("`%s` %s", manager.getOrCreate(event.getUser().getIdLong(), p).isEnabled() ? "\uD83D\uDFE2" : "\uD83D\uDD34", p))
3232
.collect(Collectors.joining("\n"));
3333
Responses.info(event, String.format("%s's Preferences", event.getUser().getName()), preferences).queue();
3434
}
Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,34 @@
11

22
package net.javadiscord.javabot.systems.user_preferences.commands;
33

4-
import com.dynxsty.dih4jda.interactions.commands.AutoCompletable;
54
import com.dynxsty.dih4jda.interactions.commands.SlashCommand;
6-
import com.dynxsty.dih4jda.util.AutoCompleteUtils;
7-
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
85
import 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;
116
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
127
import net.dv8tion.jda.api.interactions.commands.OptionType;
8+
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
139
import net.dv8tion.jda.api.interactions.commands.build.SubcommandData;
1410
import net.javadiscord.javabot.Bot;
1511
import net.javadiscord.javabot.systems.user_preferences.UserPreferenceManager;
1612
import net.javadiscord.javabot.systems.user_preferences.model.Preference;
1713
import net.javadiscord.javabot.util.Responses;
1814
import 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
}

src/main/java/net/javadiscord/javabot/systems/user_preferences/model/Preference.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package net.javadiscord.javabot.systems.user_preferences.model;
22

3+
import net.dv8tion.jda.api.interactions.commands.Command;
4+
35
/**
46
* Contains all preferences users can set.
57
*/
@@ -25,4 +27,8 @@ public String toString() {
2527
public boolean getDefaultState() {
2628
return defaultState;
2729
}
30+
31+
public Command.Choice toChoice() {
32+
return new Command.Choice(name, String.valueOf(ordinal()));
33+
}
2834
}

0 commit comments

Comments
 (0)