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

64 lines
2.5 KiB
JavaScript

const client = global.discord_client
const pool = global.db_pool;
const createProdHandler = require('../messageHandlers/create_prod.js');
if (!global.productCreationData) global.productCreationData = {};
const execute = async (interaction) => {
if (!interaction.guildId) return interaction.reply({ content: "This command can only be used in a server", ephemeral: true });
console.log("Checking if user is already creating a product");
if (global.productCreationData[interaction.user.id]) return interaction.reply({ content: "You are already creating a product!", ephemeral: true });
global.productCreationData[interaction.user.id] = {
name: interaction.options.getString("name")
};
try {
const productName = global.productCreationData[interaction.user.id].name;
const productResult = await pool.query(`SELECT * FROM products WHERE UPPER(name) = UPPER(?)`, [productName]);
if (productResult.length > 0) {
delete global.productCreationData[interaction.user.id];
return interaction.reply({ content: "A product with this name already exists!", ephemeral: true });
}
console.log("Checking guild hub");
const guildId = interaction.guildId;
const hubResult = await pool.query(`SELECT * FROM hubs WHERE discordGuild = ?`, [guildId]);
if (hubResult.length === 0) {
console.log("No hub found");
delete global.productCreationData[interaction.user.id];
return interaction.reply({ content: "This guild does not have a hub set up!", ephemeral: true });
}
console.log("Hub found");
// Proceed with creation
await interaction.reply({ ephemeral: true, content: "Getting things ready..." });
await interaction.user.send({ content: `Creating product: \`${productName}\`` });
const finalMsg = await interaction.user.send({ content: "Please provide a description for the product. Say `cancel` to exit." });
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.productCreationData[interaction.user.id] = {
name: productName,
step: 1,
hub: hubResult[0].id
};
global.dmHandlers[interaction.user.id] = createProdHandler;
} 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 }