NotParcel/commands/update.js

64 lines
3.2 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}\`` });
switch (interaction.options.getString("field")) {
case "name":
await interaction.user.send({ content: "Please provide a new name for the product. Say `cancel` to exit." });
break;
case "description":
await interaction.user.send({ content: "Please provide a new description for the product. Say `cancel` to exit." });
break;
case "devProductId":
await interaction.user.send({ content: "Please provide a new developer product ID for the product. Say `cancel` to exit." });
break;
case "imageId":
await interaction.user.send({ content: "Please provide a new image ID for the product. Say `cancel` to exit." });
break;
case "file":
await interaction.user.send({ content: "Please provide a new file for the product. Say `cancel` to exit." });
break;
case "stock":
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":
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 });
}
interaction.editReply({ ephemeral: true, content: "Check your DMs!" });
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 }