satanic-security/interactions/commands/add.js
2025-06-17 16:39:33 -06:00

88 lines
4 KiB
JavaScript

const fs = require('fs');
const path = require('path');
const https = require('https');
const execute = async (interaction, db, client) => {
if (!interaction.isCommand()) return;
await interaction.deferReply({ ephemeral: true }).catch(console.error);
db.get("SELECT * FROM badActors WHERE id = ?", [interaction.options.getUser("user")], async (err, row) => {
if (err) {
console.error("Database error:", err);
return interaction.editReply({ content: "An error occurred while accessing the database.", ephemeral: true });
}
if (row) {
return interaction.editReply({ content: "That user is already listed!", ephemeral: true });
} else {
const user = interaction.options.getUser("user").id;
const comment = interaction.options.getString("comment") || "No comment provided";
const attachments = [];
for (let i = 1; i <= 4; i++) {
const attachment = interaction.options.getAttachment(`attachment${i}`);
const fileExt = attachment.name ? path.extname(attachment.name) : '';
if (attachment) {
const url = attachment.url;
const filename = `${user.id}-${i}${attachment}.${fileExt}`;
const filepath = path.join(__dirname, '../../storage', filename);
// Await the download before continuing
await new Promise((resolve, reject) => {
const file = fs.createWriteStream(filepath);
https.get(url, (response) => {
response.pipe(file);
file.on('finish', () => {
file.close(resolve);
});
}).on('error', (err) => {
fs.unlink(filepath, () => { });
console.error('Error downloading attachment:', err);
reject(err);
});
});
attachments.push(filename);
}
}
const timestamp = Date.now();
db.run("INSERT INTO badActors (id, reportedBy, comment, timestamp, attachments) VALUES (?, ?, ?, ?, ?)", [
user,
interaction.user.id,
comment,
timestamp,
JSON.stringify(attachments)
], function (err) {
if (err) {
console.error("Database error:", err);
return interaction.editReply({ content: "An error occurred while adding the user to the list.", ephemeral: true });
}
const embed = {
title: "Bad Actor Added",
description: `User <@${user}> has been added to the bad actors list.`,
fields: [
{ name: "Reported By", value: `<@${interaction.user.id}>`, inline: true },
{ name: "Comment", value: comment, inline: false },
{ name: "Timestamp", value: new Date(timestamp).toLocaleString(), inline: true }
],
color: 0xff0000
};
interaction.editReply({ embeds: [embed], files: attachments.map(file => path.join(__dirname, '../../storage', file))});
client.channels.fetch(process.env.ADMIN_LOG_CHANNEL).then(logChan => {
logChan.send({
embeds: [embed],
content: `User <@${user}> has been added to the bad actors list by <@${interaction.user.id}>.`,
files: attachments.map(file => ({
attachment: path.join(__dirname, '../../storage', file),
name: file
}))
}).catch(console.error);
})
});
}
});
}
module.exports = { execute };