const { Client, GatewayIntentBits, Collection } = require('discord.js'); const fs = require('fs'); const path = require('path'); const deployCommands = require('./deploy-commands'); const { readDb } = require('../backend/db'); const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers] }); client.commands = new Collection(); const commandHandler = require('./handlers/command-handler'); const eventHandler = require('./handlers/event-handler'); commandHandler(client); eventHandler(client); client.on('interactionCreate', async interaction => { if (!interaction.isCommand()) return; const command = client.commands.get(interaction.commandName); if (!command) return; // Check per-guild toggles try { const db = readDb(); const guildSettings = db[interaction.guildId] || {}; const toggles = guildSettings.commandToggles || {}; const protectedCommands = ['manage-commands', 'help']; // If command is protected, always allow if (!protectedCommands.includes(command.name)) { if (toggles[command.name] === false) { await interaction.reply({ content: 'This command has been disabled on this server.', flags: 64 }); return; } // If the module-level enabled flag is false, treat as disabled too if (command.enabled === false) { await interaction.reply({ content: 'This command is currently disabled globally.', flags: 64 }); return; } } try { await command.execute(interaction); } catch (error) { console.error(error); await interaction.reply({ content: 'There was an error while executing this command!', flags: 64 }); } } catch (error) { console.error('Error checking command toggles:', error); await interaction.reply({ content: 'Internal error occurred.', flags: 64 }); } }); client.on('guildCreate', guild => { deployCommands(guild.id); }); const login = () => { client.login(process.env.DISCORD_BOT_TOKEN); } module.exports = { login, client };