diff --git a/commands/add.js b/commands/add.js index cc566af..91fb45a 100644 --- a/commands/add.js +++ b/commands/add.js @@ -30,7 +30,7 @@ module.exports = { if (!category) category = "ALL"; - libCore.addSource(title, link, category, (err, result) => { + libCore.addSource(title, link, category, interaction.guildId, interaction.channelId, (err, result) => { console.log("Result from adding entry", result); if (result) { @@ -39,7 +39,7 @@ module.exports = { interaction.reply(`${title} already exists in the list of RSS sources`); } - libCore.loadFeeds(); + libCore.loadFeeds(); }); }catch(err){ log.ERROR(err) diff --git a/libCore.js b/libCore.js index b7b8ece..87a0266 100644 --- a/libCore.js +++ b/libCore.js @@ -35,7 +35,7 @@ var storageSource = new storageHandler.Storage(userTable); * @param {string} link - URL of RSS feed. * @param {string} category - Category of RSS feed. */ -exports.addSource = function (title, link, category, callback) { +exports.addSource = function (title, link, category, guildId, channelId, callback) { for (i = 0; i < feeds.length; i++) { if (feeds[i].link == link) { @@ -47,7 +47,9 @@ exports.addSource = function (title, link, category, callback) { "fields": { "title": title, "link": link, - "category": category + "category": category, + 'guild_id': guildId, + "channel_id": channelId } }], function (err, record) { if (err) { diff --git a/libStorage.js b/libStorage.js index d409c09..fc87d85 100644 --- a/libStorage.js +++ b/libStorage.js @@ -36,12 +36,16 @@ class RSSRecord { * @param {*} _title * @param {*} _link * @param {*} _category + * @param {*} _guild_id + * @param {*} _channel_id */ - constructor(_id, _title, _link, _category) { + constructor(_id, _title, _link, _category, _guild_id, _channel_id) { this.id = _id; this.title = _title; this.link= _link; - this.category = _category; + this.category = _category; + this.guild_id = _guild_id; + this.channel_id = _channel_id; } getId() { @@ -82,7 +86,7 @@ exports.Storage = class Storage { for (var entry of toBeSaved) { entry = entry.fields log.DEBUG("Entry:", entry); - this.returnRecord(undefined, entry.title, entry.link, entry.category, (err, record) => { + this.returnRecord(undefined, entry.title, entry.link, entry.category, entry.guild_id, entry.channel_id, (err, record) => { if (err) callback(err, undefined); newRecords.push(record); if (toBeSaved.length === 1) { @@ -119,13 +123,8 @@ exports.Storage = class Storage { */ checkForTitle(title, callback) { if (!title) callback(new Error("No title given when checking for title"), undefined); - const sqlQuery = `SELECT * FROM ${this.dbTable} WHERE title = '${title}'`; - - runSQL(sqlQuery, this.connection, (err, rows) => { - if (err) callback(err, undefined); - if (rows[0]?.title) callback(undefined, true); - else callback(undefined, false); - }) + + this.getRecordBy("title", title, callback); } /** @@ -157,7 +156,7 @@ exports.Storage = class Storage { callback(new Error("Entry object malformed, check the object before saving it"), undefined) } - const sqlQuery = `INSERT INTO ${this.dbTable} (title, link, category) VALUES ('${entryObject.title}', '${entryObject.link}', '${entryObject.category}');`; + const sqlQuery = `INSERT INTO ${this.dbTable} (title, link, category, guild_id, channel_id) VALUES ('${entryObject.title}', '${entryObject.link}', '${entryObject.category}', '${entryObject.guild_id}', '${entryObject.channel_id}');`; log.DEBUG(`Adding new entry with SQL query: '${sqlQuery}'`) @@ -179,6 +178,8 @@ exports.Storage = class Storage { if (!entryObject.link) callback(new Error("No link given before updating"), undefined); queryParams.push(`link = '${entryObject.link}'`); if (entryObject.category) queryParams.push(`category = '${entryObject.category}'`); + if (entryObject.guild_id) queryParams.push(`guild_id = '${entryObject.guild_id}'`); + if (entryObject.channel_id) queryParams.push(`channel_id = '${entryObject.channel_id}'`); let sqlQuery = `UPDATE ${this.dbTable} SET`; @@ -230,12 +231,14 @@ exports.Storage = class Storage { * @param {*} _category The category of the record * @param {*} callback Callback function to return an error or the record */ - returnRecord(_id, _title, _link, _category, callback) { - log.DEBUG(`Return record for these values: ID: '${_id}', Title: '${_title}', Category: '${_category}', Link: '${_link}'`) - if (!_link && !_title) callback(new Error("No link or title given when creating a record"), undefined); + returnRecord(_id, _title, _link, _category, _guild_id, _channel_id, callback) { + log.DEBUG(`Return record for these values: ID: '${_id}', Title: '${_title}', Category: '${_category}', Link: '${_link}', Guild: '${_guild_id}', Channel:'${_channel_id}'`) + if (!_link && !_title && !_guild_id && !_channel_id) callback(new Error("No link or title given when creating a record"), undefined); let entryObject = { "title": _title, - "link": _link + "link": _link, + "guild_id": _guild_id, + "channel_id": _channel_id } if (_category) entryObject.category = _category; @@ -245,7 +248,7 @@ exports.Storage = class Storage { if (err) callback(err, undefined); this.getRecordBy('id', entryObject.id, (err, record) => { if (err) callback(err, undefined); - callback(undefined, new RSSRecord(record.id, record.title, record.link, record.category)); + callback(undefined, new RSSRecord(record.id, record.title, record.link, record.category, record.guild_id, record.channel_id)); }) }) } @@ -257,7 +260,7 @@ exports.Storage = class Storage { if (err) callback(err, undefined); this.getRecordBy("title", entryObject.title, (err, record) => { if (err) callback(err, undefined); - callback(undefined, new RSSRecord(record.id, record.title, record.link, record.category)); + callback(undefined, new RSSRecord(record.id, record.title, record.link, record.category, record.guild_id, record.channel_id)); }) }); } @@ -266,7 +269,7 @@ exports.Storage = class Storage { if (err) callback(err, undefined); this.getRecordBy('title', entryObject.title, (err, record) => { if (err) callback(err, undefined); - callback(undefined, new RSSRecord(record.id, record.title, record.link, record.category)); + callback(undefined, new RSSRecord(record.id, record.title, record.link, record.category, record.guild_id, record.channel_id)); }) }) } @@ -288,7 +291,7 @@ exports.Storage = class Storage { if (err) callback(err, undefined); for (const row of rows) { log.DEBUG("Row from SQL query:", row); - rssRecords.push(new RSSRecord(row.id, row.title, row.link, row.category)); + rssRecords.push(new RSSRecord(row.id, row.title, row.link, row.category, row.guild_id, row.channel_id)); } log.DEBUG("All records:", rssRecords); callback(undefined, rssRecords);