From ef5f455d4de931fac65db857fd25ab07016aaeb0 Mon Sep 17 00:00:00 2001 From: benji7425 Date: Mon, 2 Oct 2017 00:41:55 +0100 Subject: [PATCH] Add upgrading of json data on startup --- app/index.js | 22 ++++++++++++++++++++++ app/models/feed-data.js | 6 +++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/app/index.js b/app/index.js index a451453..9c0d847 100644 --- a/app/index.js +++ b/app/index.js @@ -14,6 +14,8 @@ client.on("beforeLogin", () => { }); client.on("ready", () => { + doUpgradeJSON(); + parseLinksInGuilds(client.guilds, client.guildsData) .then(() => checkFeedsInGuilds(client.guildsData)); @@ -42,4 +44,24 @@ function parseLinksInGuilds(guilds, guildsData) { promises.push(guildData.cachePastPostedLinks(guilds.get(guildId))); } return Promise.all(promises); +} + +function doUpgradeJSON() { + Object.keys(client.guildsData).forEach(id => { + const guild = client.guilds.get(id); + if (!guild) + return; + + client.guildsData[id].feeds.forEach(feed => { + if (feed.roleName) { + feed.roleID = client.guilds.get(id).roles.find(x => x.name.toLowerCase() === feed.roleName.toLowerCase()).id; + delete feed.roleName; + } + + if (feed.channelName) { + feed.channelID = client.guilds.get(id).channels.find(x => x.name.toLowerCase() === feed.channelName.toLowerCase()).id; + delete feed.channelID; + } + }); + }); } \ No newline at end of file diff --git a/app/models/feed-data.js b/app/models/feed-data.js index 4663ee1..d0d44c1 100644 --- a/app/models/feed-data.js +++ b/app/models/feed-data.js @@ -9,7 +9,7 @@ const GetUrls = require("get-urls"); //for extracting urls from messages const ShortID = require("shortid"); //to provide ids for each feed, allowing guilds to remove them module.exports = class FeedData { - constructor({ id = null, url, channelID, roleID, cachedLinks = null, maxCacheSize }) { + constructor({ id = null, url, channelID, roleID, cachedLinks = null, maxCacheSize, roleName = undefined, channelName = undefined }) { this.id = id || ShortID.generate(); this.url = url; this.channelID = channelID; @@ -17,6 +17,10 @@ module.exports = class FeedData { this.cachedLinks = cachedLinks || []; this.maxCacheSize = maxCacheSize || 10; + //these two are actually deprecated, but need to be here for compatibility with old data files to be upgraded + this.roleName = roleName; + this.channelName = channelName; + this.cachedLinks.push = (...elements) => { Array.prototype.push.apply( this.cachedLinks,