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

Bugfix with /change_help_title allowing too long titles #466

Merged
merged 2 commits into from
Jul 18, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -57,6 +61,9 @@ public void onSlashCommand(@NotNull SlashCommandInteractionEvent event) {
if (!helper.handleIsHelpThread(event)) {
return;
}
if (!handleIsValidTitle(title, event)) {
return;
}
Zabuzard marked this conversation as resolved.
Show resolved Hide resolved

ThreadChannel helpThread = event.getThreadChannel();
if (helpThread.isArchived()) {
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> isOverviewChannelName;
private final String overviewChannelPattern;
Expand Down