Skip to content

Commit

Permalink
fix - autopost cache wasnt being removed after blacklist update
Browse files Browse the repository at this point in the history
  • Loading branch information
phil-flip committed Apr 2, 2024
1 parent a704f2f commit f378c95
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
14 changes: 11 additions & 3 deletions commands/blacklist/add.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
const postcache = require('../../database/models/postcache');

const autopostchannel = require('../../database/models/autopostchannel');

async function addTag(servertagsblacklist, tag, serverID, managementServerID) {
if (await servertagsblacklist.findOne({ where: { serverID: [serverID, managementServerID], tag } }).catch(ERR)) return false;
await servertagsblacklist.findOrCreate({ where: { serverID, tag } }).catch(ERR);
return true;
}

async function getAutopostChannels(serverID) {
const result = await autopostchannel.findAll({ attributes: ['channelID'], where: { serverID } });
return result;
}

// clear autopost to force changes
function pruneAutopost(channelID) {
postcache.destroy({ where: { channelID } }).catch(ERR);
async function pruneAutopost(serverID) {
const channelIDs = await getAutopostChannels(serverID);
channelIDs.forEach(({ channelID }) => postcache.destroy({ where: { channelID } }).catch(ERR));
}

module.exports.run = async (interaction, servertagsblacklist, tag) => {
Expand All @@ -23,7 +31,7 @@ module.exports.run = async (interaction, servertagsblacklist, tag) => {
const added = await addTag(servertagsblacklist, tagNew, interaction.guild.id, mgmtServer);
if (added) {
messageSuccess(interaction, uwu(`ßß\`${tagNew}\` has been added to the servers blacklist.`));
pruneAutopost(interaction.channel.id);
pruneAutopost(interaction.guild.id);
} else {
messageFail(interaction, uwu(`ßß\`${tagNew}\` is already added to this servers backlist.`));
}
Expand Down
14 changes: 11 additions & 3 deletions commands/blacklist/remove.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
const postcache = require('../../database/models/postcache');

const autopostchannel = require('../../database/models/autopostchannel');

async function removeTag(servertagsblacklist, id, serverID) {
if (!await servertagsblacklist.findOne({ where: { serverID, id } }).catch(ERR)) return false;
await servertagsblacklist.destroy({ where: { serverID, id } }).catch(ERR);
return true;
}

async function getAutopostChannels(serverID) {
const result = await autopostchannel.findAll({ attributes: ['channelID'], where: { serverID } });
return result;
}

// clear autopost to force changes
function pruneAutopost(channelID) {
postcache.destroy({ where: { channelID } }).catch(ERR);
async function pruneAutopost(serverID) {
const channelIDs = await getAutopostChannels(serverID);
channelIDs.forEach(({ channelID }) => postcache.destroy({ where: { channelID } }).catch(ERR));
}

module.exports.run = async (interaction, servertagsblacklist, tag) => {
const added = await removeTag(servertagsblacklist, tag, interaction.guild.id);
if (added) {
// FIXME: LOW: this is a hack to not show tag name in the success and error message. can be fixed by setting the value to the same as the on screen name
messageSuccess(interaction, 'Tag has been removed from the serwers blacklist.');
pruneAutopost(interaction.channel.id);
pruneAutopost(interaction.guild.id);
} else {
messageFail(interaction, 'This tag doesn\'t exist on the serwers backlist. \n(Keep in mind that we have also globally blocked tags!)');
}
Expand Down

0 comments on commit f378c95

Please sign in to comment.