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" } }