Skip to content

Commit

Permalink
Make snooze buttons configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
Matyrobbrt committed May 29, 2024
1 parent 4c63431 commit b8012a6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
package net.neoforged.camelot.config.module

import groovy.contracts.Requires
import groovy.transform.CompileStatic

import java.time.Duration

/**
* The module that allows users to set up reminders.
*/
@CompileStatic
class Reminders extends ModuleConfiguration {
static final List<Duration> DEFAULT_SNOOZE_DURATIONS = List.of(Duration.ofHours(1), Duration.ofHours(6), Duration.ofDays(1))

/**
* The durations that snooze buttons will have
*/
List<Duration> snoozeDurations = new ArrayList<>(DEFAULT_SNOOZE_DURATIONS)

@Override
@Requires({ snoozeDurations.size() <= 20 })
void validate() {
super.validate()
}
}
12 changes: 7 additions & 5 deletions src/main/java/net/neoforged/camelot/module/RemindersModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.time.Instant;
import java.util.Collection;
import java.util.EnumSet;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
Expand All @@ -56,10 +57,7 @@ public RemindersModule() {
private static final String SNOOZE_BUTTON_ID = "snooze_reminder";
private static final String SNOOZE_EMOJI = String.valueOf('⏱');

public static final ActionRow SNOOZE_BUTTONS = ActionRow.of(Stream.of(
Duration.ofHours(1), Duration.ofHours(6), Duration.ofDays(1)
).map(duration -> Button.secondary(SNOOZE_BUTTON_ID + "-" + duration.getSeconds(), SNOOZE_EMOJI + " " + DateUtils.formatDuration(duration)))
.toArray(ItemComponent[]::new));
private List<ActionRow> snoozeButtons = List.of();

private final Cache<Long, Reminder> snoozable = Caffeine.newBuilder()
.initialCapacity(10)
Expand All @@ -79,6 +77,9 @@ public void registerCommands(CommandClientBuilder builder) {

@Override
public void setup(JDA jda) {
snoozeButtons = ActionRow.partitionOf(config().getSnoozeDurations().stream().map(duration -> Button.secondary(SNOOZE_BUTTON_ID + "-" + duration.getSeconds(), SNOOZE_EMOJI + " " + DateUtils.formatDuration(duration)))
.toArray(ItemComponent[]::new));

Database.main().useExtension(RemindersDAO.class, db -> db.getAllReminders()
.forEach(reminder -> {
final Instant now = Instant.now();
Expand Down Expand Up @@ -202,7 +203,8 @@ private MessageCreateData createBaseMessage(User user, Reminder reminder, String
.build()
)
.setAllowedMentions(ALLOWED_MENTIONS)
.setComponents(SNOOZE_BUTTONS, ActionRow.of(DismissListener.createDismissButton(user)))
.addComponents(snoozeButtons)
.addActionRow(DismissListener.createDismissButton(user))
.setSuppressedNotifications(false)
.build();
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/neoforged/camelot/util/DateUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public class DateUtils {
public static String formatDuration(Duration duration) {
final StringBuilder str = new StringBuilder();

final long years = duration.getSeconds() / ChronoUnit.YEARS.getDuration().getSeconds();
duration = duration.minus(of(years, ChronoUnit.YEARS));
final long years = duration.getSeconds() / (ChronoUnit.DAYS.getDuration().getSeconds() * 365);
duration = duration.minus(of(years * 365, ChronoUnit.DAYS));
if (years > 0) appendMaybePlural(str, years, "year");

final long months = duration.getSeconds() / ChronoUnit.MONTHS.getDuration().getSeconds();
Expand Down

0 comments on commit b8012a6

Please sign in to comment.