Fix issues with feed addition

This commit is contained in:
benji7425 2017-08-13 03:12:20 +01:00
parent 385f0f8656
commit ac9da5f879
3 changed files with 24 additions and 14 deletions

View File

@ -62,22 +62,31 @@ 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];
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;
}

View File

@ -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) {

View File

@ -18,6 +18,6 @@ module.exports = class GuildData {
}
checkFeeds() {
this.feeds.forEach(feed => feed.check());
// this.feeds.forEach(feed => feed.check());
}
};