Skip to content

Commit 94252f3

Browse files
committed
add reason size limit to command
1 parent fd30a49 commit 94252f3

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

src/main/java/net/discordjug/javabot/systems/help/commands/UnreserveCommand.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
@AutoDetectableComponentHandler(UnreserveCommand.UNRESERVE_ID)
4040
public class UnreserveCommand extends SlashCommand implements ModalHandler {
4141
static final String UNRESERVE_ID = "unreserve";
42+
private static final int MINIMUM_REASON_LENGTH = 11;
4243
private static final String REASON_ID = "reason";
4344
private final BotConfig botConfig;
4445
private final DbActions dbActions;
@@ -72,9 +73,9 @@ public void execute(@NotNull SlashCommandInteractionEvent event) {
7273
onCloseRequest(event, event, event.getChannel(), reason, ()->{
7374
TextInput reasonInput = TextInput
7475
.create(REASON_ID, "Reason", TextInputStyle.SHORT)
75-
.setRequiredRange(11, 100)
76+
.setRequiredRange(MINIMUM_REASON_LENGTH, 100)
7677
.setRequired(true)
77-
.setPlaceholder("Please enter the reason you are closing this post here")
78+
.setPlaceholder(reason == null ? "Please enter the reason you are closing this post here" : reason)
7879
.build();
7980
Modal modal = Modal
8081
.create(ComponentIdBuilder.build(UNRESERVE_ID), "Close post")
@@ -91,13 +92,14 @@ public void handleModal(ModalInteractionEvent event, List<ModalMapping> values)
9192
.stream()
9293
.filter(mapping -> REASON_ID.equals(mapping.getId()))
9394
.map(mapping -> mapping.getAsString())
95+
.filter(reason -> !isReasonInvalid(reason))
9496
.findAny()
9597
.ifPresentOrElse(reason -> {
9698
onCloseRequest(event, event, event.getChannel(), reason, ()->{
97-
Responses.error(event, "An error occured - The reason field is missing.").queue();
99+
Responses.error(event, "The provided reason is missing or not valid").queue();
98100
ExceptionLogger.capture(new IllegalStateException("A reason was expected but not present"), getClass().getName());
99101
});
100-
}, () -> Responses.warning(event, "A reason must be provided").queue());
102+
}, () -> Responses.warning(event, "A valid reason must be provided").queue());
101103

102104
}
103105

@@ -115,7 +117,7 @@ private void onCloseRequest(Interaction interaction, IReplyCallback replyCallbac
115117
}
116118
HelpManager manager = new HelpManager(postThread, dbActions, botConfig, helpAccountRepository, helpTransactionRepository, preferenceService);
117119
if (manager.isForumEligibleToBeUnreserved(interaction)) {
118-
if (replyCallback.getUser().getIdLong() != postThread.getOwnerIdLong() && reason == null) {
120+
if (replyCallback.getUser().getIdLong() != postThread.getOwnerIdLong() && isReasonInvalid(reason)) {
119121
noReasonHandler.run();
120122
return;
121123
}
@@ -127,6 +129,10 @@ private void onCloseRequest(Interaction interaction, IReplyCallback replyCallbac
127129
}
128130
}
129131

132+
private boolean isReasonInvalid(String reason) {
133+
return reason == null || reason.length() < MINIMUM_REASON_LENGTH;
134+
}
135+
130136
private void replyInvalidChannel(IReplyCallback replyCallback) {
131137
Responses.warning(replyCallback, "Invalid Channel",
132138
"This command may only be used in either the text-channel-based help system, or in our new forum help system.")

0 commit comments

Comments
 (0)