diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/help/AskCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/help/AskCommand.java index 95b69fbcae..3d9f72270b 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/help/AskCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/help/AskCommand.java @@ -16,6 +16,8 @@ import org.togetherjava.tjbot.commands.SlashCommandVisibility; import org.togetherjava.tjbot.config.Config; +import java.util.Optional; + import static org.togetherjava.tjbot.commands.help.HelpSystemHelper.TITLE_COMPACT_LENGTH_MAX; import static org.togetherjava.tjbot.commands.help.HelpSystemHelper.TITLE_COMPACT_LENGTH_MIN; @@ -83,8 +85,14 @@ public void onSlashCommand(@NotNull SlashCommandInteractionEvent event) { return; } - TextChannel helpStagingChannel = event.getTextChannel(); - helpStagingChannel.createThreadChannel("[%s] %s".formatted(category, title)) + Optional maybeOverviewChannel = + helper.handleRequireOverviewChannelForAsk(event.getGuild(), event.getChannel()); + if (maybeOverviewChannel.isEmpty()) { + return; + } + TextChannel overviewChannel = maybeOverviewChannel.orElseThrow(); + + overviewChannel.createThreadChannel("[%s] %s".formatted(category, title)) .flatMap(threadChannel -> handleEvent(event, threadChannel, event.getMember(), title, category)) .queue(any -> { 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 7e86c6974a..b7f5e77f1a 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 @@ -110,7 +110,7 @@ boolean handleIsHelpThread(@NotNull IReplyCallback event) { if (event.getChannelType() == ChannelType.GUILD_PUBLIC_THREAD) { ThreadChannel thread = event.getThreadChannel(); - if (isStagingChannelName.test(thread.getParentChannel().getName())) { + if (isOverviewChannelName.test(thread.getParentChannel().getName())) { return true; } } @@ -181,6 +181,31 @@ static boolean isTitleValid(@NotNull CharSequence title) { && titleCompact.length() <= TITLE_COMPACT_LENGTH_MAX; } + @NotNull + Optional handleRequireOverviewChannelForAsk(@NotNull Guild guild, + @NotNull MessageChannel respondTo) { + Predicate isChannelName = this::isOverviewChannelName; + String channelPattern = this.getOverviewChannelPattern(); + + Optional maybeChannel = guild.getTextChannelCache() + .stream() + .filter(channel -> isChannelName.test(channel.getName())) + .findAny(); + + if (maybeChannel.isEmpty()) { + logger.warn( + "Attempted to create a help thread, did not find the overview channel matching the configured pattern '{}' for guild '{}'", + channelPattern, guild.getName()); + + respondTo.sendMessage( + "Sorry, I was unable to locate the overview channel. The server seems wrongly configured, please contact a moderator.") + .queue(); + return Optional.empty(); + } + + return maybeChannel; + } + private record HelpThreadName(@Nullable String category, @NotNull String title) { static @NotNull HelpThreadName ofChannelName(@NotNull CharSequence channelName) { Matcher matcher = EXTRACT_CATEGORY_TITLE_PATTERN.matcher(channelName); diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpThreadOverviewUpdater.java b/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpThreadOverviewUpdater.java index 123387a656..0a79750c1a 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpThreadOverviewUpdater.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpThreadOverviewUpdater.java @@ -47,7 +47,7 @@ public final class HelpThreadOverviewUpdater extends MessageReceiverAdapter impl * @param helper the helper to use */ public HelpThreadOverviewUpdater(@NotNull Config config, @NotNull HelpSystemHelper helper) { - super(Pattern.compile(config.getHelpSystem().getStagingChannelPattern())); + super(Pattern.compile(config.getHelpSystem().getOverviewChannelPattern())); allCategories = config.getHelpSystem().getCategories(); this.helper = helper; @@ -82,28 +82,18 @@ public void onMessageReceived(@NotNull MessageReceivedEvent event) { } private void updateOverviewForGuild(@NotNull Guild guild) { - Optional maybeStagingChannel = - handleRequireChannel(ChannelType.STAGING, guild); - Optional maybeOverviewChannel = - handleRequireChannel(ChannelType.OVERVIEW, guild); + Optional maybeOverviewChannel = handleRequireOverviewChannel(guild); - if (maybeStagingChannel.isEmpty() || maybeOverviewChannel.isEmpty()) { + if (maybeOverviewChannel.isEmpty()) { return; } - updateOverview(maybeStagingChannel.orElseThrow(), maybeOverviewChannel.orElseThrow()); + updateOverview(maybeOverviewChannel.orElseThrow()); } - private @NotNull Optional handleRequireChannel(@NotNull ChannelType channelType, - @NotNull Guild guild) { - Predicate isChannelName = switch (channelType) { - case OVERVIEW -> helper::isOverviewChannelName; - case STAGING -> helper::isStagingChannelName; - }; - String channelPattern = switch (channelType) { - case OVERVIEW -> helper.getOverviewChannelPattern(); - case STAGING -> helper.getStagingChannelPattern(); - }; + private @NotNull Optional handleRequireOverviewChannel(@NotNull Guild guild) { + Predicate isChannelName = helper::isOverviewChannelName; + String channelPattern = helper.getOverviewChannelPattern(); Optional maybeChannel = guild.getTextChannelCache() .stream() @@ -113,18 +103,17 @@ private void updateOverviewForGuild(@NotNull Guild guild) { if (maybeChannel.isEmpty()) { logger.warn( "Unable to update help thread overview, did not find a {} channel matching the configured pattern '{}' for guild '{}'", - channelType, channelPattern, guild.getName()); + ChannelType.OVERVIEW, channelPattern, guild.getName()); return Optional.empty(); } return maybeChannel; } - private void updateOverview(@NotNull IThreadContainer stagingChannel, - @NotNull MessageChannel overviewChannel) { + private void updateOverview(@NotNull TextChannel overviewChannel) { logger.debug("Updating overview of active questions"); - List activeThreads = stagingChannel.getThreadChannels() + List activeThreads = overviewChannel.getThreadChannels() .stream() .filter(Predicate.not(ThreadChannel::isArchived)) .toList(); diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/help/ImplicitAskListener.java b/application/src/main/java/org/togetherjava/tjbot/commands/help/ImplicitAskListener.java index 30007fb55e..5af0f1ae68 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/help/ImplicitAskListener.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/help/ImplicitAskListener.java @@ -85,8 +85,14 @@ public void onMessageReceived(@NotNull MessageReceivedEvent event) { String title = createTitle(message.getContentDisplay()); - TextChannel helpStagingChannel = event.getTextChannel(); - helpStagingChannel.createThreadChannel(title) + Optional maybeOverviewChannel = + helper.handleRequireOverviewChannelForAsk(event.getGuild(), event.getChannel()); + if (maybeOverviewChannel.isEmpty()) { + return; + } + TextChannel overviewChannel = maybeOverviewChannel.orElseThrow(); + + overviewChannel.createThreadChannel(title) .flatMap(threadChannel -> handleEvent(threadChannel, message, title)) .queue(any -> { }, ImplicitAskListener::handleFailure); @@ -143,7 +149,6 @@ private Optional getLastHelpThreadIfOnCooldown(long userId) { } return HelpSystemHelper.isTitleValid(titleCandidate) ? titleCandidate : "Untitled"; - } private @NotNull RestAction handleEvent(@NotNull ThreadChannel threadChannel,