Skip to content

Commit ed3b3d3

Browse files
committed
Use DateTimeFormatter in forms
1 parent 15dee0b commit ed3b3d3

File tree

4 files changed

+26
-32
lines changed

4 files changed

+26
-32
lines changed

src/main/java/net/discordjug/javabot/systems/staff_commands/forms/FormInteractionManager.java

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package net.discordjug.javabot.systems.staff_commands.forms;
22

3-
import java.text.DateFormat;
4-
import java.text.ParseException;
5-
import java.text.SimpleDateFormat;
63
import java.time.Instant;
4+
import java.time.LocalDateTime;
5+
import java.time.ZoneOffset;
6+
import java.time.format.DateTimeFormatter;
7+
import java.time.format.DateTimeParseException;
78
import java.util.List;
8-
import java.util.Locale;
99
import java.util.Objects;
1010
import java.util.Optional;
11-
import java.util.TimeZone;
1211
import java.util.function.Function;
1312

1413
import lombok.RequiredArgsConstructor;
@@ -45,14 +44,14 @@
4544
public class FormInteractionManager implements ButtonHandler, ModalHandler {
4645

4746
/**
48-
* Date and time format used in forms.
47+
* String representation of the date and time format used in forms.
4948
*/
50-
public static final DateFormat DATE_FORMAT;
49+
public static final String DATE_FORMAT_STRING = "dd/MM/yyyy HH:mm";
5150

5251
/**
53-
* String representation of the date and time format used in forms.
52+
* Date and time formatter used in forms.
5453
*/
55-
public static final String DATE_FORMAT_STRING;
54+
public static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern(DATE_FORMAT_STRING);
5655

5756
/**
5857
* Component ID used for form buttons and modals.
@@ -62,12 +61,6 @@ public class FormInteractionManager implements ButtonHandler, ModalHandler {
6261

6362
private final FormsRepository formsRepo;
6463

65-
static {
66-
DATE_FORMAT_STRING = "dd/MM/yyyy HH:mm";
67-
DATE_FORMAT = new SimpleDateFormat(FormInteractionManager.DATE_FORMAT_STRING, Locale.ENGLISH);
68-
DATE_FORMAT.setTimeZone(TimeZone.getTimeZone("UTC"));
69-
}
70-
7164
/**
7265
* Closes the form, preventing further submissions and disabling associated
7366
* buttons from a message this form is attached to, if any.
@@ -239,8 +232,8 @@ public static Optional<Instant> parseExpiration(SlashCommandInteractionEvent eve
239232
expiration = Optional.empty();
240233
} else {
241234
try {
242-
expiration = Optional.of(FormInteractionManager.DATE_FORMAT.parse(expirationStr).toInstant());
243-
} catch (ParseException e) {
235+
expiration = Optional.of(LocalDateTime.parse(expirationStr, DATE_FORMATTER).toInstant(ZoneOffset.UTC));
236+
} catch (DateTimeParseException e) {
244237
throw new IllegalArgumentException("Invalid date. You should follow the format `"
245238
+ FormInteractionManager.DATE_FORMAT_STRING + "`.");
246239
}

src/main/java/net/discordjug/javabot/systems/staff_commands/forms/commands/AddFieldFormSubcommand.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import net.discordjug.javabot.systems.staff_commands.forms.dao.FormsRepository;
88
import net.discordjug.javabot.systems.staff_commands.forms.model.FormData;
99
import net.discordjug.javabot.systems.staff_commands.forms.model.FormField;
10+
import net.discordjug.javabot.util.Responses;
1011
import net.dv8tion.jda.api.components.textinput.TextInputStyle;
1112
import net.dv8tion.jda.api.entities.Message;
1213
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
@@ -17,14 +18,15 @@
1718
import net.dv8tion.jda.api.interactions.commands.OptionType;
1819
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
1920
import net.dv8tion.jda.api.interactions.commands.build.SubcommandData;
21+
import net.dv8tion.jda.api.modals.Modal;
2022
import xyz.dynxsty.dih4jda.interactions.AutoCompletable;
2123

2224
/**
2325
* The `/form add-field` command. This command allows for modification of
2426
* {@link FormData} by adding new fields to it. See
2527
* {@link RemoveFieldFormSubcommand} for the command used to remove fields from
2628
* a form.<br>
27-
* Currently, due to Discord limitations, only 5 fields are allowed per form.
29+
* Currently, due to Discord limitations, only {@link Modal#MAX_COMPONENTS} fields are allowed per form.
2830
* Trying to add more fields will have no effect.
2931
*
3032
* @see FormData
@@ -57,32 +59,29 @@ public AddFieldFormSubcommand(FormsRepository formsRepo, BotConfig botConfig) {
5759
.addOption(OptionType.STRING, FORM_PLACEHOLDER_FIELD, "Field placeholder")
5860
.addOption(OptionType.BOOLEAN, FORM_REQUIRED_FIELD,
5961
"Whether or not the user has to input data in this field. Default: false")
60-
.addOptions(
61-
new OptionData(OptionType.STRING, FORM_STYLE_FIELD, "Input style. Default: SHORT", false)
62-
.addChoices(
63-
Arrays.stream(TextInputStyle.values()).filter(t -> t != TextInputStyle.UNKNOWN)
64-
.map(style -> new Choice(style.name(), style.name())).toList()))
62+
.addOptions(new OptionData(OptionType.STRING, FORM_STYLE_FIELD, "Input style. Default: SHORT", false)
63+
.addChoices(Arrays.stream(TextInputStyle.values()).filter(t -> t != TextInputStyle.UNKNOWN)
64+
.map(style -> new Choice(style.name(), style.name())).toList()))
6565
.addOption(OptionType.STRING, FORM_VALUE_FIELD, "Initial field value"));
6666
}
6767

6868
@Override
6969
public void execute(SlashCommandInteractionEvent event) {
7070
if (!checkForStaffRole(event)) return;
71-
event.deferReply(true).queue();
7271
Optional<FormData> formOpt = formsRepo.getForm(event.getOption(FORM_ID_FIELD, OptionMapping::getAsLong));
7372
if (formOpt.isEmpty()) {
74-
event.getHook().sendMessage("A form with this ID was not found.").queue();
73+
Responses.error(event, "A form with this ID was not found.").queue();
7574
return;
7675
}
7776
FormData form = formOpt.get();
7877

7978
if (form.fields().size() >= Message.MAX_COMPONENT_COUNT) {
80-
event.getHook().sendMessage("Can't add more than 5 components to a form").queue();
79+
Responses.error(event, "Can't add more than %s components to a form", Message.MAX_COMPONENT_COUNT).queue();
8180
return;
8281
}
8382

8483
formsRepo.addField(form, createFormFieldFromEvent(event));
85-
event.getHook().sendMessage("Added a new field to the form.").queue();
84+
event.reply("Added a new field to the form.").setEphemeral(true).queue();
8685
}
8786

8887
@Override

src/main/java/net/discordjug/javabot/systems/staff_commands/forms/commands/AttachFormSubcommand.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import net.discordjug.javabot.systems.staff_commands.forms.FormInteractionManager;
1010
import net.discordjug.javabot.systems.staff_commands.forms.dao.FormsRepository;
1111
import net.discordjug.javabot.systems.staff_commands.forms.model.FormData;
12+
import net.dv8tion.jda.api.components.Component.Type;
1213
import net.dv8tion.jda.api.components.MessageTopLevelComponentUnion;
1314
import net.dv8tion.jda.api.components.actionrow.ActionRow;
1415
import net.dv8tion.jda.api.components.actionrow.ActionRowChildComponent;
@@ -140,7 +141,8 @@ private static void attachFormToMessage(Message message, String buttonLabel, But
140141
button = button.asDisabled();
141142
}
142143

143-
if (rows.isEmpty() || rows.get(rows.size() - 1).getActionComponents().size() >= 5) {
144+
if (rows.isEmpty()
145+
|| rows.get(rows.size() - 1).getActionComponents().size() >= ActionRow.getMaxAllowed(Type.BUTTON)) {
144146
rows.add(ActionRow.of(button));
145147
} else {
146148
ActionRow lastRow = rows.get(rows.size() - 1);

src/main/java/net/discordjug/javabot/systems/staff_commands/forms/model/FormData.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package net.discordjug.javabot.systems.staff_commands.forms.model;
22

33
import java.time.Instant;
4+
import java.time.ZoneId;
45
import java.util.ArrayList;
56
import java.util.Collections;
6-
import java.util.Date;
77
import java.util.List;
88
import java.util.Objects;
99
import java.util.Optional;
@@ -63,8 +63,8 @@ public Optional<String> getOptionalSubmitMessage() {
6363

6464
/**
6565
* Get information about the form's attachment state. If the form is attached to
66-
* a message, this method will return a non-empty optional containing information
67-
* about the message this form is attached to.
66+
* a message, this method will return a non-empty optional containing
67+
* information about the message this form is attached to.
6868
*
6969
* @return optional attachment info
7070
*/
@@ -119,7 +119,7 @@ public String toString() {
119119
} else if (hasExpired()) {
120120
prefix = "Expired";
121121
} else {
122-
prefix = FormInteractionManager.DATE_FORMAT.format(new Date(expiration.toEpochMilli())) + " UTC";
122+
prefix = FormInteractionManager.DATE_FORMATTER.format(expiration.atZone(ZoneId.of("UTC"))) + " UTC";
123123
}
124124

125125
return String.format("[%s] %s", prefix, title);

0 commit comments

Comments
 (0)