Skip to content

Commit bd1ca9f

Browse files
Added preference state indication
1 parent b459dad commit bd1ca9f

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

src/main/java/net/javadiscord/javabot/systems/user_preferences/UserPreferenceManager.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class UserPreferenceManager {
2727
* @param enabled The preferences' state.
2828
* @return Whether the operation was successful.
2929
*/
30-
public boolean set(long userId, Preference preference, boolean enabled) {
30+
public boolean setOrCreate(long userId, Preference preference, boolean enabled) {
3131
try (Connection con = dataSource.getConnection()) {
3232
UserPreferenceRepository repo = new UserPreferenceRepository(con);
3333
Optional<UserPreference> preferenceOptional = repo.getById(userId, preference);
@@ -46,4 +46,31 @@ public boolean set(long userId, Preference preference, boolean enabled) {
4646
return false;
4747
}
4848
}
49+
50+
/**
51+
* Gets a single {@link UserPreference} (or creates a new one if it doesn't exist yet).
52+
*
53+
* @param userId The users' id.
54+
* @param preference The {@link Preference} to get.
55+
* @return The {@link UserPreference}.
56+
*/
57+
public UserPreference getOrCreate(long userId, Preference preference) {
58+
try (Connection con = dataSource.getConnection()) {
59+
UserPreferenceRepository repo = new UserPreferenceRepository(con);
60+
Optional<UserPreference> preferenceOptional = repo.getById(userId, preference);
61+
if (preferenceOptional.isPresent()) {
62+
return preferenceOptional.get();
63+
} else {
64+
UserPreference userPreference = new UserPreference();
65+
userPreference.setUserId(userId);
66+
userPreference.setOrdinal(preference.ordinal());
67+
userPreference.setEnabled(true);
68+
repo.insert(userPreference, false);
69+
return userPreference;
70+
}
71+
} catch (SQLException e) {
72+
ExceptionLogger.capture(e, getClass().getSimpleName());
73+
return null;
74+
}
75+
}
4976
}

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,23 +44,24 @@ public void execute(@NotNull SlashCommandInteractionEvent event) {
4444
Preference preference = Preference.values()[preferenceMapping.getAsInt()];
4545
boolean state = stateMapping.getAsBoolean();
4646
UserPreferenceManager manager = new UserPreferenceManager(Bot.getDataSource());
47-
if (manager.set(event.getUser().getIdLong(), preference, state)) {
47+
if (manager.setOrCreate(event.getUser().getIdLong(), preference, state)) {
4848
Responses.info(event, "Preference Updated", "Successfully set `%s` to `%s`!", preference, state).queue();
4949
} else {
5050
Responses.error(event, "Could not update `%s` to `%s`.", preference, state).queue();
5151
}
5252
}
5353

54-
private @NotNull List<Command.Choice> getPreferenceChoices() {
54+
private @NotNull List<Command.Choice> getPreferenceChoices(long userId) {
5555
List<Command.Choice> choices = new ArrayList<>(Preference.values().length);
56+
UserPreferenceManager manager = new UserPreferenceManager(Bot.getDataSource());
5657
for (Preference p : Preference.values()) {
57-
choices.add(new Command.Choice(p.toString(), p.ordinal()));
58+
choices.add(new Command.Choice(String.format("%s (%s)", p, manager.getOrCreate(userId, p).isEnabled() ? "Enabled" : "Disabled"), p.ordinal()));
5859
}
5960
return choices;
6061
}
6162

6263
@Override
6364
public void handleAutoComplete(@NotNull CommandAutoCompleteInteractionEvent event, @NotNull AutoCompleteQuery target) {
64-
event.replyChoices(AutoCompleteUtils.handleChoices(event, e -> getPreferenceChoices())).queue();
65+
event.replyChoices(AutoCompleteUtils.handleChoices(event, e -> getPreferenceChoices(e.getUser().getIdLong()))).queue();
6566
}
6667
}

0 commit comments

Comments
 (0)