From b9e5ce7cc78790d6df10026807219168d41cc4de Mon Sep 17 00:00:00 2001 From: Zabuzard Date: Sat, 9 Jul 2022 15:35:27 +0200 Subject: [PATCH 1/2] added title verification to /change_help_title * also decreased title max length to 70 to account for long categories like "[Together Java Bot]" --- .../commands/help/ChangeHelpTitleCommand.java | 21 +++++++++++++++++++ .../tjbot/commands/help/HelpSystemHelper.java | 2 +- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/help/ChangeHelpTitleCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/help/ChangeHelpTitleCommand.java index a94471c103..c1a55d7bbc 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/help/ChangeHelpTitleCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/help/ChangeHelpTitleCommand.java @@ -4,6 +4,7 @@ import com.github.benmanes.caffeine.cache.Caffeine; import net.dv8tion.jda.api.entities.ThreadChannel; import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; +import net.dv8tion.jda.api.interactions.callbacks.IReplyCallback; import net.dv8tion.jda.api.interactions.commands.OptionType; import org.jetbrains.annotations.NotNull; import org.togetherjava.tjbot.commands.SlashCommandAdapter; @@ -15,6 +16,9 @@ import java.util.Optional; import java.util.concurrent.TimeUnit; +import static org.togetherjava.tjbot.commands.help.HelpSystemHelper.TITLE_COMPACT_LENGTH_MAX; +import static org.togetherjava.tjbot.commands.help.HelpSystemHelper.TITLE_COMPACT_LENGTH_MIN; + /** * Implements the {@code /change-help-title} command, which is able to change the title of a help * thread. @@ -57,6 +61,9 @@ public void onSlashCommand(@NotNull SlashCommandInteractionEvent event) { if (!helper.handleIsHelpThread(event)) { return; } + if (!handleIsValidTitle(title, event)) { + return; + } ThreadChannel helpThread = event.getThreadChannel(); if (helpThread.isArchived()) { @@ -88,4 +95,18 @@ private boolean isHelpThreadOnCooldown(@NotNull ThreadChannel helpThread) { .filter(Instant.now()::isBefore) .isPresent(); } + + private boolean handleIsValidTitle(@NotNull CharSequence title, @NotNull IReplyCallback event) { + if (HelpSystemHelper.isTitleValid(title)) { + return true; + } + + event.reply( + "Sorry, but the title length (after removal of special characters) has to be between %d and %d." + .formatted(TITLE_COMPACT_LENGTH_MIN, TITLE_COMPACT_LENGTH_MAX)) + .setEphemeral(true) + .queue(); + + return false; + } } diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpSystemHelper.java b/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpSystemHelper.java index b7f5e77f1a..c6bbd74544 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpSystemHelper.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpSystemHelper.java @@ -36,7 +36,7 @@ public final class HelpSystemHelper { private static final Pattern TITLE_COMPACT_REMOVAL_PATTERN = Pattern.compile("\\W"); static final int TITLE_COMPACT_LENGTH_MIN = 2; - static final int TITLE_COMPACT_LENGTH_MAX = 80; + static final int TITLE_COMPACT_LENGTH_MAX = 70; private final Predicate isOverviewChannelName; private final String overviewChannelPattern; From c7a9e66246b45b841639f0bb6e39d82a3846c521 Mon Sep 17 00:00:00 2001 From: Zabuzard Date: Mon, 18 Jul 2022 08:59:00 +0200 Subject: [PATCH 2/2] Minor improvement (CR Tanish) --- .../tjbot/commands/help/ChangeHelpTitleCommand.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/help/ChangeHelpTitleCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/help/ChangeHelpTitleCommand.java index c1a55d7bbc..14a6d0cb5b 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/help/ChangeHelpTitleCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/help/ChangeHelpTitleCommand.java @@ -58,10 +58,7 @@ public ChangeHelpTitleCommand(@NotNull HelpSystemHelper helper) { public void onSlashCommand(@NotNull SlashCommandInteractionEvent event) { String title = event.getOption(TITLE_OPTION).getAsString(); - if (!helper.handleIsHelpThread(event)) { - return; - } - if (!handleIsValidTitle(title, event)) { + if (!helper.handleIsHelpThread(event) || !handleIsValidTitle(title, event)) { return; }