Browse Source

Fix a few more issues

shorten
benji7425 4 years ago
parent
commit
fbab7730b8
  1. 24
      app/index.js
  2. 16
      app/models/feed-data.js
  3. 6
      app/models/guild-data.js

24
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 <url> <channelName> <roleName>
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) {

16
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

6
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)));
}
};
Loading…
Cancel
Save