From fbab7730b818357670849230d708d3910c2f8499 Mon Sep 17 00:00:00 2001 From: benji7425 Date: Sun, 13 Aug 2017 04:15:06 +0100 Subject: [PATCH] Fix a few more issues --- app/index.js | 24 +++++++++++++++--------- app/models/feed-data.js | 16 +++++++--------- app/models/guild-data.js | 6 +++--- 3 files changed, 25 insertions(+), 21 deletions(-) diff --git a/app/index.js b/app/index.js index 9f9a737..e8f9d7e 100644 --- a/app/index.js +++ b/app/index.js @@ -3,6 +3,7 @@ const FileSystem = require("fs"); //external lib imports const JsonFile = require("jsonfile"); +const Url = require("url"); //my imports const DiscordUtil = require("discordjs-util"); @@ -23,8 +24,8 @@ module.exports = (client) => { parseLinksInGuilds(client.guilds, guildsData).then(writeFile(guildsData)); //set up an interval to check all the feeds - checkFeedsInGuilds(guildsData); - setInterval(() => checkFeedsInGuilds(guildsData), config.feedCheckIntervalSec * 1000); + checkFeedsInGuilds(client.guilds, guildsData); + setInterval(() => checkFeedsInGuilds(client.guilds, guildsData), config.feedCheckIntervalSec * 1000); //set up an on message handler to detect when links are posted client.on("message", message => { @@ -63,12 +64,17 @@ 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]; + const channel = message.mentions.channels.first(); + if(!channel) + return message.reply("Please tag a channel with #channel-name"); - if (!feedUrl || !channelName) { - message.reply("Please supply all the needed fields in this format:\n add-feed url channel-name role-name"); - return; - } + const feedUrl = parameters[2], channelName = channel.name, roleName = parameters[4]; + + if (!Url.parse(feedUrl).host) + return message.reply("Please supply a valid url"); + + if (!feedUrl || !channelName) + return message.reply("Please supply all the needed fields in this format:\n add-feed url channel-name role-name"); const feedData = new FeedData({ url: feedUrl, @@ -94,8 +100,8 @@ function addFeed(client, guildsData, message) { }); } -function checkFeedsInGuilds(guildsData) { - Object.keys(guildsData).forEach(key => guildsData[key].checkFeeds()); +function checkFeedsInGuilds(guilds, guildsData) { + Object.keys(guildsData).forEach(key => guildsData[key].checkFeeds(guilds)); } function parseLinksInGuilds(guilds, guildsData) { diff --git a/app/models/feed-data.js b/app/models/feed-data.js index 0a72d18..3e3e954 100644 --- a/app/models/feed-data.js +++ b/app/models/feed-data.js @@ -11,7 +11,7 @@ module.exports = class FeedData { this.url = url; this.channelName = channelName; this.roleName = roleName; - this.cachedLinks = cachedLinks | []; + this.cachedLinks = cachedLinks || []; } /** @@ -33,7 +33,7 @@ module.exports = class FeedData { } check(guild) { - Dns.resolve(Url.parse(this.url).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 @@ -41,13 +41,16 @@ module.exports = class FeedData { if (err) DiscordUtil.dateError(err); else { - let latest = articles[0]; //extract the latest link + let latest = articles[0].link; //extract the latest link latest = normaliseUrl(latest); //standardise it a bit //if we don't have it cached already, cache it and callback if (!this.cachedLinks.includes(latest)) { this.cachedLinks.push(latest); - post(guild, latest); + + const channel = guild.channels.find(ch => ch.type === "text" && ch.name.toLowerCase() === this.channelName.toLowerCase()); + const role = guild.roles.find(role => role.name.toLowerCase() === this.roleName.toLowerCase()); + channel.send(role + " " + latest); } } }); @@ -55,11 +58,6 @@ module.exports = class FeedData { } }; -function post(guild, url){ - const channel = guild.channels.first(ch => ch.type === "text" && ch.name.toLower() === this.channelName.toLower()); - channel.send(url); -} - function normaliseUrl(url) { url = url.replace("https://", "http://"); //cheaty way to get around http and https not matching diff --git a/app/models/guild-data.js b/app/models/guild-data.js index 65936ba..94253d2 100644 --- a/app/models/guild-data.js +++ b/app/models/guild-data.js @@ -4,7 +4,7 @@ const Util = require("discordjs-util"); module.exports = class GuildData { constructor({ id, feeds }) { this.id = id; - this.feeds = feeds.filter(feed => new FeedData(feed)); + this.feeds = feeds.map(feed => new FeedData(feed)); } cachePastPostedLinks() { @@ -17,7 +17,7 @@ module.exports = class GuildData { return Promise.all(promises); } - checkFeeds() { - // this.feeds.forEach(feed => feed.check()); + checkFeeds(guilds) { + this.feeds.forEach(feed => feed.check(guilds.get(this.id))); } }; \ No newline at end of file