36 lines
2 KiB
JavaScript
36 lines
2 KiB
JavaScript
const client = global.discord_client
|
|
const pool = global.db_pool;
|
|
|
|
const execute = async (interaction) => {
|
|
var robloxID = interaction.options.getNumber("roblox-id") || null;
|
|
const discordID = interaction.options.getUser("discord-id")?.id || null;
|
|
if (!robloxID && !discordID) return interaction.reply({ content: "You must provide a Roblox ID or Discord ID", ephemeral: true });
|
|
|
|
// If both robloxID and discordID are provided, we can't continue
|
|
if (robloxID && discordID) return interaction.reply({ content: "You can't provide both a Roblox ID and Discord ID", ephemeral: true });
|
|
|
|
// If discordID is provided, we need to find the robloxID
|
|
if (discordID) {
|
|
const [row] = await pool.query('SELECT * FROM users WHERE discordId = ?', [discordID]);
|
|
if (!row) return interaction.reply({ content: "User not found", ephemeral: true });
|
|
robloxID = row.robloxId;
|
|
}
|
|
|
|
// Check if the user exists
|
|
const [user] = await pool.query('SELECT * FROM users WHERE robloxId = ?', [robloxID]);
|
|
if (!user) return interaction.reply({ content: "User not found", ephemeral: true });
|
|
|
|
const productName = interaction.options.getString("product-name");
|
|
// try catch try and find the product based on partial product name, parse everything in uppercase to make things easier
|
|
const [product] = await pool.query('SELECT * FROM products WHERE UPPER(name) LIKE ?', [`%${productName.toUpperCase()}%`]);
|
|
if (!product) return interaction.reply({ content: "Product not found", ephemeral: true });
|
|
// Check if the user already owns the product
|
|
const [purchase] = await pool.query('SELECT * FROM purchases WHERE robloxId = ? AND productId = ?', [robloxID, product.id]);
|
|
if (!purchase) return interaction.reply({ content: "User doesn't own product", ephemeral: true });
|
|
|
|
// Remove purchase from database
|
|
await pool.query('DELETE FROM purchases WHERE robloxId = ? AND productId = ?', [robloxID, product.id]);
|
|
return interaction.reply({ content: `Removed \`${product.name}\` from ${robloxID}`, ephemeral: true });
|
|
};
|
|
|
|
module.exports = { execute }
|