Skip to content

Commit 10f2575

Browse files
authored
refactor methods to handle invalid transfers (#933)
* refactor methods to handle invalid transfers * refactor bot transfer checks * add check for too short message transfers resolves #926 * minor change related to sent message * refactor handling of short messages * handle longer messages
1 parent 50a6bcd commit 10f2575

1 file changed

Lines changed: 36 additions & 10 deletions

File tree

application/src/main/java/org/togetherjava/tjbot/features/moderation/TransferQuestionCommand.java

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import net.dv8tion.jda.api.interactions.commands.build.Commands;
1616
import net.dv8tion.jda.api.interactions.components.Modal;
1717
import net.dv8tion.jda.api.interactions.components.text.TextInput;
18+
import net.dv8tion.jda.api.interactions.components.text.TextInput.Builder;
1819
import net.dv8tion.jda.api.interactions.components.text.TextInputStyle;
1920
import net.dv8tion.jda.api.requests.RestAction;
2021
import net.dv8tion.jda.api.utils.messages.MessageCreateBuilder;
@@ -49,6 +50,8 @@ public final class TransferQuestionCommand extends BotCommandAdapter
4950
private static final Pattern TITLE_GUESS_COMPACT_REMOVAL_PATTERN = Pattern.compile("\\W");
5051
private static final int TITLE_MIN_LENGTH = 3;
5152
private static final Color EMBED_COLOR = new Color(50, 164, 168);
53+
private static final int INPUT_MAX_LENGTH = Message.MAX_CONTENT_LENGTH;
54+
private static final int INPUT_MIN_LENGTH = 3;
5255
private final Predicate<String> isHelpForumName;
5356
private final List<String> tags;
5457

@@ -70,7 +73,7 @@ public TransferQuestionCommand(Config config) {
7073
@Override
7174
public void onMessageContext(MessageContextInteractionEvent event) {
7275

73-
if (isBotMessageTransfer(event)) {
76+
if (isInvalidForTransfer(event)) {
7477
return;
7578
}
7679

@@ -87,12 +90,15 @@ public void onMessageContext(MessageContextInteractionEvent event) {
8790
.setValue(createTitle(originalMessage))
8891
.build();
8992

90-
TextInput modalInput =
93+
Builder modalInputBuilder =
9194
TextInput.create(MODAL_INPUT_ID, "Question", TextInputStyle.PARAGRAPH)
92-
.setValue(originalMessage)
93-
.setRequiredRange(3, 2000)
94-
.setPlaceholder("Contents of the question")
95-
.build();
95+
.setRequiredRange(INPUT_MIN_LENGTH, INPUT_MAX_LENGTH)
96+
.setPlaceholder("Contents of the question");
97+
98+
if (!isQuestionTooShort(originalMessage)) {
99+
String trimmedMessage = getMessageUptoMaxLimit(originalMessage);
100+
modalInputBuilder.setValue(trimmedMessage);
101+
}
96102

97103
TextInput modalTag = TextInput.create(MODAL_TAG, "Most fitting tag", TextInputStyle.SHORT)
98104
.setValue(mostCommonTag)
@@ -103,7 +109,7 @@ public void onMessageContext(MessageContextInteractionEvent event) {
103109
generateComponentId(authorId, originalMessageId, originalChannelId);
104110
Modal transferModal = Modal.create(modalComponentId, "Transfer this question")
105111
.addActionRow(modalTitle)
106-
.addActionRow(modalInput)
112+
.addActionRow(modalInputBuilder.build())
107113
.addActionRow(modalTag)
108114
.build();
109115

@@ -228,11 +234,31 @@ private MessageEmbed makeEmbedForPost(User originalUser, String originalMessage)
228234
private record ForumPost(User author, Message message) {
229235
}
230236

231-
private boolean isBotMessageTransfer(MessageContextInteractionEvent event) {
232-
if (event.getTarget().getAuthor().isBot()) {
233-
event.reply("Cannot transfer messages from a bot.").setEphemeral(true).queue();
237+
private boolean isBotMessageTransfer(User author) {
238+
return author.isBot();
239+
}
240+
241+
private void handleBotMessageTransfer(MessageContextInteractionEvent event) {
242+
event.reply("Cannot transfer messages from a bot.").setEphemeral(true).queue();
243+
}
244+
245+
private boolean isQuestionTooShort(String question) {
246+
return question.length() < INPUT_MIN_LENGTH;
247+
}
248+
249+
private boolean isInvalidForTransfer(MessageContextInteractionEvent event) {
250+
User author = event.getTarget().getAuthor();
251+
252+
if (isBotMessageTransfer(author)) {
253+
handleBotMessageTransfer(event);
234254
return true;
235255
}
236256
return false;
237257
}
258+
259+
private String getMessageUptoMaxLimit(String originalMessage) {
260+
return originalMessage.length() > INPUT_MAX_LENGTH
261+
? originalMessage.substring(0, INPUT_MAX_LENGTH)
262+
: originalMessage;
263+
}
238264
}

0 commit comments

Comments
 (0)