NotParcel/commands/revoke.js

58 lines
2.6 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 });
// 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("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 ? AND hubId = ?', [`%${productName.toUpperCase()}%`, hub.id]);
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]);
if (hub.logChannel != null) {
try {
chan = await client.channels.fetch(hub.logChannel);
chan.send({
embeds: [
{
title: `Product Revoked`,
color: 0xff0000,
description: `**Roblox ID:** ${user.robloxId}\n**Discord User:** <@${user.discordId}>\n**Product:** ${product.name}`
}
]
})
} catch (error) {
// Do nothing, channel was deleted
}
}
return interaction.reply({ content: `Removed \`${product.name}\` from ${robloxID}`, ephemeral: true });
};
module.exports = { execute }