Fix issues with feed addition
This commit is contained in:
parent
385f0f8656
commit
ac9da5f879
24
app/index.js
24
app/index.js
|
@ -62,22 +62,31 @@ const HandleMessage = {
|
||||||
|
|
||||||
function addFeed(client, guildsData, message) {
|
function addFeed(client, guildsData, message) {
|
||||||
const parameters = message.content.split(" "); //expect !addfeed <url> <channelName> <roleName>
|
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({
|
const feedData = new FeedData({
|
||||||
link: parameters[1],
|
url: feedUrl,
|
||||||
channelName: parameters[2],
|
channelName: channelName,
|
||||||
roleName: parameters[3]
|
roleName: roleName
|
||||||
});
|
});
|
||||||
|
|
||||||
//ask the user if they're happy with the details they set up, save if yes, don't if no
|
//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 => {
|
.then(responseMessage => {
|
||||||
|
|
||||||
//if they responded yes, save the feed and let them know, else tell them to start again
|
//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])
|
if (!guildsData[message.guild.id])
|
||||||
guildsData[message.guild.id] = new GuildData({ id: message.guild.id, feeds: [] });
|
guildsData[message.guild.id] = new GuildData({ id: message.guild.id, feeds: [] });
|
||||||
|
|
||||||
guildsData[message.guild.id].feeds.push(feedData);
|
guildsData[message.guild.id].feeds.push(feedData);
|
||||||
|
writeFile(guildsData);
|
||||||
responseMessage.reply("Your new feed has been saved!");
|
responseMessage.reply("Your new feed has been saved!");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -104,6 +113,7 @@ function writeFile(guildsData) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function fromJSON(json) {
|
function fromJSON(json) {
|
||||||
const guildIDs = Object.keys(json);
|
const guildsData = Object.keys(json);
|
||||||
guildIDs.forEach(guildID => { guildIDs[guildID] = new GuildData(guildIDs[guildID]); });
|
guildsData.forEach(guildID => { json[guildID] = new GuildData(json[guildID]); });
|
||||||
|
return json;
|
||||||
}
|
}
|
|
@ -7,8 +7,8 @@ const Url = require("url");
|
||||||
const FeedRead = require("feed-read");
|
const FeedRead = require("feed-read");
|
||||||
|
|
||||||
module.exports = class FeedData {
|
module.exports = class FeedData {
|
||||||
constructor({ link, channelName, roleName, cachedLinks }) {
|
constructor({ url, channelName, roleName, cachedLinks }) {
|
||||||
this.link = link;
|
this.url = url;
|
||||||
this.channelName = channelName;
|
this.channelName = channelName;
|
||||||
this.roleName = roleName;
|
this.roleName = roleName;
|
||||||
this.cachedLinks = cachedLinks | [];
|
this.cachedLinks = cachedLinks | [];
|
||||||
|
@ -33,11 +33,11 @@ module.exports = class FeedData {
|
||||||
}
|
}
|
||||||
|
|
||||||
check(guild) {
|
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)
|
if (err)
|
||||||
DiscordUtil.dateError("Connection Error: Can't resolve host", err); //log our error if we can't resolve the host
|
DiscordUtil.dateError("Connection Error: Can't resolve host", err); //log our error if we can't resolve the host
|
||||||
else
|
else
|
||||||
FeedRead(this.link, (err, articles) => { //check the feed
|
FeedRead(this.url, (err, articles) => { //check the feed
|
||||||
if (err)
|
if (err)
|
||||||
DiscordUtil.dateError(err);
|
DiscordUtil.dateError(err);
|
||||||
else {
|
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());
|
const channel = guild.channels.first(ch => ch.type === "text" && ch.name.toLower() === this.channelName.toLower());
|
||||||
channel.send(link);
|
channel.send(url);
|
||||||
}
|
}
|
||||||
|
|
||||||
function normaliseUrl(url) {
|
function normaliseUrl(url) {
|
||||||
|
|
|
@ -18,6 +18,6 @@ module.exports = class GuildData {
|
||||||
}
|
}
|
||||||
|
|
||||||
checkFeeds() {
|
checkFeeds() {
|
||||||
this.feeds.forEach(feed => feed.check());
|
// this.feeds.forEach(feed => feed.check());
|
||||||
}
|
}
|
||||||
};
|
};
|
Loading…
Reference in a new issue