diff --git a/src/main/java/de/efi23a/bot/features/alert/AlertFeature.java b/src/main/java/de/efi23a/bot/features/alert/AlertFeature.java index ba6f19d..7dbd8d9 100644 --- a/src/main/java/de/efi23a/bot/features/alert/AlertFeature.java +++ b/src/main/java/de/efi23a/bot/features/alert/AlertFeature.java @@ -53,10 +53,6 @@ public class AlertFeature { * Optimal: 24 Stunden → Unter 24 Stunden wird eine Erinnerung geschickt. */ private static final String ALERT_LAST_REMINDER = "ALERT_LAST_REMINDER"; - /** - * Die Zeit (in Minuten) für den AlertCheck Scheduler. - */ - private static final String ALERT_SCHEDULER_DELAY = "ALERT_SCHEDULER_DELAY"; private final JDA jda; private final MongoConfig mongoConfig; @@ -67,7 +63,7 @@ public class AlertFeature { @PostConstruct void postConstruct() { client = mongoConfig.mongoClient(); - db = client.getDatabase("TestCluster"); + db = client.getDatabase(System.getenv("DATABASE")); alerts = db.getCollection("alerts"); registerAlertCommand(); @@ -124,14 +120,15 @@ private void startAlertCheckerTask() { && lastReminder == null || (Duration.between(Instant.now(), lastReminder.toInstant()) .getSeconds() / 60 / 60) > lastReminderHours) { - alert.replace("lastReminder", System.currentTimeMillis() + ""); + alert.replace("lastReminder", Instant.now()); updateAlert(alert); sendAlert(alert); return; } + if (Duration.between(Instant.now(), lastReminder.toInstant()) .getSeconds() / 60 / 60 > firstReminderHours) { - alert.replace("lastReminder", System.currentTimeMillis() + ""); + alert.replace("lastReminder", Instant.now()); updateAlert(alert); sendAlert(alert); return; @@ -171,7 +168,7 @@ public void addAlert(String name, String date, String description, String create document.put("date", date); document.put("description", description); document.put("createdBy", createdBy); - document.put("lastReminder", ""); + document.put("lastReminder", null); alerts.insertOne(document); } @@ -199,7 +196,7 @@ public void editAlert(String name, String property, String value) { doc.replace(property, value); if (property.equalsIgnoreCase("date")) { - doc.replace("lastReminder", ""); + doc.replace("lastReminder", null); } alerts.replaceOne(filter, doc); @@ -234,8 +231,8 @@ private Document getAlertByName(String name) { } private Date getAlertLastReminder(Document document) { - var lastReminder = document.getString("lastReminder"); - return !lastReminder.isBlank() ? new Date(Long.parseLong(lastReminder)) : null; + Instant lastReminder = (Instant) document.get("lastReminder"); + return lastReminder != null ? Date.from(lastReminder) : null; } private void sendAlert(Document alert) { diff --git a/src/main/java/de/efi23a/bot/features/alert/AlertFeatureListener.java b/src/main/java/de/efi23a/bot/features/alert/AlertFeatureListener.java index 2927f63..139664c 100644 --- a/src/main/java/de/efi23a/bot/features/alert/AlertFeatureListener.java +++ b/src/main/java/de/efi23a/bot/features/alert/AlertFeatureListener.java @@ -18,6 +18,7 @@ public class AlertFeatureListener extends ListenerAdapter { private final JDA jda; private final AlertFeature alertFeature; + private final SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy"); @PostConstruct void postConstruct() { @@ -73,11 +74,9 @@ public void onSlashCommandInteraction(SlashCommandInteractionEvent event) { } try { - var sdf = new SimpleDateFormat("dd.MM.yyyy"); + var dateInstance = sdf.parse(date); - var dateInstanz = sdf.parse(date); - - if (sdf.parse(sdf.format(new Date(System.currentTimeMillis()))).after(dateInstanz)) { + if (sdf.parse(sdf.format(new Date(System.currentTimeMillis()))).after(dateInstance)) { throw new Exception(); } } catch (Exception e) { @@ -102,11 +101,9 @@ public void onSlashCommandInteraction(SlashCommandInteractionEvent event) { if (property.equalsIgnoreCase("date")) { try { - var sdf = new SimpleDateFormat("dd.MM.yyyy"); - - var dateInstanz = sdf.parse(value); + var dateInstance = sdf.parse(value); - if (sdf.parse(sdf.format(new Date(System.currentTimeMillis()))).after(dateInstanz)) { + if (sdf.parse(sdf.format(new Date(System.currentTimeMillis()))).after(dateInstance)) { throw new Exception(); } } catch (Exception e) {