// bot.js - Discord bot for moderation and utilities // Copyright (C) 2025 Luis Bauza // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. require('dotenv').config(); const fs = require('node:fs'); const path = require('node:path'); const { Client, Collection, Events, GatewayIntentBits } = require('discord.js'); const Logger = require('./logger'); const logger = new Logger('bot'); const client = new Client({ intents: [ GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent, ], }); client.commands = new Collection(); const commandsPath = path.join(__dirname, 'commands'); const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js')); await Promise.all( commandFiles.map(async file => { const filePath = path.join(commandsPath, file); const command = await import(filePath); if ('data' in command && 'execute' in command) { client.commands.set(command.data.name, command); } else { logger.warn( `[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`, ); } }), ); client.once(Events.ClientReady, () => { logger.log(`Ready! Logged in as ${client.user.tag}`); }); client.on(Events.InteractionCreate, async interaction => { if (!interaction.isChatInputCommand()) return; const command = client.commands.get(interaction.commandName); if (!command) { logger.error(`No command matching ${interaction.commandName} was found.`); return; } try { await command.execute(interaction); } catch (error) { logger.error(error); if (interaction.replied || interaction.deferred) { await interaction.followUp({ content: 'There was an error while executing this command!', ephemeral: true, }); } else { await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true, }); } } }); client.login(process.env.DISCORD_TOKEN);