const { Client, GatewayIntentBits, Collection, Partials, InteractionType, ModalBuilder, TextInputBuilder, TextInputStyle, ActionRowBuilder, EmbedBuilder } = require('discord.js'); const fs = require('fs'); const path = require('path'); const config = require('./config.json'); const client = new Client({ intents: [GatewayIntentBits.Guilds], partials: [Partials.Channel] }); client.commands = new Collection(); // Load command files const commandsPath = path.join(__dirname, 'commands'); const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js')); // Set up commands in the Collection for (const file of commandFiles) { const command = require(`./commands/${file}`); client.commands.set(command.data.name, command); } client.once('ready', async () => { console.log(`Logged in as ${client.user.tag}!`); // Register commands directly to a specific guild const guild = client.guilds.cache.get(config.guildId); if (guild) { await guild.commands.set(client.commands.map(cmd => cmd.data)); console.log('Slash commands registered.'); } else { console.error('Guild not found.'); } }); // Interaction handling for commands and buttons client.on('interactionCreate', async interaction => { if (interaction.isCommand()) { const command = client.commands.get(interaction.commandName); if (!command) return; try { await command.execute(interaction); } catch (error) { console.error(error); // Only reply if the interaction hasn't already been replied to or deferred if (!interaction.replied && !interaction.deferred) { await interaction.reply({ content: 'There was an error executing this command.', ephemeral: true }); } } } // Handle button interactions if (interaction.isButton()) { try { if (interaction.customId === 'attend') { // Show the GitHub repository modal const modal = new ModalBuilder() .setCustomId('github_modal') .setTitle('GitHub Repository'); const githubInput = new TextInputBuilder() .setCustomId('githubLink') .setLabel('Link your GitHub repository code here!') .setStyle(TextInputStyle.Short) .setRequired(true); const modalRow = new ActionRowBuilder().addComponents(githubInput); modal.addComponents(modalRow); await interaction.showModal(modal); } else if (interaction.customId === 'unattend') { const embed = new EmbedBuilder() .setColor(0xFF0000) .setTitle('Withdrawal') .setDescription(`${interaction.user} has withdrawn from the contest.`) .setTimestamp(); const resultChannel = interaction.client.channels.cache.get(config.resultChannelId); await resultChannel.send({ embeds: [embed] }); // Update response instead of replying again if (!interaction.replied && !interaction.deferred) { await interaction.reply({ content: 'You have successfully withdrawn.', ephemeral: true }); } } } catch (error) { console.error("Error handling button interaction:", error); } } // Handle modal submissions if (interaction.type === InteractionType.ModalSubmit) { try { if (interaction.customId === 'github_modal') { const githubLink = interaction.fields.getTextInputValue('githubLink'); const embed = new EmbedBuilder() .setColor(0x00FF00) .setTitle('GitHub Submission') .setDescription(`${interaction.user} submitted the following GitHub repository:`) .addFields({ name: 'Repository Link', value: githubLink }) .setTimestamp(); const resultChannel = interaction.client.channels.cache.get(config.resultChannelId); await resultChannel.send({ embeds: [embed] }); // Confirm submission in response if (!interaction.replied && !interaction.deferred) { await interaction.reply({ content: 'Your submission has been received!', ephemeral: true }); } } } catch (error) { console.error("Error handling modal submission:", error); } } }); // Login to Discord client.login(config.token);