NotParcel/commands/setup.js

46 lines
2.5 KiB
JavaScript

const client = global.discord_client
const pool = global.db_pool;
// /setup command to initialize a hub in the server
/* Hub table schema
CREATE TABLE IF NOT EXISTS hubs (
id CHAR(36) PRIMARY KEY DEFAULT UUID(),
ownerId BIGINT, -- Owner of the hub (Discord ID)
discordGuild BIGINT, -- Discord Guild ID
name VARCHAR(128), -- Name of the hub
shortDescription VARCHAR(256), -- Short description of the hub
longDescription TEXT, -- Long description of the hub
allowGiftPurchase BOOLEAN DEFAULT TRUE, -- Allow users to buy gifts for others on this hub.
tos TEXT DEFAULT 'This Hub does not have any Terms of Service yet. If you are the Hub owner, you can update this under settings.', -- Terms of Service
bgmId BIGINT DEFAULT NULL, -- Background Music ID
secretKey VARCHAR(128) UNIQUE NOT NULL -- Secret key for the hub to authenticate
)
*/
const crypto = require('crypto');
const log = global.log;
const execute = async (interaction) => {
if (!interaction.guildId) return interaction.reply({ content: "This command can only be used in a server", ephemeral: true });
try {
const guildId = interaction.guildId;
const [existingHub] = await pool.query('SELECT * FROM hubs WHERE discordGuild = ?', [guildId]);
if (existingHub) return interaction.reply({ content: "This server already has a hub set up.", ephemeral: true });
const ownerId = interaction.user.id;
const name = interaction.guild.name;
const shortDescription = "Our super cool product hub!";
const longDescription = "Welcome to our super cool product hub! We offer a variety of awesome products for you to explore and purchase. Enjoy your stay!";
const result = await pool.query('INSERT INTO hubs (ownerId, discordGuild, name, shortDescription, longDescription, secretKey) VALUES (?, ?, ?, ?, ?, ?)', [ownerId, guildId, name, shortDescription, longDescription, await crypto.randomBytes(64).toString('hex')]);
const [hub] = await pool.query('SELECT * FROM hubs WHERE discordGuild = ?', [guildId]);
if (result.affectedRows === 1) {
return interaction.reply({ content: `Hub successfully created with ID: \`${hub.id}\`. Get details via the /settings command!`, ephemeral: true });
} else {
return interaction.reply({ content: "An error occurred while creating the hub. Please try again later.", ephemeral: true });
}
} catch (error) {
log.error(error.stack);
return interaction.reply({ content: "An error occurred while creating the hub. Please try again later.", ephemeral: true });
}
}
module.exports = { execute }