diff --git a/commands/update.js b/commands/update.js index 7b10811..bc828b2 100644 --- a/commands/update.js +++ b/commands/update.js @@ -42,6 +42,9 @@ const execute = async (interaction) => { case "category": await interaction.user.send({ content: "Please provide a new category for the product. Say `cancel` to exit. Set to `~none` to remove." }); break; + case "delete": + await interaction.user.send({ content: "Are you sure you want to delete this product? This action is irreversible, and all buyees will lose access to the file forever! Type `confirm` to delete, or `cancel` to exit." }); + break; default: return interaction.editReply({ content: "Invalid field provided.", ephemeral: true }); } diff --git a/messageHandlers/update_prod.js b/messageHandlers/update_prod.js index 01991a4..0a502f6 100644 --- a/messageHandlers/update_prod.js +++ b/messageHandlers/update_prod.js @@ -132,6 +132,50 @@ const execute = async (message) => { await pool.query('UPDATE products SET category = ? WHERE id = ?', [newCategory, global.productUpdateData[message.author.id].id]); message.channel.send('Category updated!'); break; + case "delete": // Delete + if (message.content.toLowerCase() === 'cancel') { + cancel(message.author); + return; + } + if (message.content.toLowerCase() !== 'confirm') { + message.channel.send('You must type `confirm` to delete the product, or `cancel` to exit.'); + return; + } + // Proceed with deletion + message.channel.send('Deletion Confirmed. Starting deletion process...').then(async msg => { + let curMsg = msg.content; + // Delete fileAuth + curMsg = curMsg + '\nDeleting file authorizations...' + await msg.edit(curMsg); + await pool.query('DELETE FROM fileAuth WHERE product = ?', [global.productUpdateData[message.author.id].id]); + // Delete file + curMsg = curMsg + '\nDeleting product file...' + await msg.edit(curMsg); + const [product] = await pool.query('SELECT file FROM products WHERE id = ?', [global.productUpdateData[message.author.id].id]); + if (product) { + const safeFileId = path.basename(product.file); + const filePath = path.join(__dirname, '../productFiles', safeFileId); // Code we use in the CDN route + if (fs.existsSync(filePath)) { + fs.unlinkSync(filePath); + curMsg = curMsg + `\nDeleted file: ${safeFileId}`; + await msg.edit(curMsg); + } else { + curMsg = curMsg + `\nFile not found (skipping): ${safeFileId}`; + await msg.edit(curMsg); + } + } + // Delete purchases + curMsg = curMsg + '\nDeleting purchases...'; + await msg.edit(curMsg); + await pool.query('DELETE FROM purchases WHERE productId = ?', [global.productUpdateData[message.author.id].id]); + // Delete product + curMsg = curMsg + '\nDeleting product...'; + await msg.edit(curMsg); + await pool.query('DELETE FROM products WHERE id = ?', [global.productUpdateData[message.author.id].id]); + curMsg = curMsg + '\nProduct deletion complete.'; + await msg.edit(curMsg); + }); + break; default: message.channel.send('Invalid type. Somehow?'); cancel(message.author);