Browse Source

Merge branch 'feed-checking' into v2

shorten
benji7425 4 years ago
parent
commit
d5583b2c04
  1. 1
      app/config.json
  2. 77
      app/index.js
  3. 50
      app/models/feed-data.js
  4. 8
      app/models/guild-data.js
  5. 10
      package.json

1
app/config.json

@ -1,5 +1,6 @@
{
"saveIntervalSec": 60,
"feedCheckIntervalSec": 30,
"commands": {
"version": "version",
"addFeed": "add-feed"

77
app/index.js

@ -2,7 +2,9 @@
const FileSystem = require("fs");
//external lib imports
const JsonFile = require("jsonfile");
const JsonFile = require("jsonfile"); //for saving to/from JSON
const Url = require("url"); //for url parsing
const GetUrls = require("get-urls"); //for extracting urls from messages
//my imports
const DiscordUtil = require("discordjs-util");
@ -20,8 +22,13 @@ module.exports = (client) => {
const guildsData = FileSystem.existsSync(SAVE_FILE) ? fromJSON(JsonFile.readFileSync(SAVE_FILE)) : {};
setInterval(() => writeFile(guildsData), config.saveIntervalSec * 1000);
parseLinksInAllGuilds(client.guilds, guildsData).then(writeFile(guildsData));
parseLinksInGuilds(client.guilds, guildsData).then(writeFile(guildsData));
//set up an interval to check all the feeds
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 => {
if (message.author.id !== client.user.id) { //check the bot isn't triggering itself
if (message.channel.type === "dm")
@ -56,24 +63,45 @@ const HandleMessage = {
};
function addFeed(client, guildsData, message) {
const feedData = createNewFeed(message); //create a new feed data instance from the data in our message
const parameters = message.content.split(" "); //expect !addfeed <url> <channelName> <roleName>
const feedUrl = [...GetUrls(message.content)][0];
const channel = message.mentions.channels.first();
if (!feedUrl || !channel)
return message.reply("Please provide both a channel and an RSS feed URL. You can optionally @mention a role also.");
const role = message.mentions.roles.first();
const feedData = new FeedData({
url: feedUrl,
channelName: channel.name,
roleName: role.name
});
//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") {
saveFeed(guildsData, message.guild.id, feedData);
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
responseMessage.reply("Your feed has not been saved, please add it again with the correct details");
});
}
function parseLinksInAllGuilds(guilds, guildsData) {
function checkFeedsInGuilds(guilds, guildsData) {
Object.keys(guildsData).forEach(key => guildsData[key].checkFeeds(guilds));
}
function parseLinksInGuilds(guilds, guildsData) {
const promises = [];
for (let guild of guilds) {
const guildData = guildsData[guild.id];
@ -83,39 +111,12 @@ function parseLinksInAllGuilds(guilds, guildsData) {
return Promise.all(promises);
}
/**
* Create a new feed from the message object where the user is setting it up
* @param {Discord.Message} message
* @returns {FeedData} Newly created feed data object
*/
function createNewFeed(message) {
const parameters = message.content.split(" "); //expect !addfeed <url> <channelName> <roleName>
const feedData = new FeedData({
link: parameters[1],
channelName: parameters[2],
roleName: parameters[3]
});
return feedData;
}
/**
* Saves a passed feed data object into the passed guildsData object, for the specified guild
* @param {object} guildsData
* @param {string} guildID
* @param {FeedData} feedData
*/
function saveFeed(guildsData, guildID, feedData) {
if (!guildsData[guildID])
guildsData[guildID] = new GuildData({ id: guildID, feeds: [] });
guildsData[guildID].feeds.push(feedData);
}
function writeFile(guildsData) {
JsonFile.writeFile(SAVE_FILE, guildsData, err => { if (err) DiscordUtil.dateError("Error writing file", err); });
}
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;
}

50
app/models/feed-data.js

@ -1,9 +1,17 @@
//my imports
const DiscordUtil = require("discordjs-util");
//external lib imports
const Dns = require("dns"); //for host resolution checking
const Url = require("url"); //for url parsing
const FeedRead = require("feed-read"); //for extracing new links from RSS feeds
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 | [];
this.cachedLinks = cachedLinks || [];
}
/**
@ -23,8 +31,44 @@ module.exports = class FeedData {
.catch(reject);
});
}
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
if (err)
DiscordUtil.dateError("Connection Error: Can't resolve host", err); //log our error if we can't resolve the host
else
FeedRead(this.url, (err, articles) => { //check the feed
if (err)
DiscordUtil.dateError(err);
else {
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);
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);
}
}
});
});
}
};
function normaliseUrl(url) {
url = url.replace("https://", "http://"); //cheaty way to get around http and https not matching
if (Url.parse(url).host.includes("youtu")) //detect youtu.be and youtube.com - yes I know it's hacky
url = url.split("&")[0]; //quick way to chop off stuff like &feature=youtube
url = url.replace("http://www.youtube.com/watch?v=", "http://youtu.be/"); //turn full url into share url
return url;
}
function getUrls(str) {
return str.match(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig);
}

8
app/models/guild-data.js

@ -4,16 +4,20 @@ 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() {
const promises = [];
this.feeds.forEach(feed => {
promises.push(feed.cachePastPostedLinks(this).catch(Util.dateError));
});
return Promise.all(promises);
}
checkFeeds(guilds) {
this.feeds.forEach(feed => feed.check(guilds.get(this.id)));
}
};

10
package.json

@ -4,8 +4,8 @@
"description": "",
"main": "app/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node wrapper.js"
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node wrapper.js"
},
"repository": {
"type": "git",
@ -22,7 +22,7 @@
"discordjs-util": "git+https://github.com/benji7425/discordjs-util.git",
"dns": "0.2.2",
"feed-read": "0.0.1",
"jsonfile": "3.0.1",
"urijs": "1.18.10"
"get-urls": "7.0.0",
"jsonfile": "3.0.1"
}
}
}
Loading…
Cancel
Save