Skip to content

Commit

Permalink
optimized code
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasGnG committed Sep 27, 2023
1 parent 4882cc0 commit e37aa19
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 19 deletions.
19 changes: 8 additions & 11 deletions src/main/java/de/efi23a/bot/features/alert/AlertFeature.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down

0 comments on commit e37aa19

Please sign in to comment.