Skip to content

Commit 4e7b9b6

Browse files
authored
fix(rss): logic for acquiring target channels (#1090)
This commit attempts to change the way a text channel list for RSS feeds gets generated, as it would originally not consider any channels that matched the fallback channel pattern and it would constantly log a warning that would clutter the log channels and skip sending any RSS posts as a result. The method which is responsible for finding the text channels from a given RSS feed configuration now focuses on collecting a list of text channels that match the target channel pattern from the configuration, and if no channels are found, the same collection attempt happens with the fallback channel pattern. In case an empty list is still yielded, a now-improved and more accurate warning message gets logged.
1 parent 2738a98 commit 4e7b9b6

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

application/src/main/java/org/togetherjava/tjbot/features/javamail/RSSHandlerRoutine.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import net.dv8tion.jda.api.JDA;
77
import net.dv8tion.jda.api.entities.MessageEmbed;
88
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
9+
import net.dv8tion.jda.api.utils.cache.SnowflakeCacheView;
910
import org.apache.commons.text.StringEscapeUtils;
1011
import org.jetbrains.annotations.Nullable;
1112
import org.jooq.tools.StringUtils;
@@ -125,8 +126,8 @@ public void runRoutine(@Nonnull JDA jda) {
125126
private void sendRSS(JDA jda, RSSFeed feedConfig) {
126127
List<TextChannel> textChannels = getTextChannelsFromFeed(jda, feedConfig);
127128
if (textChannels.isEmpty()) {
128-
logger.warn("Tried to send an RSS post, got empty response (channel {} not found)",
129-
feedConfig.targetChannelPattern());
129+
logger.warn(
130+
"Tried to send an RSS post, but neither a target channel nor a fallback channel was found.");
130131
return;
131132
}
132133

@@ -326,18 +327,18 @@ private static boolean isValidDateFormat(Item rssItem, RSSFeed feedConfig) {
326327
* @return an {@link List} of the text channels found, or empty if none are found
327328
*/
328329
private List<TextChannel> getTextChannelsFromFeed(JDA jda, RSSFeed feed) {
329-
// Attempt to find the target channel, use the fallback otherwise
330-
if (targetChannelPatterns.containsKey(feed)) {
331-
return jda.getTextChannelCache()
332-
.stream()
333-
.filter(channel -> targetChannelPatterns.get(feed).test(channel.getName()))
334-
.toList();
335-
} else {
336-
return jda.getTextChannelCache()
337-
.stream()
338-
.filter(channel -> fallbackChannelPattern.test(channel.getName()))
339-
.toList();
330+
final SnowflakeCacheView<TextChannel> textChannelCache = jda.getTextChannelCache();
331+
List<TextChannel> textChannels = textChannelCache.stream()
332+
.filter(channel -> targetChannelPatterns.get(feed).test(channel.getName()))
333+
.toList();
334+
335+
if (!textChannels.isEmpty()) {
336+
return textChannels;
340337
}
338+
339+
return textChannelCache.stream()
340+
.filter(channel -> fallbackChannelPattern.test(channel.getName()))
341+
.toList();
341342
}
342343

343344
/**

0 commit comments

Comments
 (0)