const { SlashCommandBuilder, ActionRowBuilder, ChannelSelectMenuBuilder, StringSelectMenuBuilder, ModalBuilder, TextInputBuilder, TextInputStyle, ComponentType } = require('discord.js'); const api = require('../api'); const { readDb, writeDb } = require('../../backend/db.js'); const defaultWelcomeMessages = ["Welcome to the server, {user}!", "Hey {user}, welcome!", "{user} has joined the party!"]; module.exports = { name: 'setup-welcome', description: 'Interactively set up the welcome message for this server.', enabled: true, builder: new SlashCommandBuilder() .setName('setup-welcome') .setDescription('Interactively set up the welcome message for this server.'), async execute(interaction) { const db = readDb(); const guildId = interaction.guildId; if (!db[guildId]) { db[guildId] = {}; } const channelSelect = new ChannelSelectMenuBuilder() .setCustomId('welcome_channel_select') .setPlaceholder('Select the channel for welcome messages.') .setChannelTypes([0]); // Text channels const row1 = new ActionRowBuilder().addComponents(channelSelect); const channelReply = await interaction.reply({ content: 'Please select the channel where you want welcome messages to be sent.', components: [row1], flags: 64, }); try { const channelConfirmation = await channelReply.awaitMessageComponent({ componentType: ComponentType.ChannelSelect, time: 60000, }); const channelId = channelConfirmation.values[0]; // persist via backend try { const existing = (await api.getServerSettings(guildId)) || {}; existing.welcomeEnabled = true; existing.welcomeChannel = channelId; await api.upsertServerSettings(guildId, existing); } catch (e) { console.error('Error persisting welcome settings to backend, falling back to local:', e); const db = readDb(); if (!db[guildId]) db[guildId] = {}; db[guildId].welcomeChannel = channelId; db[guildId].welcomeEnabled = true; writeDb(db); } const messageOptions = defaultWelcomeMessages.map(msg => ({ label: msg.length > 100 ? msg.substring(0, 97) + '...' : msg, value: msg, })); messageOptions.push({ label: 'Custom Message', value: 'custom', }); const messageSelect = new StringSelectMenuBuilder() .setCustomId('welcome_message_select') .setPlaceholder('Select a welcome message or create your own.') .addOptions(messageOptions); const row2 = new ActionRowBuilder().addComponents(messageSelect); await channelConfirmation.update({ content: `Channel <#${channelId}> selected. Now, please select a welcome message.`, components: [row2], }); const messageConfirmation = await channelReply.awaitMessageComponent({ componentType: ComponentType.StringSelect, time: 60000, }); const selectedMessage = messageConfirmation.values[0]; if (selectedMessage === 'custom') { const modal = new ModalBuilder() .setCustomId('welcome_custom_message_modal') .setTitle('Custom Welcome Message'); const messageInput = new TextInputBuilder() .setCustomId('custom_message_input') .setLabel("Your custom message") .setPlaceholder('Use {user} for username and {server} for server name.') .setStyle(TextInputStyle.Paragraph) .setRequired(true); const firstActionRow = new ActionRowBuilder().addComponents(messageInput); modal.addComponents(firstActionRow); await messageConfirmation.showModal(modal); const modalSubmit = await interaction.awaitModalSubmit({ time: 300000, }); const customMessage = modalSubmit.fields.getTextInputValue('custom_message_input'); try { const existing = (await api.getServerSettings(guildId)) || {}; existing.welcomeMessage = customMessage; await api.upsertServerSettings(guildId, existing); } catch (e) { console.error('Error persisting welcome message to backend, falling back to local:', e); const db = readDb(); if (!db[guildId]) db[guildId] = {}; db[guildId].welcomeMessage = customMessage; writeDb(db); } await modalSubmit.reply({ content: `Welcome message setup complete! Channel: <#${channelId}>, Message: "${customMessage}"`, flags: 64, }); } else { try { const existing = (await api.getServerSettings(guildId)) || {}; existing.welcomeMessage = selectedMessage; await api.upsertServerSettings(guildId, existing); } catch (e) { console.error('Error persisting welcome message to backend, falling back to local:', e); const db = readDb(); if (!db[guildId]) db[guildId] = {}; db[guildId].welcomeMessage = selectedMessage; writeDb(db); } await messageConfirmation.update({ content: `Welcome message setup complete! Channel: <#${channelId}>, Message: "${selectedMessage}"`, components: [], }); } } catch (error) { console.error('Error during welcome setup:', error); await interaction.editReply({ content: 'Configuration timed out or an error occurred.', components: [] }); } }, };