Skip to content

Commit

Permalink
$subscribe - add Brighter Shores (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
Supinic authored Nov 17, 2024
1 parent cb2a4ab commit 1067c1d
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 3 deletions.
63 changes: 63 additions & 0 deletions commands/subscribe/event-types/brighter-shores.js
Original file line number Diff line number Diff line change
@@ -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}`
};
}
};
1 change: 1 addition & 0 deletions commands/subscribe/event-types/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const eventTypeFileList = [
"brighter-shores",
"bun",
"changelog",
"channel-live",
Expand Down
5 changes: 2 additions & 3 deletions commands/subscribe/event_subscription.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit 1067c1d

Please sign in to comment.