NotParcel/commands/update.js
2025-09-15 10:38:33 -06:00

81 lines
3.6 KiB
JavaScript

const client = global.discord_client
const pool = global.db_pool;
const updateProductHandler = require('../messageHandlers/update_prod.js');
if (!global.productUpdateData) global.productUpdateData = {};
const execute = async (interaction) => {
if (!interaction.guild) return interaction.reply({ content: "This command must be used in a server!", ephemeral: true });
if (global.productUpdateData[interaction.user.id]) return interaction.reply({ content: "You are already updating a product!", ephemeral: true });
try {
// Get the hub for the guild
const guildID = interaction.guild.id;
const [hub] = await pool.query('SELECT * FROM hubs WHERE discordGuild = ?', [guildID]);
if (!hub) return interaction.reply({ content: "Hub not found for this guild", ephemeral: true });
const productName = interaction.options.getString("name");
const [product] = await pool.query('SELECT * FROM products WHERE UPPER(name) LIKE ? AND hubId = ?', [`%${productName.toUpperCase()}%`, hub.id]);
if (!product) return interaction.reply({ content: "Product not found", ephemeral: true });
// Proceed with creation
await interaction.reply({ ephemeral: true, content: "Getting things ready..." });
await interaction.user.send({ content: `Updating product: \`${product.name}\`` });
let finalMsg;
switch (interaction.options.getString("field")) {
case "name":
finalMsg = await interaction.user.send({ content: "Please provide a new name for the product. Say `cancel` to exit." });
break;
case "description":
finalMsg = await interaction.user.send({ content: "Please provide a new description for the product. Say `cancel` to exit." });
break;
case "devProductId":
finalMsg = await interaction.user.send({ content: "Please provide a new developer product ID for the product. Say `cancel` to exit." });
break;
case "imageId":
finalMsg = await interaction.user.send({ content: "Please provide a new image ID for the product. Say `cancel` to exit." });
break;
case "file":
finalMsg = await interaction.user.send({ content: "Please provide a new file for the product. Say `cancel` to exit." });
break;
case "stock":
finalMsg = await interaction.user.send({ content: "Please provide a new stock quantity for the product. Say `cancel` to exit. Set `-1` to disable." });
break;
case "category":
finalMsg = 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":
finalMsg = 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 });
}
await interaction.editReply({
ephemeral: true,
content: "Check your DMs!",
components: [
{
type: 1, // ActionRow
components: [
{
type: 2, // Button
style: 5, // Link button
label: "Go there!",
url: finalMsg.url
}
]
}
]
});
global.productUpdateData[interaction.user.id] = {
id: product.id,
type: interaction.options.getString("field")
};
global.dmHandlers[interaction.user.id] = updateProductHandler;
} catch (err) {
console.error(err);
delete global.productCreationData[interaction.user.id];
return interaction.editReply({ content: "An error occurred during the product creation process.", ephemeral: true });
}
};
module.exports = { execute }