Skip to content

Commit d969411

Browse files
committed
Moved question threads under overview channel instead of staging channel
1 parent 5e28950 commit d969411

File tree

4 files changed

+54
-27
lines changed

4 files changed

+54
-27
lines changed

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import org.togetherjava.tjbot.commands.SlashCommandVisibility;
1717
import org.togetherjava.tjbot.config.Config;
1818

19+
import java.util.Optional;
20+
1921
import static org.togetherjava.tjbot.commands.help.HelpSystemHelper.TITLE_COMPACT_LENGTH_MAX;
2022
import static org.togetherjava.tjbot.commands.help.HelpSystemHelper.TITLE_COMPACT_LENGTH_MIN;
2123

@@ -83,8 +85,14 @@ public void onSlashCommand(@NotNull SlashCommandInteractionEvent event) {
8385
return;
8486
}
8587

86-
TextChannel helpStagingChannel = event.getTextChannel();
87-
helpStagingChannel.createThreadChannel("[%s] %s".formatted(category, title))
88+
Optional<TextChannel> maybeOverviewChannel =
89+
helper.handleRequireOverviewChannelForAsk(event.getGuild(), event.getChannel());
90+
if (maybeOverviewChannel.isEmpty()) {
91+
return;
92+
}
93+
TextChannel overviewChannel = maybeOverviewChannel.orElseThrow();
94+
95+
overviewChannel.createThreadChannel("[%s] %s".formatted(category, title))
8896
.flatMap(threadChannel -> handleEvent(event, threadChannel, event.getMember(), title,
8997
category))
9098
.queue(any -> {

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ boolean handleIsHelpThread(@NotNull IReplyCallback event) {
110110
if (event.getChannelType() == ChannelType.GUILD_PUBLIC_THREAD) {
111111
ThreadChannel thread = event.getThreadChannel();
112112

113-
if (isStagingChannelName.test(thread.getParentChannel().getName())) {
113+
if (isOverviewChannelName.test(thread.getParentChannel().getName())) {
114114
return true;
115115
}
116116
}
@@ -181,6 +181,31 @@ static boolean isTitleValid(@NotNull CharSequence title) {
181181
&& titleCompact.length() <= TITLE_COMPACT_LENGTH_MAX;
182182
}
183183

184+
@NotNull
185+
Optional<TextChannel> handleRequireOverviewChannelForAsk(@NotNull Guild guild,
186+
@NotNull MessageChannel respondTo) {
187+
Predicate<String> isChannelName = this::isOverviewChannelName;
188+
String channelPattern = this.getOverviewChannelPattern();
189+
190+
Optional<TextChannel> maybeChannel = guild.getTextChannelCache()
191+
.stream()
192+
.filter(channel -> isChannelName.test(channel.getName()))
193+
.findAny();
194+
195+
if (maybeChannel.isEmpty()) {
196+
logger.warn(
197+
"Attempted to create a help thread, did not find the overview channel matching the configured pattern '{}' for guild '{}'",
198+
channelPattern, guild.getName());
199+
200+
respondTo.sendMessage(
201+
"Sorry, I was unable to locate the overview channel. The server seems wrongly configured, please contact a moderator.")
202+
.queue();
203+
return Optional.empty();
204+
}
205+
206+
return maybeChannel;
207+
}
208+
184209
private record HelpThreadName(@Nullable String category, @NotNull String title) {
185210
static @NotNull HelpThreadName ofChannelName(@NotNull CharSequence channelName) {
186211
Matcher matcher = EXTRACT_CATEGORY_TITLE_PATTERN.matcher(channelName);

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

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public final class HelpThreadOverviewUpdater extends MessageReceiverAdapter impl
4747
* @param helper the helper to use
4848
*/
4949
public HelpThreadOverviewUpdater(@NotNull Config config, @NotNull HelpSystemHelper helper) {
50-
super(Pattern.compile(config.getHelpSystem().getStagingChannelPattern()));
50+
super(Pattern.compile(config.getHelpSystem().getOverviewChannelPattern()));
5151

5252
allCategories = config.getHelpSystem().getCategories();
5353
this.helper = helper;
@@ -82,28 +82,18 @@ public void onMessageReceived(@NotNull MessageReceivedEvent event) {
8282
}
8383

8484
private void updateOverviewForGuild(@NotNull Guild guild) {
85-
Optional<TextChannel> maybeStagingChannel =
86-
handleRequireChannel(ChannelType.STAGING, guild);
87-
Optional<TextChannel> maybeOverviewChannel =
88-
handleRequireChannel(ChannelType.OVERVIEW, guild);
85+
Optional<TextChannel> maybeOverviewChannel = handleRequireOverviewChannel(guild);
8986

90-
if (maybeStagingChannel.isEmpty() || maybeOverviewChannel.isEmpty()) {
87+
if (maybeOverviewChannel.isEmpty()) {
9188
return;
9289
}
9390

94-
updateOverview(maybeStagingChannel.orElseThrow(), maybeOverviewChannel.orElseThrow());
91+
updateOverview(maybeOverviewChannel.orElseThrow());
9592
}
9693

97-
private @NotNull Optional<TextChannel> handleRequireChannel(@NotNull ChannelType channelType,
98-
@NotNull Guild guild) {
99-
Predicate<String> isChannelName = switch (channelType) {
100-
case OVERVIEW -> helper::isOverviewChannelName;
101-
case STAGING -> helper::isStagingChannelName;
102-
};
103-
String channelPattern = switch (channelType) {
104-
case OVERVIEW -> helper.getOverviewChannelPattern();
105-
case STAGING -> helper.getStagingChannelPattern();
106-
};
94+
private @NotNull Optional<TextChannel> handleRequireOverviewChannel(@NotNull Guild guild) {
95+
Predicate<String> isChannelName = helper::isOverviewChannelName;
96+
String channelPattern = helper.getOverviewChannelPattern();
10797

10898
Optional<TextChannel> maybeChannel = guild.getTextChannelCache()
10999
.stream()
@@ -113,18 +103,17 @@ private void updateOverviewForGuild(@NotNull Guild guild) {
113103
if (maybeChannel.isEmpty()) {
114104
logger.warn(
115105
"Unable to update help thread overview, did not find a {} channel matching the configured pattern '{}' for guild '{}'",
116-
channelType, channelPattern, guild.getName());
106+
ChannelType.OVERVIEW, channelPattern, guild.getName());
117107
return Optional.empty();
118108
}
119109

120110
return maybeChannel;
121111
}
122112

123-
private void updateOverview(@NotNull IThreadContainer stagingChannel,
124-
@NotNull MessageChannel overviewChannel) {
113+
private void updateOverview(@NotNull TextChannel overviewChannel) {
125114
logger.debug("Updating overview of active questions");
126115

127-
List<ThreadChannel> activeThreads = stagingChannel.getThreadChannels()
116+
List<ThreadChannel> activeThreads = overviewChannel.getThreadChannels()
128117
.stream()
129118
.filter(Predicate.not(ThreadChannel::isArchived))
130119
.toList();

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,14 @@ public void onMessageReceived(@NotNull MessageReceivedEvent event) {
8585

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

88-
TextChannel helpStagingChannel = event.getTextChannel();
89-
helpStagingChannel.createThreadChannel(title)
88+
Optional<TextChannel> maybeOverviewChannel =
89+
helper.handleRequireOverviewChannelForAsk(event.getGuild(), event.getChannel());
90+
if (maybeOverviewChannel.isEmpty()) {
91+
return;
92+
}
93+
TextChannel overviewChannel = maybeOverviewChannel.orElseThrow();
94+
95+
overviewChannel.createThreadChannel(title)
9096
.flatMap(threadChannel -> handleEvent(threadChannel, message, title))
9197
.queue(any -> {
9298
}, ImplicitAskListener::handleFailure);
@@ -143,7 +149,6 @@ private Optional<HelpThread> getLastHelpThreadIfOnCooldown(long userId) {
143149
}
144150

145151
return HelpSystemHelper.isTitleValid(titleCandidate) ? titleCandidate : "Untitled";
146-
147152
}
148153

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

0 commit comments

Comments
 (0)