I am developing a plugin and this is where I'm stuck. If one of the attachments uploaded more than once is not an image or a video, I try to remove the ones that are not, but I could not find any way to remove the faulty one.
/* @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
* | [ Image / Link Checker ] |
* @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ */
// Allowed domains
let allowedDomains = [];
await db.query('SELECT imageChannel, linkChannel, warnChannel, allowedLinks FROM config WHERE server = ?', message.guild.id, async function(err, res, fields){
if (err) throw err;
// Validation
const warnChannel = await res[0]['warnChannel'];
let validAttachment = false;
let validLink = false;
let validContent = "";
let imageChannel = [];
imageChannel.push(res[0]['imageChannel']);
let linkChannel = [];
linkChannel.push(res[0]['linkChannel']);
let allowedLinks = [];
allowedLinks.push(res[0]['allowedLinks']);
let disallowedLinks = [];
disallowedLinks.push(res[0]['disallowedLinks']);
// Allowed file extensions
const allowedExtensions = ['png', 'jpg', 'jpeg', 'gif', 'mp4'];
if (imageChannel.includes(message.channel.id)) {
const attachments = message.attachments;
if (attachments.size > 0) {
let attachmentsList = [];
attachments.forEach(async (attachment) => {
fileExtension = attachment.name.toLowerCase().slice((attachment.name.lastIndexOf(".") - 1 >>> 0) + 2);
if (!allowedExtensions.includes(fileExtension)) {
attachmentsList.push(attachment);
console.log(attachment.id);
// Delete the message containing the attachment
await message.attachments.delete(attachment.id);
// Optionally, you can send a warning message to the user
message.guild.channels.cache.get(warnChannel).send(`${message.author}, why are you sharing unauthorized content?`);
}
});
attachmentsList.forEach(async (attachmentData) => {
const attachmentsToDelete = message.attachments.filter((attachment) => attachment.id == attachmentData);
if (attachmentsToDelete.size > 0) {
try {
// Delete the message containing the attachment
await message.delete();
console.log(`Deleted message with attachment ID: ${attachmentData}`);
} catch (error) {
console.error(`Error deleting message: ${error}`);
}
}
});
}
}
});
If one of the attachments uploaded more than once is not an image or a video, I try to remove the ones that are not, but I could not find any way to remove the faulty one.
Rather than deleting, considering editing the message with the filtered Attachments.
attachmentsList.forEach(async (attachmentData) => {
const files = message.attachments.filter((attachment) => attachment.id !== attachmentData); // 1
if (files.size !== message.attachments.size) { // 2
try {
await message.edit({ files }); // 3
console.log(`Deleted message with attachment ID: ${attachmentData}`);
} catch (error) {
console.error(`Error deleting message: ${error}`);
}
}
});