From 4b29ce9d307cb42f1b159970eb3ff68ba9011915 Mon Sep 17 00:00:00 2001 From: benji7425 Date: Tue, 11 Jul 2017 12:58:27 +0100 Subject: [PATCH 02/30] Add node settings git@github.com:benji7425/node-settings.git --- .eslintrc.json | 30 +++++++++++++++++++++++++++++ .gitignore | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++ .npmrc | 3 +++ 3 files changed, 85 insertions(+) create mode 100644 .eslintrc.json create mode 100644 .gitignore create mode 100644 .npmrc diff --git a/.eslintrc.json b/.eslintrc.json new file mode 100644 index 0000000..f67f1c2 --- /dev/null +++ b/.eslintrc.json @@ -0,0 +1,30 @@ +{ + "env": { + "browser": true, + "node": true, + "commonjs": true, + "es6": true + }, + "extends": "eslint:recommended", + "parserOptions": { + "sourceType": "module" + }, + "rules": { + "indent": [ + "warn", + "tab", + { "SwitchCase": 1} + ], + "quotes": [ + "warn", + "double" + ], + "semi": [ + "error", + "always" + ], + "no-undef": "error", + "no-unused-vars": "warn", + "eqeqeq": ["error", "always"] + } +} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..61b461d --- /dev/null +++ b/.gitignore @@ -0,0 +1,52 @@ +### Node ### +# Logs +logs +*.log +npm-debug.log* + +# Runtime data +pids +*.pid +*.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage + +# nyc test coverage +.nyc_output + +# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (http://nodejs.org/api/addons.html) +build/Release + +# Dependency directories +node_modules +node_cache +jspm_packages + +# Optional npm cache directory +.npm + +# Optional eslint cache +.eslintcache + +# Optional REPL history +.node_repl_history + +# Output of 'npm pack' +*.tgz + +# Yarn Integrity file +.yarn-integrity + + +# End of https://www.gitignore.io/api/node \ No newline at end of file diff --git a/.npmrc b/.npmrc new file mode 100644 index 0000000..799e6bc --- /dev/null +++ b/.npmrc @@ -0,0 +1,3 @@ +save=true +save-exact=true +cache=node_cache From ef1d1d0042de563000a1342875019042261296c6 Mon Sep 17 00:00:00 2001 From: benji7425 Date: Tue, 11 Jul 2017 13:06:13 +0100 Subject: [PATCH 03/30] Initialise node --- package.json | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 package.json diff --git a/package.json b/package.json new file mode 100644 index 0000000..5ccb99d --- /dev/null +++ b/package.json @@ -0,0 +1,19 @@ +{ + "name": "discord-bot-feed-linker", + "version": "2.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/benji7425/discord-bot-feed-linker.git" + }, + "author": "", + "license": "ISC", + "bugs": { + "url": "https://github.com/benji7425/discord-bot-feed-linker/issues" + }, + "homepage": "https://github.com/benji7425/discord-bot-feed-linker#readme" +} From ef0d26bf73c89d6e98505b006154b9b4d7f2e3e5 Mon Sep 17 00:00:00 2001 From: benji7425 Date: Thu, 13 Jul 2017 21:03:24 +0100 Subject: [PATCH 04/30] Install discord.js --- package.json | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 5ccb99d..e29641b 100644 --- a/package.json +++ b/package.json @@ -15,5 +15,8 @@ "bugs": { "url": "https://github.com/benji7425/discord-bot-feed-linker/issues" }, - "homepage": "https://github.com/benji7425/discord-bot-feed-linker#readme" + "homepage": "https://github.com/benji7425/discord-bot-feed-linker#readme", + "dependencies": { + "discord.js": "11.1.0" + } } From 34ab0a0a1833fa350ac013b995c15da0599f9dcc Mon Sep 17 00:00:00 2001 From: benji7425 Date: Tue, 18 Jul 2017 01:16:12 +0100 Subject: [PATCH 05/30] Install required npm packages Just grab all the packages used from v1 --- package.json | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index e29641b..b384e15 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,9 @@ }, "homepage": "https://github.com/benji7425/discord-bot-feed-linker#readme", "dependencies": { - "discord.js": "11.1.0" + "discord.js": "11.1.0", + "dns": "0.2.2", + "feed-read": "0.0.1", + "urijs": "1.18.10" } } From f74313519795e204c9c266fd2d10fe1a76601d53 Mon Sep 17 00:00:00 2001 From: benji7425 Date: Wed, 26 Jul 2017 22:29:05 +0100 Subject: [PATCH 06/30] Basic setup --- .gitignore | 6 ++++++ .vscode/launch.json | 14 ++++++++++++++ app/index.js | 9 +++++++++ package.json | 1 + wrapper.js | 18 ++++++++++++++++++ 5 files changed, 48 insertions(+) create mode 100644 .vscode/launch.json create mode 100644 app/index.js create mode 100644 wrapper.js diff --git a/.gitignore b/.gitignore index 61b461d..f772ff4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,9 @@ +# Project specific + +token.json +guilds.json +log + ### Node ### # Logs logs diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..fa3533d --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,14 @@ +{ + // Use IntelliSense to learn about possible Node.js debug attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "node", + "request": "launch", + "name": "Launch Program", + "program": "${workspaceRoot}/wrapper.js" + } + ] +} \ No newline at end of file diff --git a/app/index.js b/app/index.js new file mode 100644 index 0000000..d830bc3 --- /dev/null +++ b/app/index.js @@ -0,0 +1,9 @@ +//acts as on ready function +module.exports = (client) => { + client.on("message", message => message.reply("Hello!")); + //check messages in channel for links posted since last online + + //set up an interval to check all the feeds + + //set up an on message handler to detect when links are posted +}; \ No newline at end of file diff --git a/package.json b/package.json index b384e15..795442e 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "homepage": "https://github.com/benji7425/discord-bot-feed-linker#readme", "dependencies": { "discord.js": "11.1.0", + "discordjs-util": "git+https://github.com/benji7425/discordjs-util.git", "dns": "0.2.2", "feed-read": "0.0.1", "urijs": "1.18.10" diff --git a/wrapper.js b/wrapper.js new file mode 100644 index 0000000..9a176e3 --- /dev/null +++ b/wrapper.js @@ -0,0 +1,18 @@ +const Discord = require("discord.js"); +const Util = require("discordjs-util"); + +const client = new Discord.Client(); + +process.on("uncaughtException", (err) => { + Util.dateError(err); +}); + +client.login(require("./token.json").token); + +client.on("ready", () => { + require("./app/index.js")(client); +}); + +client.on("disconnect", eventData => { + Util.dateError(eventData.code, eventData.reason); +}); \ No newline at end of file From 372e6f3d740516b33a44d692b51a20f0d9df0e92 Mon Sep 17 00:00:00 2001 From: benji7425 Date: Wed, 26 Jul 2017 23:53:01 +0100 Subject: [PATCH 07/30] Add data models --- app/models/feed.js | 7 +++++++ app/models/guild-data.js | 6 ++++++ 2 files changed, 13 insertions(+) create mode 100644 app/models/feed.js create mode 100644 app/models/guild-data.js diff --git a/app/models/feed.js b/app/models/feed.js new file mode 100644 index 0000000..f9281a1 --- /dev/null +++ b/app/models/feed.js @@ -0,0 +1,7 @@ +module.exports = class Feed{ + constructor({link, channelName, roleID}){ + this.link = link; + this.channelName = channelName; + this.roleID = roleID; + } +}; \ No newline at end of file diff --git a/app/models/guild-data.js b/app/models/guild-data.js new file mode 100644 index 0000000..89042ac --- /dev/null +++ b/app/models/guild-data.js @@ -0,0 +1,6 @@ +module.exports = class GuildData { + constructor({id, feeds}) { + this.id = id; + this.feeds = feeds + } +}; \ No newline at end of file From 6a4f2a32d81b6f91ee6c377a0c468a59b79d36f5 Mon Sep 17 00:00:00 2001 From: benji7425 Date: Thu, 27 Jul 2017 07:50:44 +0100 Subject: [PATCH 08/30] Add loading data from json file --- app/index.js | 23 +++++++++++++++++++---- app/models/guild-data.js | 4 +++- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/app/index.js b/app/index.js index d830bc3..00dcad7 100644 --- a/app/index.js +++ b/app/index.js @@ -1,9 +1,24 @@ +//node imports +const FileSystem = require("fs"); + +//external lib imports +const JSONFile = require("jsonfile"); + +//app component imports +const GuildData = require("./models/guild-data.js"); + +const SAVE_FILE = "./guilds.json"; + //acts as on ready function module.exports = (client) => { - client.on("message", message => message.reply("Hello!")); - //check messages in channel for links posted since last online - + const guildsData = FileSystem.existsSync(SAVE_FILE) ? parseJSON(JSONFile.readFileSync(SAVE_FILE)) : {}; //pull saved data from file + //set up an interval to check all the feeds //set up an on message handler to detect when links are posted -}; \ No newline at end of file +}; + +function parseJSON(json) { + const guildIDs = Object.keys(json); + guildIDs.forEach(guildID => { guildIDs[guildID] = new GuildData(guildIDs[guildID]); }); +} \ No newline at end of file diff --git a/app/models/guild-data.js b/app/models/guild-data.js index 89042ac..6ee120c 100644 --- a/app/models/guild-data.js +++ b/app/models/guild-data.js @@ -1,6 +1,8 @@ +const Feed = require("./feed.js"); + module.exports = class GuildData { constructor({id, feeds}) { this.id = id; - this.feeds = feeds + this.feeds = feeds.filter(feed => new Feed(feed)); } }; \ No newline at end of file From b8d6fe8d3ca04e668f7b54ffc706f48400ebe2ce Mon Sep 17 00:00:00 2001 From: benji7425 Date: Sat, 29 Jul 2017 20:28:00 +0100 Subject: [PATCH 09/30] Install jsonfile (it was missing) --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 795442e..1223f18 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,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" } } From 084e805f394b87b5a2871a412179b2d28ab952bf Mon Sep 17 00:00:00 2001 From: benji7425 Date: Sat, 29 Jul 2017 20:31:55 +0100 Subject: [PATCH 10/30] Add feed class methods for retrieving links in past messages Also added guild property to the model, for identification --- app/models/feed-data.js | 30 ++++++++++++++++++++++++++++++ app/models/feed.js | 7 ------- 2 files changed, 30 insertions(+), 7 deletions(-) create mode 100644 app/models/feed-data.js delete mode 100644 app/models/feed.js diff --git a/app/models/feed-data.js b/app/models/feed-data.js new file mode 100644 index 0000000..6179f07 --- /dev/null +++ b/app/models/feed-data.js @@ -0,0 +1,30 @@ +module.exports = class FeedData { + constructor({ link, channelName, roleID, cachedLinks }) { + this.link = link; + this.channelName = channelName; + this.roleID = roleID; + this.cachedLinks = cachedLinks; + } + + /** + * Returns a promise providing all the links posted in the last 100 messages + * @param {Discord.Guild} guild The guild this feed belongs to + * @returns {Promise} Links posted in last 100 messages + */ + updatePastPostedLinks(guild) { + const channel = guild.channels.find(ch => ch.type === "text" && ch.name === this.channelName); + + return new Promise((resolve, reject) => { + channel.fetchMessages({ limit: 100 }) + .then(messages => { + messages.forEach(m => Array.prototype.push.apply(this.cachedLinks, getUrls(m))); //push all the links in each message into our links array + resolve(this); + }) + .catch(reject); + }); + } +}; + +function getUrls(str) { + return str.match(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig); +} \ No newline at end of file diff --git a/app/models/feed.js b/app/models/feed.js deleted file mode 100644 index f9281a1..0000000 --- a/app/models/feed.js +++ /dev/null @@ -1,7 +0,0 @@ -module.exports = class Feed{ - constructor({link, channelName, roleID}){ - this.link = link; - this.channelName = channelName; - this.roleID = roleID; - } -}; \ No newline at end of file From 36aa5639abe9d8c887b2f272c4ed58d71e1aae6f Mon Sep 17 00:00:00 2001 From: benji7425 Date: Sat, 29 Jul 2017 20:32:33 +0100 Subject: [PATCH 11/30] Add method for recursively iterating over feeds in guild --- app/models/guild-data.js | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/app/models/guild-data.js b/app/models/guild-data.js index 6ee120c..53941dd 100644 --- a/app/models/guild-data.js +++ b/app/models/guild-data.js @@ -1,8 +1,21 @@ -const Feed = require("./feed.js"); +const FeedData = require("./feed-data.js"); +const Util = require("discordjs-util"); module.exports = class GuildData { - constructor({id, feeds}) { + constructor({ id, feeds }) { this.id = id; - this.feeds = feeds.filter(feed => new Feed(feed)); + this.feeds = feeds.filter(feed => new FeedData(feed)); + } + + cachePastPostedLinks() { + let i = 0; + const recurse = () => { + this.feeds[i++].cachePastPostedLinks(this) + .catch(Util.dateError) + .then(recurse); + if (i > this.feeds.length) + return Promise.resolve(); + }; + } }; \ No newline at end of file From 02b400266daa98dfb965d29ea5c8ad722385fcab Mon Sep 17 00:00:00 2001 From: benji7425 Date: Sat, 29 Jul 2017 20:33:05 +0100 Subject: [PATCH 12/30] Add method to iterate over all guild to update their feed caches --- app/index.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/app/index.js b/app/index.js index 00dcad7..90894e9 100644 --- a/app/index.js +++ b/app/index.js @@ -2,6 +2,7 @@ const FileSystem = require("fs"); //external lib imports +const Dicsord = require("discord.js"); const JSONFile = require("jsonfile"); //app component imports @@ -13,11 +14,21 @@ const SAVE_FILE = "./guilds.json"; module.exports = (client) => { const guildsData = FileSystem.existsSync(SAVE_FILE) ? parseJSON(JSONFile.readFileSync(SAVE_FILE)) : {}; //pull saved data from file + parseLinksInAllGuilds(client.guilds, guildsData); + //set up an interval to check all the feeds //set up an on message handler to detect when links are posted }; +function parseLinksInAllGuilds(guilds, guildsData) { + for (let guild of guilds) { + const guildData = guildsData[guild.id]; + if (guildData) + guildData.cachePastPostedLinks(); + } +} + function parseJSON(json) { const guildIDs = Object.keys(json); guildIDs.forEach(guildID => { guildIDs[guildID] = new GuildData(guildIDs[guildID]); }); From c82bece834ae0dbebfdc974a57f52c93f056b713 Mon Sep 17 00:00:00 2001 From: benji7425 Date: Sat, 29 Jul 2017 20:36:41 +0100 Subject: [PATCH 13/30] Add writing back to data file after all guilds update their caches --- app/index.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/app/index.js b/app/index.js index 90894e9..c9633e1 100644 --- a/app/index.js +++ b/app/index.js @@ -5,6 +5,9 @@ const FileSystem = require("fs"); const Dicsord = require("discord.js"); const JSONFile = require("jsonfile"); +//my imports +const Util = require("discordjs-util"); + //app component imports const GuildData = require("./models/guild-data.js"); @@ -14,7 +17,7 @@ const SAVE_FILE = "./guilds.json"; module.exports = (client) => { const guildsData = FileSystem.existsSync(SAVE_FILE) ? parseJSON(JSONFile.readFileSync(SAVE_FILE)) : {}; //pull saved data from file - parseLinksInAllGuilds(client.guilds, guildsData); + parseLinksInAllGuilds(client.guilds, guildsData).then(writeFile); //set up an interval to check all the feeds @@ -22,14 +25,20 @@ module.exports = (client) => { }; function parseLinksInAllGuilds(guilds, guildsData) { + const promises = []; for (let guild of guilds) { const guildData = guildsData[guild.id]; if (guildData) - guildData.cachePastPostedLinks(); + promises.push(guildData.cachePastPostedLinks()); } + return Promise.all(promises); } function parseJSON(json) { const guildIDs = Object.keys(json); guildIDs.forEach(guildID => { guildIDs[guildID] = new GuildData(guildIDs[guildID]); }); +} + +function writeFile(guildsData){ + JSONFile.write(SAVE_FILE, guildsData, err => {if(err) Util.dateError(err); }); } \ No newline at end of file From 5e5929565ebba1a35cf7cf993d9996da6fe7038f Mon Sep 17 00:00:00 2001 From: benji7425 Date: Sat, 29 Jul 2017 20:40:55 +0100 Subject: [PATCH 14/30] Fix mistake whereby the caches for each guild would update synchronously --- app/models/guild-data.js | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/app/models/guild-data.js b/app/models/guild-data.js index 53941dd..82de0f9 100644 --- a/app/models/guild-data.js +++ b/app/models/guild-data.js @@ -8,14 +8,12 @@ module.exports = class GuildData { } cachePastPostedLinks() { - let i = 0; - const recurse = () => { - this.feeds[i++].cachePastPostedLinks(this) - .catch(Util.dateError) - .then(recurse); - if (i > this.feeds.length) - return Promise.resolve(); - }; + const promises = []; + + this.feeds.forEach(feed => { + promises.push(feed.cachePastPostedLinks(this).catch(Util.dateError)); + }); + return Promise.all(promises); } }; \ No newline at end of file From cda70c73332be6271c88190649052b3d1006f99e Mon Sep 17 00:00:00 2001 From: benji7425 Date: Sun, 30 Jul 2017 20:24:37 +0100 Subject: [PATCH 15/30] Add method for adding feed via in-chat command --- app/config.json | 5 ++++ app/index.js | 60 ++++++++++++++++++++++++++++++++++++++--- app/models/feed-data.js | 6 ++--- 3 files changed, 64 insertions(+), 7 deletions(-) create mode 100644 app/config.json diff --git a/app/config.json b/app/config.json new file mode 100644 index 0000000..9fc31e9 --- /dev/null +++ b/app/config.json @@ -0,0 +1,5 @@ +{ + "commands": { + "addFeed": "!addfeed" + } +} \ No newline at end of file diff --git a/app/index.js b/app/index.js index c9633e1..a3d6606 100644 --- a/app/index.js +++ b/app/index.js @@ -2,7 +2,6 @@ const FileSystem = require("fs"); //external lib imports -const Dicsord = require("discord.js"); const JSONFile = require("jsonfile"); //my imports @@ -10,18 +9,43 @@ const Util = require("discordjs-util"); //app component imports const GuildData = require("./models/guild-data.js"); +const FeedData = require("./models/feed-data.js"); const SAVE_FILE = "./guilds.json"; //acts as on ready function module.exports = (client) => { + const config = require("./config.json"); const guildsData = FileSystem.existsSync(SAVE_FILE) ? parseJSON(JSONFile.readFileSync(SAVE_FILE)) : {}; //pull saved data from file - parseLinksInAllGuilds(client.guilds, guildsData).then(writeFile); + parseLinksInAllGuilds(client.guilds, guildsData).then(writeFile(guildsData)); //set up an interval to check all the feeds //set up an on message handler to detect when links are posted + client.on("message", message => { + if (message.member.id !== client.user.id) { //make sure the bot ignores itself + + //check if the user is admin and is invoking the add feed command + if (message.member.permissions.has("ADMINISTRATOR") && message.content.startsWith(config.commands.setup)) { + const feedData = createNewFeed(message); //create a new feed data instance from the data in our message + + //ask the user if they're happy with the details they set up, save if yes, don't if no + Util.ask(client, message.channel, message.member, "Are you happy with this? " + feedData) + .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); + 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) { @@ -34,11 +58,39 @@ 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 + 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 parseJSON(json) { const guildIDs = Object.keys(json); guildIDs.forEach(guildID => { guildIDs[guildID] = new GuildData(guildIDs[guildID]); }); } -function writeFile(guildsData){ - JSONFile.write(SAVE_FILE, guildsData, err => {if(err) Util.dateError(err); }); +function writeFile(guildsData) { + JSONFile.write(SAVE_FILE, guildsData, err => { if (err) Util.dateError(err); }); } \ No newline at end of file diff --git a/app/models/feed-data.js b/app/models/feed-data.js index 6179f07..f4aae1c 100644 --- a/app/models/feed-data.js +++ b/app/models/feed-data.js @@ -1,9 +1,9 @@ module.exports = class FeedData { - constructor({ link, channelName, roleID, cachedLinks }) { + constructor({ link, channelName, roleName, cachedLinks }) { this.link = link; this.channelName = channelName; - this.roleID = roleID; - this.cachedLinks = cachedLinks; + this.roleName = roleName; + this.cachedLinks = cachedLinks | []; } /** From d5198fc5fc64ccb372502a4cb0f9dda9ba20b519 Mon Sep 17 00:00:00 2001 From: benji7425 Date: Fri, 11 Aug 2017 20:48:11 +0100 Subject: [PATCH 16/30] Update wrapper.js --- wrapper.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/wrapper.js b/wrapper.js index 9a176e3..4efa939 100644 --- a/wrapper.js +++ b/wrapper.js @@ -1,18 +1,20 @@ const Discord = require("discord.js"); -const Util = require("discordjs-util"); +const DiscordUtil = require("discordjs-util"); const client = new Discord.Client(); process.on("uncaughtException", (err) => { - Util.dateError(err); + DiscordUtil.dateError("Uncaught exception!", err); }); client.login(require("./token.json").token); client.on("ready", () => { + DiscordUtil.dateLog("Registered bot " + client.user.username); require("./app/index.js")(client); + client.user.setGame("benji7425.github.io"); }); client.on("disconnect", eventData => { - Util.dateError(eventData.code, eventData.reason); + DiscordUtil.dateError("Bot was disconnected!", eventData.code, eventData.reason); }); \ No newline at end of file From f56c8454fc28f0f64c0f1685c82ed0d2cd4f89cb Mon Sep 17 00:00:00 2001 From: benji7425 Date: Sat, 12 Aug 2017 01:42:17 +0100 Subject: [PATCH 17/30] Temporarily rename files --- app/{config.json => config.old.json} | 0 app/{index.js => index.old.js} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename app/{config.json => config.old.json} (100%) rename app/{index.js => index.old.js} (100%) diff --git a/app/config.json b/app/config.old.json similarity index 100% rename from app/config.json rename to app/config.old.json diff --git a/app/index.js b/app/index.old.js similarity index 100% rename from app/index.js rename to app/index.old.js From 5428e71f01691e6b11d3c36e43deaf79cfbc7336 Mon Sep 17 00:00:00 2001 From: benji7425 Date: Sat, 12 Aug 2017 01:44:35 +0100 Subject: [PATCH 18/30] Merge in template --- .gitignore | 5 ++--- CHANGELOG.md | 3 +++ app/config.json | 6 ++++++ app/index.js | 55 +++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 7 ++++--- 5 files changed, 70 insertions(+), 6 deletions(-) create mode 100644 CHANGELOG.md create mode 100644 app/config.json create mode 100644 app/index.js diff --git a/.gitignore b/.gitignore index f772ff4..7836b18 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,6 @@ -# Project specific - -token.json +### Discord bots #### guilds.json +token.json log ### Node ### diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..8deb76e --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,3 @@ +# Changelog + +## Unreleased \ No newline at end of file diff --git a/app/config.json b/app/config.json new file mode 100644 index 0000000..ddda549 --- /dev/null +++ b/app/config.json @@ -0,0 +1,6 @@ +{ + "saveIntervalSec": 60, + "commands": { + "version": "version" + } +} \ No newline at end of file diff --git a/app/index.js b/app/index.js new file mode 100644 index 0000000..11ba6b5 --- /dev/null +++ b/app/index.js @@ -0,0 +1,55 @@ +//node imports +const FileSystem = require("fs"); + +//external lib imports +const JsonFile = require("jsonfile"); + +//my imports +const DiscordUtil = require("discordjs-util"); + +//global vars +const SAVE_FILE = "./guilds.json"; + +module.exports = (client) => { + const config = require("./config.json"); + + const guildsData = FileSystem.existsSync(SAVE_FILE) ? fromJSON(JsonFile.readFileSync(SAVE_FILE)) : {}; + setInterval(() => writeFile(guildsData), config.saveIntervalSec * 1000); + + client.on("message", message => { + if (message.author.id !== client.user.id) { //check the bot isn't triggering itself + if (message.channel.type === "dm") + HandleMessage.DM(client, config, message); + else if (message.channel.type === "text" && message.member) + HandleMessage.Text(client, config, message, guildsData); + } + }); +}; + +const HandleMessage = { + DM: (client, config, message) => { + message.reply("This bot does not have any handling for direct messages. To learn more or get help please visit http://benji7425.github.io, or join my Discord server here: https://discord.gg/SSkbwSJ"); + }, + Text: (client, config, message, guildsData) => { + //handle admins invoking commands + if (message.content.startsWith(message.guild.me.toString()) //user is @mention-ing the bot + && message.member.permissions.has("ADMINISTRATOR")) //user has admin perms + { + const params = message.content.split(" "); //split the message at the spaces + switch (params[1]) { + //add handling for different commands here + case config.commands.version: + message.reply("v" + require("../package.json").version); + break; + } + } + } +}; + +function writeFile(guildsData) { + JsonFile.writeFile(SAVE_FILE, guildsData, err => { if (err) DiscordUtil.dateError("Error writing file", err); }); +} + +function fromJSON(json) { + throw "Not implemented"; +} \ No newline at end of file diff --git a/package.json b/package.json index 1223f18..b918016 100644 --- a/package.json +++ b/package.json @@ -2,9 +2,10 @@ "name": "discord-bot-feed-linker", "version": "2.0.0", "description": "", - "main": "index.js", + "main": "app/index.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "test": "echo \"Error: no test specified\" && exit 1", + "start": "node wrapper.js" }, "repository": { "type": "git", @@ -24,4 +25,4 @@ "jsonfile": "3.0.1", "urijs": "1.18.10" } -} +} \ No newline at end of file From 6b9bf2f27f091f968b35c209802a32b8f70fa60c Mon Sep 17 00:00:00 2001 From: benji7425 Date: Sat, 12 Aug 2017 01:49:35 +0100 Subject: [PATCH 19/30] Update new index.js and config.json with application code --- app/config.json | 3 +- app/config.old.json | 5 --- app/index.js | 68 +++++++++++++++++++++++++++++++- app/index.old.js | 96 --------------------------------------------- 4 files changed, 69 insertions(+), 103 deletions(-) delete mode 100644 app/config.old.json delete mode 100644 app/index.old.js diff --git a/app/config.json b/app/config.json index ddda549..26971c6 100644 --- a/app/config.json +++ b/app/config.json @@ -1,6 +1,7 @@ { "saveIntervalSec": 60, "commands": { - "version": "version" + "version": "version", + "addFeed": "add-feed" } } \ No newline at end of file diff --git a/app/config.old.json b/app/config.old.json deleted file mode 100644 index 9fc31e9..0000000 --- a/app/config.old.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "commands": { - "addFeed": "!addfeed" - } -} \ No newline at end of file diff --git a/app/index.js b/app/index.js index 11ba6b5..68168c4 100644 --- a/app/index.js +++ b/app/index.js @@ -7,6 +7,10 @@ const JsonFile = require("jsonfile"); //my imports const DiscordUtil = require("discordjs-util"); +//app component imports +const GuildData = require("./models/guild-data.js"); +const FeedData = require("./models/feed-data.js"); + //global vars const SAVE_FILE = "./guilds.json"; @@ -16,6 +20,8 @@ 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)); + client.on("message", message => { if (message.author.id !== client.user.id) { //check the bot isn't triggering itself if (message.channel.type === "dm") @@ -41,15 +47,75 @@ const HandleMessage = { case config.commands.version: message.reply("v" + require("../package.json").version); break; + case config.commands.addFeed: + addFeed(client, guildsData, message); + break; } } } }; +function addFeed(client, guildsData, message) { + const feedData = createNewFeed(message); //create a new feed data instance from the data in our message + + //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) + .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); + 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) { + const promises = []; + for (let guild of guilds) { + const guildData = guildsData[guild.id]; + if (guildData) + promises.push(guildData.cachePastPostedLinks()); + } + 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 + 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) { - throw "Not implemented"; + const guildIDs = Object.keys(json); + guildIDs.forEach(guildID => { guildIDs[guildID] = new GuildData(guildIDs[guildID]); }); } \ No newline at end of file diff --git a/app/index.old.js b/app/index.old.js deleted file mode 100644 index a3d6606..0000000 --- a/app/index.old.js +++ /dev/null @@ -1,96 +0,0 @@ -//node imports -const FileSystem = require("fs"); - -//external lib imports -const JSONFile = require("jsonfile"); - -//my imports -const Util = require("discordjs-util"); - -//app component imports -const GuildData = require("./models/guild-data.js"); -const FeedData = require("./models/feed-data.js"); - -const SAVE_FILE = "./guilds.json"; - -//acts as on ready function -module.exports = (client) => { - const config = require("./config.json"); - const guildsData = FileSystem.existsSync(SAVE_FILE) ? parseJSON(JSONFile.readFileSync(SAVE_FILE)) : {}; //pull saved data from file - - parseLinksInAllGuilds(client.guilds, guildsData).then(writeFile(guildsData)); - - //set up an interval to check all the feeds - - //set up an on message handler to detect when links are posted - client.on("message", message => { - if (message.member.id !== client.user.id) { //make sure the bot ignores itself - - //check if the user is admin and is invoking the add feed command - if (message.member.permissions.has("ADMINISTRATOR") && message.content.startsWith(config.commands.setup)) { - const feedData = createNewFeed(message); //create a new feed data instance from the data in our message - - //ask the user if they're happy with the details they set up, save if yes, don't if no - Util.ask(client, message.channel, message.member, "Are you happy with this? " + feedData) - .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); - 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) { - const promises = []; - for (let guild of guilds) { - const guildData = guildsData[guild.id]; - if (guildData) - promises.push(guildData.cachePastPostedLinks()); - } - 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 - 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 parseJSON(json) { - const guildIDs = Object.keys(json); - guildIDs.forEach(guildID => { guildIDs[guildID] = new GuildData(guildIDs[guildID]); }); -} - -function writeFile(guildsData) { - JSONFile.write(SAVE_FILE, guildsData, err => { if (err) Util.dateError(err); }); -} \ No newline at end of file From f02602955a6bc15f34a4fec9f0221842ef0f4b02 Mon Sep 17 00:00:00 2001 From: benji7425 Date: Sat, 12 Aug 2017 01:55:58 +0100 Subject: [PATCH 20/30] Add feed checking logic, set to run at 30 sec intervals --- app/config.json | 1 + app/index.js | 14 +++++++++--- app/models/feed-data.js | 46 ++++++++++++++++++++++++++++++++++++++++ app/models/guild-data.js | 6 +++++- 4 files changed, 63 insertions(+), 4 deletions(-) diff --git a/app/config.json b/app/config.json index 26971c6..ff568bc 100644 --- a/app/config.json +++ b/app/config.json @@ -1,5 +1,6 @@ { "saveIntervalSec": 60, + "feedCheckIntervalSec": 30, "commands": { "version": "version", "addFeed": "add-feed" diff --git a/app/index.js b/app/index.js index 68168c4..b02d198 100644 --- a/app/index.js +++ b/app/index.js @@ -20,8 +20,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(guildsData); + setInterval(() => checkFeedsInGuilds(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") @@ -69,11 +74,14 @@ function addFeed(client, guildsData, message) { } else 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 = []; for (let guild of guilds) { const guildData = guildsData[guild.id]; diff --git a/app/models/feed-data.js b/app/models/feed-data.js index f4aae1c..7f47450 100644 --- a/app/models/feed-data.js +++ b/app/models/feed-data.js @@ -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 { constructor({ link, channelName, roleName, cachedLinks }) { this.link = link; @@ -23,8 +31,46 @@ module.exports = class FeedData { .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) { return str.match(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig); } \ No newline at end of file diff --git a/app/models/guild-data.js b/app/models/guild-data.js index 82de0f9..1264809 100644 --- a/app/models/guild-data.js +++ b/app/models/guild-data.js @@ -9,11 +9,15 @@ module.exports = class GuildData { cachePastPostedLinks() { const promises = []; - + this.feeds.forEach(feed => { promises.push(feed.cachePastPostedLinks(this).catch(Util.dateError)); }); return Promise.all(promises); } + + checkFeeds() { + this.feeds.forEach(feed => feed.check()); + } }; \ No newline at end of file From 385f0f86561a1201623549349eda1cf43a5d4f2c Mon Sep 17 00:00:00 2001 From: benji7425 Date: Sat, 12 Aug 2017 02:05:59 +0100 Subject: [PATCH 21/30] Update formatting --- app/index.js | 42 +++++++++++------------------------------- 1 file changed, 11 insertions(+), 31 deletions(-) diff --git a/app/index.js b/app/index.js index b02d198..6b0e71f 100644 --- a/app/index.js +++ b/app/index.js @@ -61,7 +61,12 @@ 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 + const feedData = new FeedData({ + link: parameters[1], + channelName: parameters[2], + roleName: parameters[3] + }); //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) @@ -69,7 +74,10 @@ function addFeed(client, guildsData, message) { //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 (!guildsData[message.guild.id]) + guildsData[message.guild.id] = new GuildData({ id: message.guild.id, feeds: [] }); + + guildsData[message.guild.id].feeds.push(feedData); responseMessage.reply("Your new feed has been saved!"); } else @@ -78,7 +86,7 @@ function addFeed(client, guildsData, message) { } function checkFeedsInGuilds(guildsData) { - guildsData.forEach(guild => guild.checkFeeds()); + Object.keys(guildsData).forEach(key => guildsData[key].checkFeeds()); } function parseLinksInGuilds(guilds, guildsData) { @@ -91,34 +99,6 @@ function parseLinksInGuilds(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 - 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); }); } From ac9da5f8797062ce94f161c402dea351c3fa0b27 Mon Sep 17 00:00:00 2001 From: benji7425 Date: Sun, 13 Aug 2017 03:12:20 +0100 Subject: [PATCH 22/30] Fix issues with feed addition --- app/index.js | 24 +++++++++++++++++------- app/models/feed-data.js | 12 ++++++------ app/models/guild-data.js | 2 +- 3 files changed, 24 insertions(+), 14 deletions(-) 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 From fbab7730b818357670849230d708d3910c2f8499 Mon Sep 17 00:00:00 2001 From: benji7425 Date: Sun, 13 Aug 2017 04:15:06 +0100 Subject: [PATCH 23/30] 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 From 1539926671b143300a69f871e7c29fd4f31cd33a Mon Sep 17 00:00:00 2001 From: benji7425 Date: Tue, 22 Aug 2017 23:02:16 +0100 Subject: [PATCH 24/30] Update feed addition to be simpler --- app/index.js | 21 +++++++++------------ app/models/feed-data.js | 6 +++--- package.json | 10 +++++----- 3 files changed, 17 insertions(+), 20 deletions(-) diff --git a/app/index.js b/app/index.js index e8f9d7e..e20e4ea 100644 --- a/app/index.js +++ b/app/index.js @@ -2,8 +2,9 @@ const FileSystem = require("fs"); //external lib imports -const JsonFile = require("jsonfile"); -const Url = require("url"); +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"); @@ -64,22 +65,18 @@ const HandleMessage = { function addFeed(client, guildsData, message) { const parameters = message.content.split(" "); //expect !addfeed + const feedUrl = [...GetUrls(message.content)][0]; const channel = message.mentions.channels.first(); - if(!channel) - return message.reply("Please tag a channel with #channel-name"); - const feedUrl = parameters[2], channelName = channel.name, roleName = parameters[4]; + if (!feedUrl || !channel) + return message.reply("Please provide both a channel and an RSS feed URL. You can optionally @mention a role also."); - 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 role = message.mentions.roles.first(); const feedData = new FeedData({ url: feedUrl, - channelName: channelName, - roleName: roleName + 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 diff --git a/app/models/feed-data.js b/app/models/feed-data.js index 3e3e954..7ff2e80 100644 --- a/app/models/feed-data.js +++ b/app/models/feed-data.js @@ -2,9 +2,9 @@ const DiscordUtil = require("discordjs-util"); //external lib imports -const Dns = require("dns"); -const Url = require("url"); -const FeedRead = require("feed-read"); +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({ url, channelName, roleName, cachedLinks }) { diff --git a/package.json b/package.json index b918016..0f2b9b7 100644 --- a/package.json +++ b/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" } -} \ No newline at end of file +} From ab300263ab3bc4d26cff3e7844fafbd1f22bfdfa Mon Sep 17 00:00:00 2001 From: benji7425 Date: Wed, 23 Aug 2017 01:04:56 +0100 Subject: [PATCH 25/30] Add detection of links being posted and fix startup links retrieval --- app/index.js | 22 +++++++++++++--------- app/models/feed-data.js | 12 ++++++++++-- app/models/guild-data.js | 4 ++-- 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/app/index.js b/app/index.js index e20e4ea..0c62d60 100644 --- a/app/index.js +++ b/app/index.js @@ -22,11 +22,9 @@ module.exports = (client) => { const guildsData = FileSystem.existsSync(SAVE_FILE) ? fromJSON(JsonFile.readFileSync(SAVE_FILE)) : {}; setInterval(() => writeFile(guildsData), config.saveIntervalSec * 1000); - 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); + parseLinksInGuilds(client.guilds, guildsData).then(() => writeFile(guildsData)) + .then(() => checkFeedsInGuilds(client.guilds, guildsData)) + .then(() => setInterval(() => checkFeedsInGuilds(client.guilds, guildsData), config.feedCheckIntervalSec * 1000)); //set up an interval to check all the feeds //set up an on message handler to detect when links are posted client.on("message", message => { @@ -59,6 +57,12 @@ const HandleMessage = { break; } } + else if (guildsData[message.guild.id]) { + guildsData[message.guild.id].feeds.forEach(feedData => { + if (message.channel.name === feedData.channelName) + feedData.cachedLinks.push(...GetUrls(message.content)); //spread the urlSet returned by GetUrls into the cache array + }); + } } }; @@ -76,7 +80,7 @@ function addFeed(client, guildsData, message) { const feedData = new FeedData({ url: feedUrl, channelName: channel.name, - roleName: role.name + roleName: role ? role.name : null }); //ask the user if they're happy with the details they set up, save if yes, don't if no @@ -103,10 +107,10 @@ function checkFeedsInGuilds(guilds, guildsData) { function parseLinksInGuilds(guilds, guildsData) { const promises = []; - for (let guild of guilds) { - const guildData = guildsData[guild.id]; + for (let guildId of guilds.keys()) { + const guildData = guildsData[guildId]; if (guildData) - promises.push(guildData.cachePastPostedLinks()); + promises.push(guildData.cachePastPostedLinks(guilds.get(guildId))); } return Promise.all(promises); } diff --git a/app/models/feed-data.js b/app/models/feed-data.js index 7ff2e80..2372304 100644 --- a/app/models/feed-data.js +++ b/app/models/feed-data.js @@ -5,6 +5,7 @@ const DiscordUtil = require("discordjs-util"); 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 +const GetUrls = require("get-urls"); //for extracting urls from messages module.exports = class FeedData { constructor({ url, channelName, roleName, cachedLinks }) { @@ -12,6 +13,13 @@ module.exports = class FeedData { this.channelName = channelName; this.roleName = roleName; this.cachedLinks = cachedLinks || []; + + this.cachedLinks.push = (...elements) => { + const unique = elements + .map(el => normaliseUrl(el)) //normalise all the urls + .filter(el => !this.cachedLinks.includes(el)); //filter out any already cached + Array.prototype.push.apply(this.cachedLinks, unique); + }; } /** @@ -25,7 +33,7 @@ module.exports = class FeedData { return new Promise((resolve, reject) => { channel.fetchMessages({ limit: 100 }) .then(messages => { - messages.forEach(m => Array.prototype.push.apply(this.cachedLinks, getUrls(m))); //push all the links in each message into our links array + messages.forEach(m => this.cachedLinks.push(...GetUrls(m.content))); //push all the links in each message into our links array resolve(this); }) .catch(reject); @@ -64,7 +72,7 @@ function normaliseUrl(url) { 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 + url = url.replace(/(www.)?youtube.com\/watch\?v=/, "youtu.be/"); //turn full url into share url return url; } diff --git a/app/models/guild-data.js b/app/models/guild-data.js index 94253d2..d1ea352 100644 --- a/app/models/guild-data.js +++ b/app/models/guild-data.js @@ -7,11 +7,11 @@ module.exports = class GuildData { this.feeds = feeds.map(feed => new FeedData(feed)); } - cachePastPostedLinks() { + cachePastPostedLinks(guild) { const promises = []; this.feeds.forEach(feed => { - promises.push(feed.cachePastPostedLinks(this).catch(Util.dateError)); + promises.push(feed.updatePastPostedLinks(guild).catch(Util.dateError)); }); return Promise.all(promises); From f96b1a35859f8901a2225bc881e8cc59667617ea Mon Sep 17 00:00:00 2001 From: benji7425 Date: Wed, 23 Aug 2017 01:15:11 +0100 Subject: [PATCH 26/30] Fix issue posting link if role is null --- app/models/feed-data.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/feed-data.js b/app/models/feed-data.js index 2372304..d39ce45 100644 --- a/app/models/feed-data.js +++ b/app/models/feed-data.js @@ -57,8 +57,8 @@ module.exports = class FeedData { 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); + const role = this.roleName ? guild.roles.find(role => role.name.toLowerCase() === this.roleName.toLowerCase()) : null; + channel.send(role ? role + " " : "" + latest); } } }); From bf9fbdcdaef48537a587fdb214a916a1e5e90c71 Mon Sep 17 00:00:00 2001 From: benji7425 Date: Wed, 23 Aug 2017 01:27:06 +0100 Subject: [PATCH 27/30] Add maximum link cache size --- app/config.json | 1 + app/index.js | 7 ++++--- app/models/feed-data.js | 8 ++++++-- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/app/config.json b/app/config.json index ff568bc..77f27d4 100644 --- a/app/config.json +++ b/app/config.json @@ -1,6 +1,7 @@ { "saveIntervalSec": 60, "feedCheckIntervalSec": 30, + "maxCacheSize": 10, "commands": { "version": "version", "addFeed": "add-feed" diff --git a/app/index.js b/app/index.js index 0c62d60..a628344 100644 --- a/app/index.js +++ b/app/index.js @@ -53,7 +53,7 @@ const HandleMessage = { message.reply("v" + require("../package.json").version); break; case config.commands.addFeed: - addFeed(client, guildsData, message); + addFeed(client, guildsData, message, config.maxCacheSize); break; } } @@ -66,7 +66,7 @@ const HandleMessage = { } }; -function addFeed(client, guildsData, message) { +function addFeed(client, guildsData, message, maxCacheSize) { const parameters = message.content.split(" "); //expect !addfeed const feedUrl = [...GetUrls(message.content)][0]; @@ -80,7 +80,8 @@ function addFeed(client, guildsData, message) { const feedData = new FeedData({ url: feedUrl, channelName: channel.name, - roleName: role ? role.name : null + roleName: role ? role.name : null, + maxCacheSize: maxCacheSize }); //ask the user if they're happy with the details they set up, save if yes, don't if no diff --git a/app/models/feed-data.js b/app/models/feed-data.js index d39ce45..8f9198e 100644 --- a/app/models/feed-data.js +++ b/app/models/feed-data.js @@ -8,17 +8,21 @@ const FeedRead = require("feed-read"); //for extracing new links from RSS feeds const GetUrls = require("get-urls"); //for extracting urls from messages module.exports = class FeedData { - constructor({ url, channelName, roleName, cachedLinks }) { + constructor({ url, channelName, roleName, cachedLinks, maxCacheSize }) { this.url = url; this.channelName = channelName; this.roleName = roleName; this.cachedLinks = cachedLinks || []; + this.maxCacheSize = maxCacheSize || 10; this.cachedLinks.push = (...elements) => { const unique = elements .map(el => normaliseUrl(el)) //normalise all the urls .filter(el => !this.cachedLinks.includes(el)); //filter out any already cached - Array.prototype.push.apply(this.cachedLinks, unique); + Array.prototype.push.apply(this.cachedLinks, unique); + + if(this.cachedLinks.length > this.maxCacheSize) + this.cachedLinks.splice(0, this.cachedLinks.length - this.maxCacheSize); //remove the # of elements above the max from the beginning }; } From d0d148dbca1077777f4c303ad77b57552a914010 Mon Sep 17 00:00:00 2001 From: benji7425 Date: Wed, 23 Aug 2017 01:47:18 +0100 Subject: [PATCH 28/30] Add view-feeds command --- app/config.json | 3 ++- app/index.js | 18 ++++++++++++------ app/models/feed-data.js | 7 ++++++- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/app/config.json b/app/config.json index 77f27d4..66f76fd 100644 --- a/app/config.json +++ b/app/config.json @@ -4,6 +4,7 @@ "maxCacheSize": 10, "commands": { "version": "version", - "addFeed": "add-feed" + "addFeed": "add-feed", + "viewFeeds": "view-feeds" } } \ No newline at end of file diff --git a/app/index.js b/app/index.js index a628344..c087410 100644 --- a/app/index.js +++ b/app/index.js @@ -30,18 +30,18 @@ module.exports = (client) => { client.on("message", message => { if (message.author.id !== client.user.id) { //check the bot isn't triggering itself if (message.channel.type === "dm") - HandleMessage.DM(client, config, message); + HandleMessage.dm(client, config, message); else if (message.channel.type === "text" && message.member) - HandleMessage.Text(client, config, message, guildsData); + HandleMessage.text(client, config, message, guildsData); } }); }; const HandleMessage = { - DM: (client, config, message) => { + dm: (client, config, message) => { message.reply("This bot does not have any handling for direct messages. To learn more or get help please visit http://benji7425.github.io, or join my Discord server here: https://discord.gg/SSkbwSJ"); }, - Text: (client, config, message, guildsData) => { + text: (client, config, message, guildsData) => { //handle admins invoking commands if (message.content.startsWith(message.guild.me.toString()) //user is @mention-ing the bot && message.member.permissions.has("ADMINISTRATOR")) //user has admin perms @@ -55,6 +55,9 @@ const HandleMessage = { case config.commands.addFeed: addFeed(client, guildsData, message, config.maxCacheSize); break; + case config.commands.viewFeeds: + viewFeeds(client, guildsData, message); + break; } } else if (guildsData[message.guild.id]) { @@ -67,8 +70,6 @@ const HandleMessage = { }; function addFeed(client, guildsData, message, maxCacheSize) { - const parameters = message.content.split(" "); //expect !addfeed - const feedUrl = [...GetUrls(message.content)][0]; const channel = message.mentions.channels.first(); @@ -102,6 +103,11 @@ function addFeed(client, guildsData, message, maxCacheSize) { }); } +function viewFeeds(client, guildsData, message){ + const guildData = guildsData[message.guild.id]; + message.reply(guildData.feeds.map(f => f.toString()).join("\n")); +} + function checkFeedsInGuilds(guilds, guildsData) { Object.keys(guildsData).forEach(key => guildsData[key].checkFeeds(guilds)); } diff --git a/app/models/feed-data.js b/app/models/feed-data.js index 8f9198e..86c93e9 100644 --- a/app/models/feed-data.js +++ b/app/models/feed-data.js @@ -21,7 +21,7 @@ module.exports = class FeedData { .filter(el => !this.cachedLinks.includes(el)); //filter out any already cached Array.prototype.push.apply(this.cachedLinks, unique); - if(this.cachedLinks.length > this.maxCacheSize) + if (this.cachedLinks.length > this.maxCacheSize) this.cachedLinks.splice(0, this.cachedLinks.length - this.maxCacheSize); //remove the # of elements above the max from the beginning }; } @@ -68,6 +68,11 @@ module.exports = class FeedData { }); }); } + + toString() { + const blacklist = ["cachedLinks", "maxCacheSize"]; + return `\`\`\`JavaScript\n ${JSON.stringify(this, (k, v) => !blacklist.includes(k) ? v : undefined, "\t")} \`\`\``; + } }; function normaliseUrl(url) { From f82743d443e8ba2eaf4c0c860a94e25a1fa4c86c Mon Sep 17 00:00:00 2001 From: benji7425 Date: Wed, 23 Aug 2017 02:04:32 +0100 Subject: [PATCH 29/30] Fix issues detecting old links, and posting links with role mention --- app/models/feed-data.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/feed-data.js b/app/models/feed-data.js index 86c93e9..b314fc5 100644 --- a/app/models/feed-data.js +++ b/app/models/feed-data.js @@ -37,7 +37,7 @@ module.exports = class FeedData { return new Promise((resolve, reject) => { channel.fetchMessages({ limit: 100 }) .then(messages => { - messages.forEach(m => this.cachedLinks.push(...GetUrls(m.content))); //push all the links in each message into our links array + new Map([...messages].reverse()).forEach(m => this.cachedLinks.push(...GetUrls(m.content))); //push all the links in each message into our links array resolve(this); }) .catch(reject); @@ -62,7 +62,7 @@ module.exports = class FeedData { const channel = guild.channels.find(ch => ch.type === "text" && ch.name.toLowerCase() === this.channelName.toLowerCase()); const role = this.roleName ? guild.roles.find(role => role.name.toLowerCase() === this.roleName.toLowerCase()) : null; - channel.send(role ? role + " " : "" + latest); + channel.send((role ? role + " " : "") + latest); } } }); From 9476ec2be1f82aa92daad081ddbf134ee2351414 Mon Sep 17 00:00:00 2001 From: benji7425 Date: Wed, 23 Aug 2017 02:26:16 +0100 Subject: [PATCH 30/30] Add feed removal command --- app/config.json | 1 + app/index.js | 27 +++++++++++++++++++++++---- app/models/feed-data.js | 8 +++----- package.json | 3 ++- 4 files changed, 29 insertions(+), 10 deletions(-) diff --git a/app/config.json b/app/config.json index 66f76fd..aa0dfce 100644 --- a/app/config.json +++ b/app/config.json @@ -5,6 +5,7 @@ "commands": { "version": "version", "addFeed": "add-feed", + "removeFeed": "remove-feed", "viewFeeds": "view-feeds" } } \ No newline at end of file diff --git a/app/index.js b/app/index.js index c087410..cb785ce 100644 --- a/app/index.js +++ b/app/index.js @@ -55,8 +55,11 @@ const HandleMessage = { case config.commands.addFeed: addFeed(client, guildsData, message, config.maxCacheSize); break; + case config.commands.removeFeed: + removeFeed(client, guildsData, message); + break; case config.commands.viewFeeds: - viewFeeds(client, guildsData, message); + viewFeeds(client, guildsData[message.guild.id], message); break; } } @@ -86,7 +89,7 @@ function addFeed(client, guildsData, message, maxCacheSize) { }); //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?\n ```JavaScript\n" + JSON.stringify(feedData, null, "\n") + "```") + DiscordUtil.ask(client, message.channel, message.member, "Are you happy with this?\n" + feedData.toString()) .then(responseMessage => { //if they responded yes, save the feed and let them know, else tell them to start again @@ -103,8 +106,24 @@ function addFeed(client, guildsData, message, maxCacheSize) { }); } -function viewFeeds(client, guildsData, message){ - const guildData = guildsData[message.guild.id]; +function removeFeed(client, guildsData, message) { + const parameters = message.content.split(" "); + if (parameters.length !== 3) + message.reply(`Please use the command as such:\n\`\`\` @${client.user.username} remove-feed feedid\`\`\``); + else { + const guildData = guildsData[message.guild.id]; + const idx = guildData.feeds.findIndex(feed => feed.id === parameters[2]); + if (!Number.isInteger(idx)) + message.reply("Can't find feed with id " + parameters[2]); + else { + guildData.feeds.splice(idx, 1); + writeFile(guildsData); + message.reply("Feed removed!"); + } + } +} + +function viewFeeds(client, guildData, message) { message.reply(guildData.feeds.map(f => f.toString()).join("\n")); } diff --git a/app/models/feed-data.js b/app/models/feed-data.js index b314fc5..da5a90c 100644 --- a/app/models/feed-data.js +++ b/app/models/feed-data.js @@ -6,9 +6,11 @@ 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 const GetUrls = require("get-urls"); //for extracting urls from messages +const ShortID = require("shortid"); //to provide ids for each feed, allowing guilds to remove them module.exports = class FeedData { - constructor({ url, channelName, roleName, cachedLinks, maxCacheSize }) { + constructor({ id, url, channelName, roleName, cachedLinks, maxCacheSize }) { + this.id = id || ShortID.generate(); this.url = url; this.channelName = channelName; this.roleName = roleName; @@ -84,8 +86,4 @@ function normaliseUrl(url) { url = url.replace(/(www.)?youtube.com\/watch\?v=/, "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); } \ No newline at end of file diff --git a/package.json b/package.json index 0f2b9b7..59632b8 100644 --- a/package.json +++ b/package.json @@ -23,6 +23,7 @@ "dns": "0.2.2", "feed-read": "0.0.1", "get-urls": "7.0.0", - "jsonfile": "3.0.1" + "jsonfile": "3.0.1", + "shortid": "2.2.8" } }