const client = global.discord_client const pool = global.db_pool; const crypto = require('crypto'); const fs = require('fs'); const { pipeline } = require('stream'); const { promisify } = require('util'); const streamPipeline = promisify(pipeline); /** * Downloads a file from a URL to a specified destination. * @param {string} url - The URL to download the file from. * @param {string} dest - The destination file path to save the file. * @param {function} cb - Callback function called on completion or error. */ async function download(url, dest, cb) { try { const response = await fetch(url); if (!response.ok) { throw new Error(`Failed to fetch ${url}: ${response.statusText}`); } await streamPipeline(response.body, fs.createWriteStream(dest)); cb(null); // Signal success } catch (error) { cb(error); // Pass error to callback } } const cancel = (user) => { delete global.hubSettingsHandlers[user.id]; delete global.dmHandlers[user.id]; user.send('Closing hub settings.'); } const opts = { // Map of option number to step number 1: {step: 21, exec: () => { return 'Please provide the new short description. Say `cancel` to exit.'; }}, 2: {step: 22, exec: () => { return 'Please provide the new long description. Say `cancel` to exit.'; }}, 3: {step: 23, exec: async (hubId) => { const [hub] = await pool.query('SELECT allowGiftPurchase FROM hubs WHERE id = ?', [hubId]); if (!hub) return 'Error fetching hub data.'; const newValue = hub.allowGiftPurchase ? 0 : 1; await pool.query('UPDATE hubs SET allowGiftPurchase = ? WHERE id = ?', [newValue, hubId]); return `Allow Gift Purchase is now set to: ${newValue ? "Yes" : "No"}.\n\nType another option number, or \`cancel\` to exit.`; }}, 4: {step: 24, exec: () => { return 'Please provide the new Terms of Service. Say `cancel` to exit.'; }}, 5: {step: 25, exec: async (hubId) => { const newKey = crypto.randomBytes(16).toString('hex'); await pool.query('UPDATE hubs SET secretKey = ? WHERE id = ?', [newKey, hubId]); return `New secret key generated: ||${newKey}||\n\nType another option number, or \`cancel\` to exit.`; }}, 6: {step: 26, exec: () => { return 'Parcel Auto-Import setup is not yet implemented. Type another option number, or `cancel` to exit.'; }} } const execute = async (message) => { switch (global.hubSettingsHandlers[message.author.id].step) { case 1: // Main Menu if (message.content.toLowerCase() === 'cancel') { cancel(message.author); return; } const option = parseInt(message.content.trim()); if (!opts[option]) { message.channel.send('Invalid option. Please enter a valid option number or `cancel` to exit.'); return; } global.hubSettingsHandlers[message.author.id].step = opts[option].step; const response = await opts[option].exec(global.hubSettingsHandlers[message.author.id].hub); message.channel.send(response); break; case 21: // Edit Short Description const shortDesc = message.content.trim(); if (shortDesc.toLowerCase() === 'cancel') { cancel(message.author); return; } if (shortDesc.length > 256) { message.channel.send('Short description is too long. Please limit to 256 characters.'); return; } await pool.query('UPDATE hubs SET shortDescription = ? WHERE id = ?', [shortDesc, global.hubSettingsHandlers[message.author.id].hub]); global.hubSettingsHandlers[message.author.id].step = 1; message.channel.send('Short description updated.\n\nType an option number, or `cancel` to exit.'); break; case 22: // Edit Long Description const longDesc = message.content.trim(); if (longDesc.toLowerCase() === 'cancel') { cancel(message.author); return; } if (longDesc.length > 5000) { message.channel.send('Long description is too long. Please limit to 5000 characters.'); return; } await pool.query('UPDATE hubs SET longDescription = ? WHERE id = ?', [longDesc, global.hubSettingsHandlers[message.author.id].hub]); global.hubSettingsHandlers[message.author.id].step = 1; message.channel.send('Long description updated.\n\nType an option number, or `cancel` to exit.'); break; case 24: // Edit Terms of Service const tos = message.content.trim(); if (tos.toLowerCase() === 'cancel') { cancel(message.author); return; } if (tos.length > 10000) { message.channel.send('Terms of Service is too long. Please limit to 10000 characters.'); return; } await pool.query('UPDATE hubs SET tos = ? WHERE id = ?', [tos, global.hubSettingsHandlers[message.author.id].hub]); global.hubSettingsHandlers[message.author.id].step = 1; message.channel.send('Terms of Service updated.\n\nType an option number, or `cancel` to exit.'); break; default: message.channel.send('Invalid step.'); log.error(`Invalid hub settings step for user ${message.author.id}: ${global.hubSettingsHandlers[message.author.id].step}`); cancel(message.author); break; } } module.exports = execute