diff --git a/app/index.js b/app/index.js index 6b0e71f..9f9a737 100644 --- a/app/index.js +++ b/app/index.js @@ -62,22 +62,31 @@ const HandleMessage = { function addFeed(client, guildsData, message) { const parameters = message.content.split(" "); //expect !addfeed + + const feedUrl = parameters[2], channelName = message.mentions.channels.first().name, roleName = parameters[4]; + + if (!feedUrl || !channelName) { + message.reply("Please supply all the needed fields in this format:\n add-feed url channel-name role-name"); + return; + } + const feedData = new FeedData({ - link: parameters[1], - channelName: parameters[2], - roleName: parameters[3] + url: feedUrl, + channelName: channelName, + roleName: roleName }); //ask the user if they're happy with the details they set up, save if yes, don't if no - DiscordUtil.ask(client, message.channel, message.member, "Are you happy with this? " + feedData) + DiscordUtil.ask(client, message.channel, message.member, "Are you happy with this?\n ```JavaScript\n" + JSON.stringify(feedData, null, "\n") + "```") .then(responseMessage => { //if they responded yes, save the feed and let them know, else tell them to start again - if (message.content.toLowerCase() === "yes") { + if (responseMessage.content.toLowerCase() === "yes") { if (!guildsData[message.guild.id]) guildsData[message.guild.id] = new GuildData({ id: message.guild.id, feeds: [] }); guildsData[message.guild.id].feeds.push(feedData); + writeFile(guildsData); responseMessage.reply("Your new feed has been saved!"); } else @@ -104,6 +113,7 @@ function writeFile(guildsData) { } function fromJSON(json) { - const guildIDs = Object.keys(json); - guildIDs.forEach(guildID => { guildIDs[guildID] = new GuildData(guildIDs[guildID]); }); + const guildsData = Object.keys(json); + guildsData.forEach(guildID => { json[guildID] = new GuildData(json[guildID]); }); + return json; } \ No newline at end of file diff --git a/app/models/feed-data.js b/app/models/feed-data.js index 7f47450..0a72d18 100644 --- a/app/models/feed-data.js +++ b/app/models/feed-data.js @@ -7,8 +7,8 @@ const Url = require("url"); const FeedRead = require("feed-read"); module.exports = class FeedData { - constructor({ link, channelName, roleName, cachedLinks }) { - this.link = link; + constructor({ url, channelName, roleName, cachedLinks }) { + this.url = url; this.channelName = channelName; this.roleName = roleName; this.cachedLinks = cachedLinks | []; @@ -33,11 +33,11 @@ module.exports = class FeedData { } check(guild) { - Dns.resolve(Url.parse(this.link).host, err => { //check we can resolve the host, so we can throw an appropriate error if it fails + Dns.resolve(Url.parse(this.url).host, err => { //check we can resolve the host, so we can throw an appropriate error if it fails if (err) DiscordUtil.dateError("Connection Error: Can't resolve host", err); //log our error if we can't resolve the host else - FeedRead(this.link, (err, articles) => { //check the feed + FeedRead(this.url, (err, articles) => { //check the feed if (err) DiscordUtil.dateError(err); else { @@ -55,9 +55,9 @@ module.exports = class FeedData { } }; -function post(guild, link){ +function post(guild, url){ const channel = guild.channels.first(ch => ch.type === "text" && ch.name.toLower() === this.channelName.toLower()); - channel.send(link); + channel.send(url); } function normaliseUrl(url) { diff --git a/app/models/guild-data.js b/app/models/guild-data.js index 1264809..65936ba 100644 --- a/app/models/guild-data.js +++ b/app/models/guild-data.js @@ -18,6 +18,6 @@ module.exports = class GuildData { } checkFeeds() { - this.feeds.forEach(feed => feed.check()); + // this.feeds.forEach(feed => feed.check()); } }; \ No newline at end of file