// Import necessary modules import { EmbedBuilder } from "discord.js"; import { DebugBuilder } from "../../modules/debugger.mjs"; import { parse } from "node-html-parser"; 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; export class DRBEmbedBuilder extends EmbedBuilder { constructor() { super(); this.setTimestamp(); 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; 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) { postText = `*This post has no content* [Direct Link](${post.link})`; } postContent = postText; // Check for embedded YouTube videos and add the first four as links const ytVideos = content.match(youtubeVideoRegex); if (ytVideos) { ytVideos.slice(0, 4).forEach((ytVideo) => { 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)); if (imageLinks.length > 0) { post.image = imageLinks[0]; } } const postId = post.postId; const postPubDate = new Date(post.pubDate || Date.now()).toISOString(); const postSourceLink = source.title; const postImage = post.image; log.DEBUG("Post content: ", postContent); try { const rssMessage = new DRBEmbedBuilder() .setColor(0x0099ff) .setTitle(postTitle) .setURL(postLink) .addFields({ name: "Source", value: postSourceLink, inline: true }) .addFields({ name: "Published", value: postPubDate, inline: true }); if (postImage) { log.DEBUG("Image from post:", postImage); rssMessage.setImage(postImage); } postContent = postContent.slice(0, 4090).trim(); if (postContent) rssMessage.setDescription(postContent); const channelResponse = channel.send({ embeds: [rssMessage] }); log.DEBUG("Channel send response", channelResponse); return channelResponse; } catch (err) { log.ERROR( "Error sending message: ", postTitle, postId, postContent, postPubDate, err, ); return err; } };