From 1067c1d50628ca2fc12d42c0e68e116b886ce86f Mon Sep 17 00:00:00 2001 From: Supinic Date: Sun, 17 Nov 2024 17:57:54 +0100 Subject: [PATCH] `$subscribe` - add Brighter Shores (#109) --- .../subscribe/event-types/brighter-shores.js | 63 +++++++++++++++++++ commands/subscribe/event-types/index.js | 1 + commands/subscribe/event_subscription.sql | 5 +- 3 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 commands/subscribe/event-types/brighter-shores.js diff --git a/commands/subscribe/event-types/brighter-shores.js b/commands/subscribe/event-types/brighter-shores.js new file mode 100644 index 000000000..c278b847d --- /dev/null +++ b/commands/subscribe/event-types/brighter-shores.js @@ -0,0 +1,63 @@ +const { parseRSS } = require("../../../utils/command-utils.js"); + +const url = "https://brightershores.pro/rss.xml"; +const BRIGHTER_SHORES_LAST_UPDATE_DATE = "brighter-shores-last-update-date"; + +module.exports = { + name: "Brighter Shores", + aliases: ["BS", "brighter shores"], + notes: "Posts update news about Brighter Shores", + channelSpecificMention: true, + response: { + added: "You will now be pinged whenever a new BS update is published.", + removed: "You will no longer receive pings when a new BS update is published." + }, + generic: true, + cronExpression: "0 */5 * * * *", + subName: "BS update", + type: "custom", + process: async () => { + const response = await sb.Got.get("GenericAPI")({ + url, + responseType: "text", + throwHttpErrors: true + }); + + if (!response.ok) { + return; + } + + const data = await parseRSS(response.body); + const eligibleUpdates = data.items + .filter(i => i.link.includes("updates")) + .sort((a, b) => new sb.Date(b.isoDate) - new sb.Date(a.isoDate)); + + const previousUpdateDateString = await sb.Cache.getByPrefix(BRIGHTER_SHORES_LAST_UPDATE_DATE) + if (!previousUpdateDateString) { + // If there is no date, attempt to populate the latest one, and do not post a notification (first time only) + if (eligibleUpdates.length === 0) { // If there are no updates, do nothing and wait for a later update + return; + } + + await sb.Cache.setByPrefix(BRIGHTER_SHORES_LAST_UPDATE_DATE, eligibleUpdates[0].isoDate); + return; + } + + const previousUpdateDate = new sb.Date(previousUpdateDateString) + const newUpdates = eligibleUpdates.filter(i => new sb.Date(i.isoDate) > previousUpdateDate); + if (newUpdates.length === 0) { // No new updates, do nothing + return; + } + + await sb.Cache.setByPrefix(BRIGHTER_SHORES_LAST_UPDATE_DATE, newUpdates[0].isoDate, { + expiry: 14 * 864e5 // 14 days + }); + + const updateString = newUpdates.map(i => `${i.title} ${i.link}`).join(" -- "); + const noun = (newUpdates.length === 1) ? "update" : "updates"; + + return { + message: `New Brighter Shores ${noun}! ${updateString}` + }; + } +}; diff --git a/commands/subscribe/event-types/index.js b/commands/subscribe/event-types/index.js index 130ca6c2e..7774aa641 100644 --- a/commands/subscribe/event-types/index.js +++ b/commands/subscribe/event-types/index.js @@ -1,4 +1,5 @@ const eventTypeFileList = [ + "brighter-shores", "bun", "changelog", "channel-live", diff --git a/commands/subscribe/event_subscription.sql b/commands/subscribe/event_subscription.sql index ace7f31ce..ee6f13b6d 100644 --- a/commands/subscribe/event_subscription.sql +++ b/commands/subscribe/event_subscription.sql @@ -12,10 +12,9 @@ CREATE TABLE IF NOT EXISTS `data`.`Event_Subscription` ( PRIMARY KEY (`ID`) USING BTREE, UNIQUE INDEX `User_Alias_Event` (`User_Alias`, `Type`) USING BTREE, INDEX `FK_Event_Subscription_Channel` (`Channel`) USING BTREE, - INDEX `FK_Event_Subscription_chat_data.Platform` (`Platform`) USING BTREE, + INDEX `Event_Subscription_chat_data.Platform` (`Platform`) USING BTREE, CONSTRAINT `Event_Subscription_ibfk_1` FOREIGN KEY (`Channel`) REFERENCES `chat_data`.`Channel` (`ID`) ON UPDATE CASCADE ON DELETE CASCADE, - CONSTRAINT `Event_Subscription_ibfk_2` FOREIGN KEY (`User_Alias`) REFERENCES `chat_data`.`User_Alias` (`ID`) ON UPDATE CASCADE ON DELETE CASCADE, - CONSTRAINT `Event_Subscription_ibfk_3` FOREIGN KEY (`Platform`) REFERENCES `chat_data`.`Platform` (`ID`) ON UPDATE CASCADE ON DELETE CASCADE + CONSTRAINT `Event_Subscription_ibfk_2` FOREIGN KEY (`User_Alias`) REFERENCES `chat_data`.`User_Alias` (`ID`) ON UPDATE CASCADE ON DELETE CASCADE ) CHARSET=utf8mb4 COLLATE='utf8mb4_general_ci'