Skip to content
This repository has been archived by the owner on Jul 15, 2024. It is now read-only.

Commit

Permalink
feat: cleanup filter collection
Browse files Browse the repository at this point in the history
  • Loading branch information
wdhdev committed Jun 25, 2023
1 parent d61a167 commit e8fb605
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 7 deletions.
32 changes: 32 additions & 0 deletions src/commands/developer/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ module.exports = {
name: "channels",
description: "Clean up the channels collection.",
value: "channels"
},

{
name: "filter",
description: "Clean up the filter collection.",
value: "filter"
}
],
required: true
Expand Down Expand Up @@ -106,6 +112,32 @@ module.exports = {
return;
}

if(collection === "filter") {
const cleanFilter = require("../../util/database/cleanFilter");

const res = await cleanFilter(client);

const result = new Discord.EmbedBuilder()
.setColor(client.config_embeds.default)
.setTitle("🧹 Collection Cleanup")
.setDescription("`filter`")
.addFields (
{ name: "📝 Modified Documents", value: res.modified.length ? `\`\`\`${res.modified.join("\n")}\`\`\`` : "*None*" },
{ name: "🗑️ Removed Documents", value: res.removed.length ? `\`\`\`${res.removed.join("\n")}\`\`\`` : "*None*" }
)

await interaction.editReply({ embeds: [result], ephemeral: true });

if(res.removed.length) {
result.setAuthor({ name: interaction.user.tag.endsWith("#0") ? `@${interaction.user.username}` : interaction.user.tag, iconURL: interaction.user.displayAvatarURL({ format: "png", dynamic: true }), url: `https://discord.com/users/${interaction.user.id}` });
result.setTimestamp();

logsChannel.send({ embeds: [result] });
}

return;
}

return;
}
} catch(err) {
Expand Down
46 changes: 46 additions & 0 deletions src/util/database/cleanFilter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
module.exports = async function cleanChannels(client) {
const schema = require("../../models/filterSchema");

let autobanData = await schema.findOne({ _id: "autoban" });
let blockData = await schema.findOne({ _id: "block" });

const promises = [];

let modifiedData = [];
const removedData = [];

for(const word of autobanData.words) {
promises.push(new Promise(async resolve => {
if(blockData.words.includes(word)) {
blockData.words = blockData.words.filter(item => item !== word);

await blockData.save();

if(!modifiedData.includes("block")) modifiedData.push("block");

resolve(`Modified: block`);
} else {
resolve();
}
}))
}

await Promise.all(promises);

if(!autobanData.words.length) {
await autobanData.delete();

removedData.push("autoban");
}

if(!blockData.words.length) {
await blockData.delete();

removedData.push("block");
}

return {
"modified": modifiedData.filter(item => !removedData.includes(item)),
"removed": removedData
}
}
14 changes: 7 additions & 7 deletions src/util/send.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,9 @@ module.exports = async function (message, client, Discord) {
if(data && data.channel) {
const chatChannel = client.channels.cache.get(data.channel);

if(!guild.members.me.permissions.has(requiredPerms)) return resolve(null);
if(!guild.members.me.permissions.has(requiredPerms)) return resolve();

if(!chatChannel) return resolve(null);
if(!chatChannel) return resolve();

try {
if(data.webhook) {
Expand All @@ -175,7 +175,7 @@ module.exports = async function (message, client, Discord) {
await chatChannel.send({ embeds: [reply ? replyEmbed : null, chat] })
.then(msg => resolve(messages.push(`https://discord.com/channels/${guildId}/${msg.channel_id}/${msg.id}`)))
} catch {
resolve(null);
resolve();
}
})

Expand All @@ -193,22 +193,22 @@ module.exports = async function (message, client, Discord) {
await chatChannel.send({ embeds: [reply ? replyEmbed : null, chat] })
.then(msg => resolve(messages.push(`https://discord.com/channels/${guildId}/${msg.channel_id}/${msg.id}`)))
} catch {
resolve(null);
resolve();
}
}
} else {
try {
await chatChannel.send({ embeds: [reply ? replyEmbed : null, chat] })
.then(msg => resolve(messages.push(`https://discord.com/channels/${guildId}/${msg.channel_id}/${msg.id}`)))
} catch {
resolve(null);
resolve();
}
}
} catch {
resolve(null);
resolve();
}
} else {
resolve(null);
resolve();
}
}).clone()
}))
Expand Down

0 comments on commit e8fb605

Please sign in to comment.