Skip to content

Commit 4d10b42

Browse files
authored
Bugfix with /change_help_title allowing too long titles (#466)
* added title verification to /change_help_title * also decreased title max length to 70 to account for long categories like "[Together Java Bot]" * Minor improvement (CR Tanish)
1 parent fd3681c commit 4d10b42

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

application/src/main/java/org/togetherjava/tjbot/commands/help/ChangeHelpTitleCommand.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.github.benmanes.caffeine.cache.Caffeine;
55
import net.dv8tion.jda.api.entities.ThreadChannel;
66
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
7+
import net.dv8tion.jda.api.interactions.callbacks.IReplyCallback;
78
import net.dv8tion.jda.api.interactions.commands.OptionType;
89
import org.jetbrains.annotations.NotNull;
910
import org.togetherjava.tjbot.commands.SlashCommandAdapter;
@@ -15,6 +16,9 @@
1516
import java.util.Optional;
1617
import java.util.concurrent.TimeUnit;
1718

19+
import static org.togetherjava.tjbot.commands.help.HelpSystemHelper.TITLE_COMPACT_LENGTH_MAX;
20+
import static org.togetherjava.tjbot.commands.help.HelpSystemHelper.TITLE_COMPACT_LENGTH_MIN;
21+
1822
/**
1923
* Implements the {@code /change-help-title} command, which is able to change the title of a help
2024
* thread.
@@ -54,7 +58,7 @@ public ChangeHelpTitleCommand(@NotNull HelpSystemHelper helper) {
5458
public void onSlashCommand(@NotNull SlashCommandInteractionEvent event) {
5559
String title = event.getOption(TITLE_OPTION).getAsString();
5660

57-
if (!helper.handleIsHelpThread(event)) {
61+
if (!helper.handleIsHelpThread(event) || !handleIsValidTitle(title, event)) {
5862
return;
5963
}
6064

@@ -88,4 +92,18 @@ private boolean isHelpThreadOnCooldown(@NotNull ThreadChannel helpThread) {
8892
.filter(Instant.now()::isBefore)
8993
.isPresent();
9094
}
95+
96+
private boolean handleIsValidTitle(@NotNull CharSequence title, @NotNull IReplyCallback event) {
97+
if (HelpSystemHelper.isTitleValid(title)) {
98+
return true;
99+
}
100+
101+
event.reply(
102+
"Sorry, but the title length (after removal of special characters) has to be between %d and %d."
103+
.formatted(TITLE_COMPACT_LENGTH_MIN, TITLE_COMPACT_LENGTH_MAX))
104+
.setEphemeral(true)
105+
.queue();
106+
107+
return false;
108+
}
91109
}

application/src/main/java/org/togetherjava/tjbot/commands/help/HelpSystemHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public final class HelpSystemHelper {
3636

3737
private static final Pattern TITLE_COMPACT_REMOVAL_PATTERN = Pattern.compile("\\W");
3838
static final int TITLE_COMPACT_LENGTH_MIN = 2;
39-
static final int TITLE_COMPACT_LENGTH_MAX = 80;
39+
static final int TITLE_COMPACT_LENGTH_MAX = 70;
4040

4141
private final Predicate<String> isOverviewChannelName;
4242
private final String overviewChannelPattern;

0 commit comments

Comments
 (0)