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
1 change: 1 addition & 0 deletions application/config.json.template
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@
}
],
"fallbackChannelPattern": "java-news-and-changes",
"videoLinkPattern": "http(s)?://www\\.youtube.com.*",
"pollIntervalInMinutes": 10
},
"memberCountCategoryPattern": "Info",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,23 @@
public record RSSFeedsConfig(@JsonProperty(value = "feeds", required = true) List<RSSFeed> feeds,
@JsonProperty(value = "fallbackChannelPattern",
required = true) String fallbackChannelPattern,
@JsonProperty(value = "videoLinkPattern", required = true) String videoLinkPattern,
@JsonProperty(value = "pollIntervalInMinutes", required = true) int pollIntervalInMinutes) {

/**
* Constructs a new {@link RSSFeedsConfig}.
*
* @param feeds The list of RSS feeds to subscribe to.
* @param fallbackChannelPattern The pattern used to identify the fallback text channel to use.
* @param videoLinkPattern pattern determining if a link is a video. It is then posted in a way
* to support Discord video embeds.
* @param pollIntervalInMinutes The interval (in minutes) for polling the RSS feeds for updates.
* @throws NullPointerException if any of the parameters (feeds or fallbackChannelPattern) are
* null
*/
public RSSFeedsConfig {
Objects.requireNonNull(feeds);
Objects.requireNonNull(fallbackChannelPattern);
Objects.requireNonNull(videoLinkPattern);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import com.apptasticsoftware.rssreader.RssReader;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import net.dv8tion.jda.api.utils.cache.SnowflakeCacheView;
import net.dv8tion.jda.api.utils.messages.MessageCreateData;
import org.apache.commons.text.StringEscapeUtils;
import org.jetbrains.annotations.Nullable;
import org.jooq.tools.StringUtils;
Expand Down Expand Up @@ -79,6 +79,7 @@ public final class RSSHandlerRoutine implements Routine {
private final RssReader rssReader;
private final RSSFeedsConfig config;
private final Predicate<String> fallbackChannelPattern;
private final Predicate<String> isVideoLink;
private final Map<RSSFeed, Predicate<String>> targetChannelPatterns;
private final int interval;
private final Database database;
Expand All @@ -95,6 +96,7 @@ public RSSHandlerRoutine(Config config, Database database) {
this.database = database;
this.fallbackChannelPattern =
Pattern.compile(this.config.fallbackChannelPattern()).asMatchPredicate();
isVideoLink = Pattern.compile(this.config.videoLinkPattern()).asMatchPredicate();
this.targetChannelPatterns = new HashMap<>();
this.config.feeds().forEach(feed -> {
if (feed.targetChannelPattern() != null) {
Expand Down Expand Up @@ -155,7 +157,7 @@ private void sendRSS(JDA jda, RSSFeed feedConfig) {
}
rssItems.reversed()
.stream()
.filter(shouldItemBePosted.get())
.filter(shouldItemBePosted.orElseThrow())
.forEachOrdered(item -> postItem(textChannels, item, feedConfig));
}

Expand Down Expand Up @@ -241,8 +243,8 @@ private Optional<ZonedDateTime> getLatestPostDateFromItems(List<Item> items,
* @param feedConfig the RSS feed configuration
*/
private void postItem(List<TextChannel> textChannels, Item rssItem, RSSFeed feedConfig) {
MessageEmbed embed = constructEmbedMessage(rssItem, feedConfig).build();
textChannels.forEach(channel -> channel.sendMessageEmbeds(List.of(embed)).queue());
MessageCreateData message = constructMessage(rssItem, feedConfig);
textChannels.forEach(channel -> channel.sendMessage(message).queue());
}

/**
Expand Down Expand Up @@ -346,13 +348,18 @@ private List<TextChannel> getTextChannelsFromFeed(JDA jda, RSSFeed feed) {
}

/**
* Provides the {@link EmbedBuilder} from an RSS item used for sending RSS posts.
* Provides the message from an RSS item used for sending RSS posts.
*
* @param item the RSS item to construct the embed message from
* @param feedConfig the configuration of the RSS feed
* @return the constructed {@link EmbedBuilder} containing information from the RSS item
* @return the constructed message containing information from the RSS item
*/
private static EmbedBuilder constructEmbedMessage(Item item, RSSFeed feedConfig) {
private MessageCreateData constructMessage(Item item, RSSFeed feedConfig) {
if (item.getLink().filter(isVideoLink).isPresent()) {
// Automatic video previews are created on normal messages, not on embeds
return MessageCreateData.fromContent(item.getLink().orElseThrow());
}

final EmbedBuilder embedBuilder = new EmbedBuilder();
String title = item.getTitle().orElse("No title");
String titleLink = item.getLink().orElse("");
Expand Down Expand Up @@ -381,7 +388,7 @@ private static EmbedBuilder constructEmbedMessage(Item item, RSSFeed feedConfig)
embedBuilder.setDescription("No description");
}

return embedBuilder;
return MessageCreateData.fromEmbeds(embedBuilder.build());
}

/**
Expand Down