Add upgrading of json data on startup

This commit is contained in:
benji7425 2017-10-02 00:41:55 +01:00
parent f1f3b4a8df
commit ef5f455d4d
2 changed files with 27 additions and 1 deletions

View File

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

View File

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