Skip to content

Commit d1cd1b9

Browse files
committed
Finished up most of the structuring.
1 parent c6581b1 commit d1cd1b9

File tree

1 file changed

+70
-24
lines changed
  • src/main/java/de/presti/ree6/commands/impl/community

1 file changed

+70
-24
lines changed

src/main/java/de/presti/ree6/commands/impl/community/Schedule.java

Lines changed: 70 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,25 @@
44
import de.presti.ree6.commands.CommandEvent;
55
import de.presti.ree6.commands.interfaces.Command;
66
import de.presti.ree6.commands.interfaces.ICommand;
7+
import de.presti.ree6.sql.SQLSession;
78
import de.presti.ree6.sql.entities.ScheduledMessage;
89
import net.dv8tion.jda.api.Permission;
910
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
1011
import net.dv8tion.jda.api.interactions.commands.OptionType;
1112
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
13+
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
1214
import net.dv8tion.jda.api.interactions.commands.build.SubcommandData;
1315
import net.dv8tion.jda.internal.interactions.CommandDataImpl;
1416
import org.apache.commons.validator.GenericValidator;
1517
import org.apache.commons.validator.routines.DateValidator;
1618

1719
import java.time.Duration;
20+
import java.time.Instant;
1821
import java.time.LocalDateTime;
1922
import java.time.ZoneId;
2023
import java.time.format.DateTimeFormatter;
2124
import java.time.format.DateTimeParseException;
25+
import java.util.Map;
2226

2327
/**
2428
* Schedule a message to a specific time or a repeating time.
@@ -46,29 +50,67 @@ public void onPerform(CommandEvent commandEvent) {
4650
return;
4751
}
4852

49-
OptionMapping repeat = commandEvent.getOption("repeat");
53+
String subCommand = commandEvent.getSlashCommandInteractionEvent().getSubcommandName();
5054

51-
OptionMapping month = commandEvent.getOption("month");
52-
OptionMapping day = commandEvent.getOption("day");
53-
OptionMapping hour = commandEvent.getOption("hour");
54-
OptionMapping minute = commandEvent.getOption("minute");
55+
switch (subCommand) {
5556

56-
long fullTime = 0;
57-
if (month != null) fullTime += Duration.ofDays(31 * month.getAsLong()).toMillis();
58-
if (day != null) fullTime += Duration.ofDays(day.getAsLong()).toMillis();
59-
if (hour != null) fullTime += Duration.ofHours(hour.getAsLong()).toMillis();
60-
if (minute != null) fullTime += Duration.ofMinutes(minute.getAsLong()).toMillis();
57+
case "list" -> {
58+
StringBuilder stringBuilder = new StringBuilder();
6159

62-
if (fullTime < Duration.ofMinutes(1).toDays()) {
63-
commandEvent.reply(commandEvent.getResource("message.default.dateError.notEnough"));
64-
return;
65-
}
60+
for (ScheduledMessage scheduledMessage : SQLSession.getSqlConnector().getSqlWorker()
61+
.getEntityList(new ScheduledMessage(), "SELECT * FROM ScheduledMessage WHERE guild = :gid ",
62+
Map.of("gid", commandEvent.getGuild().getIdLong()))) {
63+
stringBuilder.append(scheduledMessage.getId()).append(" ").append("-").append(" ")
64+
.append(scheduledMessage.getMessage()).append(" ")
65+
.append("->").append(" ")
66+
.append(Instant.ofEpochMilli(scheduledMessage.getDelayAmount())
67+
.atZone(ZoneId.systemDefault()).format(DateTimeFormatter.ofPattern("dd.MM.yyyy HH/mm")));
68+
}
69+
// TODO:: list response message.
70+
}
71+
72+
case "delete" -> {
73+
OptionMapping id = commandEvent.getOption("id");
74+
75+
ScheduledMessage scheduledMessage = SQLSession.getSqlConnector().getSqlWorker()
76+
.getEntity(new ScheduledMessage(), "SELECT * FROM ScheduledMessage WHERE guild = :gid AND Id = :id",
77+
Map.of("gid", commandEvent.getGuild().getIdLong(), "id", id.getAsLong()));
78+
79+
if (scheduledMessage != null) {
80+
// TODO:: add success message.
81+
SQLSession.getSqlConnector().getSqlWorker().deleteEntity(scheduledMessage);
82+
} else {
83+
// TODO:: add failed message.
84+
}
85+
}
6686

67-
// TODO:: check for webhook and use existing one.
68-
ScheduledMessage scheduledMessage = new ScheduledMessage();
69-
scheduledMessage.setDelayAmount(fullTime);
70-
scheduledMessage.setRepeated(repeat.getAsBoolean());
71-
// TODO:: add success message.
87+
case "create" -> {
88+
OptionMapping repeat = commandEvent.getOption("repeat");
89+
OptionMapping month = commandEvent.getOption("month");
90+
OptionMapping day = commandEvent.getOption("day");
91+
OptionMapping hour = commandEvent.getOption("hour");
92+
OptionMapping minute = commandEvent.getOption("minute");
93+
94+
long fullTime = 0;
95+
if (month != null) fullTime += Duration.ofDays(31 * month.getAsLong()).toMillis();
96+
if (day != null) fullTime += Duration.ofDays(day.getAsLong()).toMillis();
97+
if (hour != null) fullTime += Duration.ofHours(hour.getAsLong()).toMillis();
98+
if (minute != null) fullTime += Duration.ofMinutes(minute.getAsLong()).toMillis();
99+
100+
if (fullTime < Duration.ofMinutes(1).toDays()) {
101+
commandEvent.reply(commandEvent.getResource("message.default.dateError.notEnough"));
102+
return;
103+
}
104+
105+
// TODO:: check for webhook and use existing one.
106+
ScheduledMessage scheduledMessage = new ScheduledMessage();
107+
scheduledMessage.setDelayAmount(fullTime);
108+
scheduledMessage.setRepeated(repeat.getAsBoolean());
109+
// TODO:: add success message.
110+
}
111+
112+
default -> commandEvent.reply(commandEvent.getResource("message.default.invalidOption"));
113+
}
72114
}
73115

74116
/**
@@ -77,11 +119,15 @@ public void onPerform(CommandEvent commandEvent) {
77119
@Override
78120
public CommandData getCommandData() {
79121
return new CommandDataImpl("schedule", "command.description.schedule")
80-
.addOption(OptionType.INTEGER, "month", "The months of the delay.", false)
81-
.addOption(OptionType.INTEGER, "day", "The days of the delay.", false)
82-
.addOption(OptionType.INTEGER, "hour", "The hours of the delay.", false)
83-
.addOption(OptionType.INTEGER, "minute", "The minutes of the delay.", false)
84-
.addOption(OptionType.BOOLEAN, "repeat", "If the schedule should be repeated.", true);
122+
.addSubcommands(new SubcommandData("create", "Create a new scheduled Message.")
123+
.addOption(OptionType.INTEGER, "month", "The months of the delay.", false)
124+
.addOption(OptionType.INTEGER, "day", "The days of the delay.", false)
125+
.addOption(OptionType.INTEGER, "hour", "The hours of the delay.", false)
126+
.addOption(OptionType.INTEGER, "minute", "The minutes of the delay.", false)
127+
.addOption(OptionType.BOOLEAN, "repeat", "If the schedule should be repeated.", true),
128+
new SubcommandData("list", "List all scheduled Messages."),
129+
new SubcommandData("delete", "Delete a scheduled Message.")
130+
.addOptions(new OptionData(OptionType.INTEGER, "id", "The ID of the scheduled Message", true).setMinValue(1)));
85131
}
86132

87133
/**

0 commit comments

Comments
 (0)