feat: syndicate freeCodeCamp news posts

This commit is contained in:
2025-08-20 12:23:53 -07:00
parent 344548c5d2
commit b5c19abb52
7 changed files with 199 additions and 2 deletions
+1
View File
@@ -8,6 +8,7 @@ export const ids = {
channels: {
mentorshipGoalForum: "1400629118110011526",
mentorshipProjectForum: "1400616702265266186",
news: "1407804798677418198",
},
roles: {
nhcarrigan: "1355033209037127771",
+10
View File
@@ -5,7 +5,11 @@
*/
import { Client, GatewayIntentBits, Events, Partials } from "discord.js";
import { scheduleJob } from "node-schedule";
import { handleMessageCreate } from "./events/handleMessageCreate.js";
import {
postFreeCodeCampNews,
} from "./modules/postNews.js";
import { respondToDm } from "./modules/respondToDm.js";
import { instantiateServer } from "./server/serve.js";
import { logger } from "./utils/logger.js";
@@ -20,11 +24,17 @@ const amari: Amari = {
GatewayIntentBits.DirectMessages,
],
partials: [ Partials.Channel ] }),
lastRssItems: {
freeCodeCamp: null,
},
};
amari.discord.once(Events.ClientReady, () => {
void logger.log("debug",
`Authenticated to Discord as ${amari.discord.user?.username ?? "unknown"}`);
scheduleJob("post news", "0 * * * *", async() => {
await postFreeCodeCampNews(amari);
});
});
amari.discord.on(Events.MessageCreate, (message) => {
+4 -1
View File
@@ -7,5 +7,8 @@
import type { Client } from "discord.js";
export interface Amari {
discord: Client;
discord: Client;
lastRssItems: {
freeCodeCamp: string | null;
};
}
+40
View File
@@ -0,0 +1,40 @@
/**
* @copyright NHCarrigan
* @license Naomi's Public License
* @author Naomi Carrigan
*/
/* eslint-disable @typescript-eslint/naming-convention -- Gonna have weird properties in this file. */
interface FreeCodeCampRSS {
items: Array<{
"creator": string;
"title": string;
"link": string;
"pubDate": string;
"content:encoded": string;
"dc:creator": string;
"content": string;
"contentSnippet": string;
"guid": string;
"categories": Array<string>;
"isoDate": Date;
}>;
feedUrl: string;
image: {
link: string;
url: string;
title: string;
};
paginationLinks: {
self: string;
};
title: string;
description: string;
generator: string;
link: string;
lastBuildDate: string;
ttl: string;
}
export type { FreeCodeCampRSS };
+57
View File
@@ -0,0 +1,57 @@
/**
* @copyright NHCarrigan
* @license Naomi's Public License
* @author Naomi Carrigan
*/
import { ChannelType } from "discord.js";
// eslint-disable-next-line @typescript-eslint/naming-convention -- Importing a class.
import Parser from "rss-parser";
import { ids } from "../config/ids.js";
import { logger } from "../utils/logger.js";
import type { Amari } from "../interfaces/amari.js";
import type { FreeCodeCampRSS } from "../interfaces/rss.js";
/**
* Fetches the RSS feed from freeCodeCamp News and posts the latest updates.
* @param amari - Amari's instance.
*/
const postFreeCodeCampNews = async(amari: Amari): Promise<void> => {
try {
const parser = new Parser<FreeCodeCampRSS, FreeCodeCampRSS["items"]>();
const { items }
= await parser.parseURL("https://www.freecodecamp.org/news/rss");
if (amari.lastRssItems.freeCodeCamp === null) {
amari.lastRssItems.freeCodeCamp = items[0]?.guid ?? null;
return;
}
const lastIndex = items.findIndex((item) => {
return item.guid === amari.lastRssItems.freeCodeCamp;
});
const latestPosts
= lastIndex > -1
? items.slice(0, Math.min(lastIndex, 5))
: items.slice(0, 5);
const channel
= amari.discord.channels.cache.get(ids.channels.news)
?? await amari.discord.channels.fetch(ids.channels.news);
if (channel === null) {
throw new Error("Cannot find news channel.");
}
if (!channel.isSendable()) {
throw new Error("News channel is not sendable.");
}
await Promise.all(latestPosts.map(async(post) => {
const sent = await channel.send(post.link);
if (channel.type === ChannelType.GuildAnnouncement) {
await sent.crosspost();
}
}));
} catch (error) {
if (error instanceof Error) {
await logger.error("post freecodecamp news module", error);
}
}
};
export { postFreeCodeCampNews };