Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor methods to handle invalid transfers #933

Merged
merged 5 commits into from
Nov 1, 2023
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import net.dv8tion.jda.api.interactions.commands.build.Commands;
import net.dv8tion.jda.api.interactions.components.Modal;
import net.dv8tion.jda.api.interactions.components.text.TextInput;
import net.dv8tion.jda.api.interactions.components.text.TextInput.Builder;
import net.dv8tion.jda.api.interactions.components.text.TextInputStyle;
import net.dv8tion.jda.api.requests.RestAction;
import net.dv8tion.jda.api.utils.messages.MessageCreateBuilder;
Expand Down Expand Up @@ -49,6 +50,8 @@ public final class TransferQuestionCommand extends BotCommandAdapter
private static final Pattern TITLE_GUESS_COMPACT_REMOVAL_PATTERN = Pattern.compile("\\W");
private static final int TITLE_MIN_LENGTH = 3;
private static final Color EMBED_COLOR = new Color(50, 164, 168);
private static final int INPUT_MAX_LENGTH = 2000;
private static final int INPUT_MIN_LENGTH = 3;
private final Predicate<String> isHelpForumName;
private final List<String> tags;

Expand All @@ -70,7 +73,7 @@ public TransferQuestionCommand(Config config) {
@Override
public void onMessageContext(MessageContextInteractionEvent event) {

if (isBotMessageTransfer(event)) {
if (isInvalidForTransfer(event)) {
return;
}

Expand All @@ -87,12 +90,14 @@ public void onMessageContext(MessageContextInteractionEvent event) {
.setValue(createTitle(originalMessage))
.build();

TextInput modalInput =
Builder modalInputBuilder =
TextInput.create(MODAL_INPUT_ID, "Question", TextInputStyle.PARAGRAPH)
.setValue(originalMessage)
.setRequiredRange(3, 2000)
.setPlaceholder("Contents of the question")
.build();
.setRequiredRange(INPUT_MIN_LENGTH, INPUT_MAX_LENGTH)
.setPlaceholder("Contents of the question");

if (!isQuestionTooShort(originalMessage)) {
modalInputBuilder.setValue(originalMessage);
}
Zabuzard marked this conversation as resolved.
Show resolved Hide resolved

TextInput modalTag = TextInput.create(MODAL_TAG, "Most fitting tag", TextInputStyle.SHORT)
.setValue(mostCommonTag)
Expand All @@ -103,7 +108,7 @@ public void onMessageContext(MessageContextInteractionEvent event) {
generateComponentId(authorId, originalMessageId, originalChannelId);
Modal transferModal = Modal.create(modalComponentId, "Transfer this question")
.addActionRow(modalTitle)
.addActionRow(modalInput)
.addActionRow(modalInputBuilder.build())
.addActionRow(modalTag)
.build();

Expand Down Expand Up @@ -228,9 +233,23 @@ private MessageEmbed makeEmbedForPost(User originalUser, String originalMessage)
private record ForumPost(User author, Message message) {
}

private boolean isBotMessageTransfer(MessageContextInteractionEvent event) {
if (event.getTarget().getAuthor().isBot()) {
event.reply("Cannot transfer messages from a bot.").setEphemeral(true).queue();
private boolean isBotMessageTransfer(User author) {
return author.isBot();
}

private void handleBotMessageTransfer(MessageContextInteractionEvent event) {
event.reply("Cannot transfer messages from a bot.").setEphemeral(true).queue();
}

private boolean isQuestionTooShort(String question) {
return question.length() < INPUT_MIN_LENGTH;
}
Taz03 marked this conversation as resolved.
Show resolved Hide resolved

private boolean isInvalidForTransfer(MessageContextInteractionEvent event) {
User author = event.getTarget().getAuthor();

if (isBotMessageTransfer(author)) {
handleBotMessageTransfer(event);
return true;
}
return false;
Expand Down
Loading