Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -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;

Expand Down Expand Up @@ -83,8 +85,14 @@ public void onSlashCommand(@NotNull SlashCommandInteractionEvent event) {
return;
}

TextChannel helpStagingChannel = event.getTextChannel();
helpStagingChannel.createThreadChannel("[%s] %s".formatted(category, title))
Optional<TextChannel> 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 -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -181,6 +181,31 @@ static boolean isTitleValid(@NotNull CharSequence title) {
&& titleCompact.length() <= TITLE_COMPACT_LENGTH_MAX;
}

@NotNull
Optional<TextChannel> handleRequireOverviewChannelForAsk(@NotNull Guild guild,
@NotNull MessageChannel respondTo) {
Predicate<String> isChannelName = this::isOverviewChannelName;
String channelPattern = this.getOverviewChannelPattern();

Optional<TextChannel> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -82,28 +82,18 @@ public void onMessageReceived(@NotNull MessageReceivedEvent event) {
}

private void updateOverviewForGuild(@NotNull Guild guild) {
Optional<TextChannel> maybeStagingChannel =
handleRequireChannel(ChannelType.STAGING, guild);
Optional<TextChannel> maybeOverviewChannel =
handleRequireChannel(ChannelType.OVERVIEW, guild);
Optional<TextChannel> maybeOverviewChannel = handleRequireOverviewChannel(guild);

if (maybeStagingChannel.isEmpty() || maybeOverviewChannel.isEmpty()) {
if (maybeOverviewChannel.isEmpty()) {
return;
}

updateOverview(maybeStagingChannel.orElseThrow(), maybeOverviewChannel.orElseThrow());
updateOverview(maybeOverviewChannel.orElseThrow());
}

private @NotNull Optional<TextChannel> handleRequireChannel(@NotNull ChannelType channelType,
@NotNull Guild guild) {
Predicate<String> 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<TextChannel> handleRequireOverviewChannel(@NotNull Guild guild) {
Predicate<String> isChannelName = helper::isOverviewChannelName;
String channelPattern = helper.getOverviewChannelPattern();

Optional<TextChannel> maybeChannel = guild.getTextChannelCache()
.stream()
Expand All @@ -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<ThreadChannel> activeThreads = stagingChannel.getThreadChannels()
List<ThreadChannel> activeThreads = overviewChannel.getThreadChannels()
.stream()
.filter(Predicate.not(ThreadChannel::isArchived))
.toList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,14 @@ public void onMessageReceived(@NotNull MessageReceivedEvent event) {

String title = createTitle(message.getContentDisplay());

TextChannel helpStagingChannel = event.getTextChannel();
helpStagingChannel.createThreadChannel(title)
Optional<TextChannel> 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);
Expand Down Expand Up @@ -143,7 +149,6 @@ private Optional<HelpThread> getLastHelpThreadIfOnCooldown(long userId) {
}

return HelpSystemHelper.isTitleValid(titleCandidate) ? titleCandidate : "Untitled";

}

private @NotNull RestAction<?> handleEvent(@NotNull ThreadChannel threadChannel,
Expand Down