generated from nhcarrigan/template
This commit is contained in:
@@ -9,6 +9,7 @@ import { scheduleJob } from "node-schedule";
|
|||||||
import { handleMessageCreate } from "./events/handleMessageCreate.js";
|
import { handleMessageCreate } from "./events/handleMessageCreate.js";
|
||||||
import {
|
import {
|
||||||
postFreeCodeCampNews,
|
postFreeCodeCampNews,
|
||||||
|
postHackerNews,
|
||||||
} from "./modules/postNews.js";
|
} from "./modules/postNews.js";
|
||||||
import { respondToDm } from "./modules/respondToDm.js";
|
import { respondToDm } from "./modules/respondToDm.js";
|
||||||
import { instantiateServer } from "./server/serve.js";
|
import { instantiateServer } from "./server/serve.js";
|
||||||
@@ -26,6 +27,7 @@ const amari: Amari = {
|
|||||||
partials: [ Partials.Channel ] }),
|
partials: [ Partials.Channel ] }),
|
||||||
lastRssItems: {
|
lastRssItems: {
|
||||||
freeCodeCamp: null,
|
freeCodeCamp: null,
|
||||||
|
hackerNews: null,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -34,6 +36,7 @@ amari.discord.once(Events.ClientReady, () => {
|
|||||||
`Authenticated to Discord as ${amari.discord.user?.username ?? "unknown"}`);
|
`Authenticated to Discord as ${amari.discord.user?.username ?? "unknown"}`);
|
||||||
scheduleJob("post news", "0 * * * *", async() => {
|
scheduleJob("post news", "0 * * * *", async() => {
|
||||||
await postFreeCodeCampNews(amari);
|
await postFreeCodeCampNews(amari);
|
||||||
|
await postHackerNews(amari);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -10,5 +10,6 @@ export interface Amari {
|
|||||||
discord: Client;
|
discord: Client;
|
||||||
lastRssItems: {
|
lastRssItems: {
|
||||||
freeCodeCamp: string | null;
|
freeCodeCamp: string | null;
|
||||||
|
hackerNews: string | null;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
+26
-1
@@ -37,4 +37,29 @@ interface FreeCodeCampRSS {
|
|||||||
ttl: string;
|
ttl: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type { FreeCodeCampRSS };
|
interface HackerNewsRSS {
|
||||||
|
items: Array<{
|
||||||
|
"creator": string;
|
||||||
|
"title": string;
|
||||||
|
"link": string;
|
||||||
|
"pubDate": string;
|
||||||
|
"dc:creator": string;
|
||||||
|
"comments": string;
|
||||||
|
"content": string;
|
||||||
|
"contentSnippet": string;
|
||||||
|
"guid": string;
|
||||||
|
"isoDate": string;
|
||||||
|
}>;
|
||||||
|
feedUrl: string;
|
||||||
|
paginationLinks: {
|
||||||
|
self: string;
|
||||||
|
};
|
||||||
|
title: string;
|
||||||
|
description: string;
|
||||||
|
generator: string;
|
||||||
|
link: string;
|
||||||
|
lastBuildDate: string;
|
||||||
|
docs: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export type { FreeCodeCampRSS, HackerNewsRSS };
|
||||||
|
|||||||
+47
-2
@@ -10,7 +10,10 @@ import Parser from "rss-parser";
|
|||||||
import { ids } from "../config/ids.js";
|
import { ids } from "../config/ids.js";
|
||||||
import { logger } from "../utils/logger.js";
|
import { logger } from "../utils/logger.js";
|
||||||
import type { Amari } from "../interfaces/amari.js";
|
import type { Amari } from "../interfaces/amari.js";
|
||||||
import type { FreeCodeCampRSS } from "../interfaces/rss.js";
|
import type {
|
||||||
|
FreeCodeCampRSS,
|
||||||
|
HackerNewsRSS,
|
||||||
|
} from "../interfaces/rss.js";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fetches the RSS feed from freeCodeCamp News and posts the latest updates.
|
* Fetches the RSS feed from freeCodeCamp News and posts the latest updates.
|
||||||
@@ -54,4 +57,46 @@ const postFreeCodeCampNews = async(amari: Amari): Promise<void> => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export { postFreeCodeCampNews };
|
/**
|
||||||
|
* Fetches the RSS feed from HackerNews and posts the latest updates.
|
||||||
|
* @param amari - Amari's instance.
|
||||||
|
*/
|
||||||
|
const postHackerNews = async(amari: Amari): Promise<void> => {
|
||||||
|
try {
|
||||||
|
const parser = new Parser<HackerNewsRSS, HackerNewsRSS["items"]>();
|
||||||
|
const { items }
|
||||||
|
= await parser.parseURL("https://hnrss.org/newest?link=comments");
|
||||||
|
if (amari.lastRssItems.hackerNews === null) {
|
||||||
|
amari.lastRssItems.hackerNews = items[0]?.guid ?? null;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const lastIndex = items.findIndex((item) => {
|
||||||
|
return item.guid === amari.lastRssItems.hackerNews;
|
||||||
|
});
|
||||||
|
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 hackernews module", error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export { postFreeCodeCampNews, postHackerNews };
|
||||||
|
|||||||
Reference in New Issue
Block a user