Browse Source

Add upgrading of json data on startup

master
benji7425 4 years ago
parent
commit
ef5f455d4d
  1. 22
      app/index.js
  2. 6
      app/models/feed-data.js

22
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;
}
});
});
}

6
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,

Loading…
Cancel
Save