Add feed checking logic, set to run at 30 sec intervals
This commit is contained in:
parent
0d46ceddd9
commit
f02602955a
|
@ -1,5 +1,6 @@
|
||||||
{
|
{
|
||||||
"saveIntervalSec": 60,
|
"saveIntervalSec": 60,
|
||||||
|
"feedCheckIntervalSec": 30,
|
||||||
"commands": {
|
"commands": {
|
||||||
"version": "version",
|
"version": "version",
|
||||||
"addFeed": "add-feed"
|
"addFeed": "add-feed"
|
||||||
|
|
14
app/index.js
14
app/index.js
|
@ -20,8 +20,13 @@ module.exports = (client) => {
|
||||||
const guildsData = FileSystem.existsSync(SAVE_FILE) ? fromJSON(JsonFile.readFileSync(SAVE_FILE)) : {};
|
const guildsData = FileSystem.existsSync(SAVE_FILE) ? fromJSON(JsonFile.readFileSync(SAVE_FILE)) : {};
|
||||||
setInterval(() => writeFile(guildsData), config.saveIntervalSec * 1000);
|
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(guildsData);
|
||||||
|
setInterval(() => checkFeedsInGuilds(guildsData), config.feedCheckIntervalSec * 1000);
|
||||||
|
|
||||||
|
//set up an on message handler to detect when links are posted
|
||||||
client.on("message", message => {
|
client.on("message", message => {
|
||||||
if (message.author.id !== client.user.id) { //check the bot isn't triggering itself
|
if (message.author.id !== client.user.id) { //check the bot isn't triggering itself
|
||||||
if (message.channel.type === "dm")
|
if (message.channel.type === "dm")
|
||||||
|
@ -69,11 +74,14 @@ function addFeed(client, guildsData, message) {
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
responseMessage.reply("Your feed has not been saved, please add it again with the correct details");
|
responseMessage.reply("Your feed has not been saved, please add it again with the correct details");
|
||||||
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function parseLinksInAllGuilds(guilds, guildsData) {
|
function checkFeedsInGuilds(guildsData) {
|
||||||
|
guildsData.forEach(guild => guild.checkFeeds());
|
||||||
|
}
|
||||||
|
|
||||||
|
function parseLinksInGuilds(guilds, guildsData) {
|
||||||
const promises = [];
|
const promises = [];
|
||||||
for (let guild of guilds) {
|
for (let guild of guilds) {
|
||||||
const guildData = guildsData[guild.id];
|
const guildData = guildsData[guild.id];
|
||||||
|
|
|
@ -1,3 +1,11 @@
|
||||||
|
//my imports
|
||||||
|
const DiscordUtil = require("discordjs-util");
|
||||||
|
|
||||||
|
//external lib imports
|
||||||
|
const Dns = require("dns");
|
||||||
|
const Url = require("url");
|
||||||
|
const FeedRead = require("feed-read");
|
||||||
|
|
||||||
module.exports = class FeedData {
|
module.exports = class FeedData {
|
||||||
constructor({ link, channelName, roleName, cachedLinks }) {
|
constructor({ link, channelName, roleName, cachedLinks }) {
|
||||||
this.link = link;
|
this.link = link;
|
||||||
|
@ -23,8 +31,46 @@ module.exports = class FeedData {
|
||||||
.catch(reject);
|
.catch(reject);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
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
|
||||||
|
if (err)
|
||||||
|
DiscordUtil.dateError(err);
|
||||||
|
else {
|
||||||
|
let latest = articles[0]; //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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function post(guild, link){
|
||||||
|
const channel = guild.channels.first(ch => ch.type === "text" && ch.name.toLower() === this.channelName.toLower());
|
||||||
|
channel.send(link);
|
||||||
|
}
|
||||||
|
|
||||||
|
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) {
|
function getUrls(str) {
|
||||||
return str.match(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig);
|
return str.match(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig);
|
||||||
}
|
}
|
|
@ -16,4 +16,8 @@ module.exports = class GuildData {
|
||||||
|
|
||||||
return Promise.all(promises);
|
return Promise.all(promises);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
checkFeeds() {
|
||||||
|
this.feeds.forEach(feed => feed.check());
|
||||||
|
}
|
||||||
};
|
};
|
Loading…
Reference in a new issue