From 048dbef11f95da56f5590a631c9a6757e42e9d52 Mon Sep 17 00:00:00 2001 From: ChrisChrome Date: Mon, 15 Sep 2025 10:55:53 -0600 Subject: [PATCH] Possibly add importing? --- commands.js | 4 ++++ commands/import.js | 53 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 commands/import.js diff --git a/commands.js b/commands.js index 91bece3..8f7bd9a 100644 --- a/commands.js +++ b/commands.js @@ -234,6 +234,10 @@ module.exports = { name: "settings", description: "View and manage hub settings", default_member_permissions: 0 + }, + { + name: "import", + description: "Import previousl Parcel purchases." } ], admin: [] diff --git a/commands/import.js b/commands/import.js new file mode 100644 index 0000000..227ab00 --- /dev/null +++ b/commands/import.js @@ -0,0 +1,53 @@ +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 +} \ No newline at end of file