53 lines
2.8 KiB
JavaScript
53 lines
2.8 KiB
JavaScript
const client = global.discord_client
|
|
const pool = global.db_pool;
|
|
|
|
const execute = async (interaction) => {
|
|
// Check if in guild
|
|
if (!interaction.guildId) return interaction.reply({ content: "This command can only be used in a server", ephemeral: true });
|
|
// Check if guild has a hub
|
|
const guildId = interaction.guildId;
|
|
const [hub] = await pool.query('SELECT * FROM hubs WHERE discordGuild = ?', [guildId]);
|
|
if (!hub) return interaction.reply({ content: "This guild does not have a hub set up!", ephemeral: true });
|
|
// Check if user is linked
|
|
const [user] = await pool.query('SELECT * FROM users WHERE discordId = ?', [interaction.user.id]);
|
|
if (!user) return interaction.reply({ content: "You must link your account first using `/link`. Head to the hub to get your code!", ephemeral: true });
|
|
// Check if hub has parcel key set
|
|
if (!hub.parcelKey) return interaction.reply({ content: "This hub does not have Parcel integration set up! Please contact the hub owner.", ephemeral: true });
|
|
|
|
const sessionData = await fetch(`https://hub.parcelroblox.com/getSession?robloxPlayerId=${user.robloxId}`, {
|
|
headers: {
|
|
'Authorization': `${hub.parcelKey}`
|
|
}
|
|
})
|
|
|
|
if (!sessionData.ok) {
|
|
console.error("Failed to fetch session data:", await sessionData.text());
|
|
return interaction.reply({ content: "Failed to connect to Parcel. Please contact the hub owner.", ephemeral: true });
|
|
}
|
|
|
|
const sessionJson = await sessionData.json();
|
|
if (sessionJson.status !== "200") {
|
|
console.error("Parcel API returned error:", sessionJson);
|
|
return interaction.reply({ content: "Failed to connect to Parcel. Please contact the hub owner.", ephemeral: true });
|
|
}
|
|
const parcelProducts = sessionJson.data.productsData.allProducts;
|
|
let count = 0;
|
|
for (const prod of parcelProducts) {
|
|
if (!prod.playerData.ownsProduct) continue; // Only import owned products
|
|
// Check if the product devproduct_id exists in our database
|
|
const [existingProduct] = await pool.query('SELECT * FROM products WHERE devProductId = ? AND hubId = ?', [prod.devproduct_id, hub.id]);
|
|
if (!existingProduct) continue; // No matching product, skip
|
|
// Product exists, check if theres already a purchase record
|
|
const [existingPurchase] = await pool.query('SELECT * FROM purchases WHERE userId = ? AND productId = ?', [user.id, existingProduct.id]);
|
|
if (existingPurchase) continue; // Already have a purchase record, skip
|
|
// Create purchase record
|
|
await pool.query('INSERT INTO purchases (userId, productId, hubId) VALUES (?, ?, ?)', [user.id, existingProduct.id, hub.id]);
|
|
console.log(`Imported purchase of product ${prod.name} > ${existingProduct.name} for user ${user.robloxId}`);
|
|
count++;
|
|
}
|
|
return interaction.reply({ content: `Import complete! Imported ${count} products.`, ephemeral: true });
|
|
|
|
};
|
|
module.exports = {
|
|
execute
|
|
} |