Linting
All checks were successful
release-tag / release-image (push) Successful in 1m52s
Lint JavaScript/Node.js / lint-js (push) Successful in 11s
DRB Tests / drb_mocha_tests (push) Successful in 29s

This commit is contained in:
Logan Cusano
2024-08-11 15:57:46 -04:00
parent 5cd47378d6
commit 117cbea67f
37 changed files with 2273 additions and 1738 deletions

View File

@@ -1,37 +1,39 @@
// Import necessary modules
import { EmbedBuilder } from 'discord.js';
import { EmbedBuilder } from "discord.js";
import { DebugBuilder } from "../../modules/debugger.mjs";
import { parse } from "node-html-parser";
import { config } from 'dotenv';
import { config } from "dotenv";
// Load environment variables
config();
const log = new DebugBuilder("server", "discordBot.modules.rssWrappers");
const imageRegex = /(http(s?):)([/|.|\w|\s|-])*((\.(?:jpg|gif|png|webm))|(\/gallery\/(?:[/|.|\w|\s|-])*))/g;
const youtubeVideoRegex = /((?:https?:)?\/\/)?((?:www|m)\.)?((?:youtube(-nocookie)?\.com|youtu.be))(\/(?:[\w\-]+\?v=|embed\/|v\/)?)([\w\-]+)/g;
const imageRegex =
/(http(s?):)([/|.|\w|\s|-])*((\.(?:jpg|gif|png|webm))|(\/gallery\/(?:[/|.|\w|\s|-])*))/g;
const youtubeVideoRegex =
/((?:https?:)?\/\/)?((?:www|m)\.)?((?:youtube(-nocookie)?\.com|youtu.be))(\/(?:[\w\-]+\?v=|embed\/|v\/)?)([\w\-]+)/g;
export class DRBEmbedBuilder extends EmbedBuilder {
constructor() {
super();
this.setTimestamp();
this.setFooter({ text: 'Brought to you by Emmelia.' });
this.setFooter({ text: "Brought to you by Emmelia." });
}
}
export const sendPost = (post, source, channel) => {
log.DEBUG("Sending post from source: ", post, source);
const postTitle = String(post.title).substring(0, 150);
const postLink = post.link;
let postContent = `*This post has no content* [Direct Link](${post.link})`;
if (post.content || post['content:encoded']) {
const content = post['content:encoded'] ?? post.content;
if (post.content || post["content:encoded"]) {
const content = post["content:encoded"] ?? post.content;
const parsedContent = parse(content);
let postText = parsedContent.text.trim();
if (postText.length >= 3800) {
postText = `${postText.slice(0, 3800).substring(0, postText.lastIndexOf(" "))} [...](${post.link})`;
} else if (postText.length === 0) {
@@ -43,16 +45,18 @@ export const sendPost = (post, source, channel) => {
const ytVideos = content.match(youtubeVideoRegex);
if (ytVideos) {
ytVideos.slice(0, 4).forEach((ytVideo) => {
if (ytVideo.includes("embed")) ytVideo = ytVideo.replace("embed/", "watch?v=");
if (ytVideo.includes("embed"))
ytVideo = ytVideo.replace("embed/", "watch?v=");
postContent += `\nEmbedded Video from Post: [YouTube](${ytVideo})`;
});
}
// Extract the first image link if available
const imageLinks = parsedContent.querySelectorAll("a")
.map(link => link.getAttribute("href"))
.filter(href => href && href.match(imageRegex));
const imageLinks = parsedContent
.querySelectorAll("a")
.map((link) => link.getAttribute("href"))
.filter((href) => href && href.match(imageRegex));
if (imageLinks.length > 0) {
post.image = imageLinks[0];
}
@@ -67,11 +71,11 @@ export const sendPost = (post, source, channel) => {
try {
const rssMessage = new DRBEmbedBuilder()
.setColor(0x0099FF)
.setColor(0x0099ff)
.setTitle(postTitle)
.setURL(postLink)
.addFields({ name: 'Source', value: postSourceLink, inline: true })
.addFields({ name: 'Published', value: postPubDate, inline: true });
.addFields({ name: "Source", value: postSourceLink, inline: true })
.addFields({ name: "Published", value: postPubDate, inline: true });
if (postImage) {
log.DEBUG("Image from post:", postImage);
@@ -87,7 +91,14 @@ export const sendPost = (post, source, channel) => {
return channelResponse;
} catch (err) {
log.ERROR("Error sending message: ", postTitle, postId, postContent, postPubDate, err);
log.ERROR(
"Error sending message: ",
postTitle,
postId,
postContent,
postPubDate,
err,
);
return err;
}
};