import { DebugBuilder } from "../modules/debugger.mjs"; import { Client, GatewayIntentBits, Collection } from 'discord.js'; import { registerActiveCommands, unregisterAllCommands } from './modules/registerCommands.mjs' import { RSSController } from '../rss-manager/rssController.mjs' import { join, dirname } from 'path'; import { readdirSync } from 'fs'; import { fileURLToPath } from 'url'; import dotenv from 'dotenv'; dotenv.config() const log = new DebugBuilder("server", "discordBot"); const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); /** * Add the enabled commands to the bot to be used by users in discord * (commands that end in '.mjs' will be enabled, to disable just remove the extension or replace with '.mjs.disabled') * @param {any} serverClient * @param {any} _commandsPath="./commands" * @returns {any} */ export const addEnabledCommands = async (serverClient, _commandsPath = "./commands") => { // Setup commands for the Discord bot serverClient.commands = new Collection(); const commandsPath = join(__dirname, _commandsPath); const commandFiles = readdirSync(commandsPath).filter(file => file.endsWith('.mjs')); for (const file of commandFiles) { const filePath = await join(commandsPath, file); log.INFO(`Adding enabled command: ${filePath}`); await import(`file://${filePath}`).then(command => { if (command.data instanceof Promise) { command.data.then(async (builder) => { command.data = builder; log.DEBUG("Importing command: ", command.data.name, command); // Set a new item in the Collection // With the key as the command name and the value as the exported module serverClient.commands.set(command.data.name, command); }); } else { log.DEBUG("Importing command: ", command.data.name, command); // Set a new item in the Collection // With the key as the command name and the value as the exported module serverClient.commands.set(command.data.name, command); } }) } // Register the commands currently in use by the bot await registerActiveCommands(serverClient); } /** * Add the enabled event listeners to the bot * (events that end in '.mjs' will be enabled, to disable just remove the extension or replace with '.mjs.disabled') * @param {any} serverClient * @param {any} _eventsPath="./events" * @returns {any} */ export function addEnabledEventListeners(serverClient, _eventsPath = "./events") { const eventsPath = join(__dirname, _eventsPath); const eventFiles = readdirSync(eventsPath).filter(file => file.endsWith('.mjs')); for (const file of eventFiles) { const filePath = join(eventsPath, file); log.INFO(`Adding enabled event listener: ${filePath}`); import(`file://${filePath}`).then(event => { log.DEBUG("Adding event: ", event); if (event.once) { serverClient.once(event.name, (...args) => event.execute(serverClient.nodeIo, ...args)); } else { serverClient.on(event.name, (...args) => event.execute(serverClient.nodeIo, ...args)); } }) } } // The discord client export const serverClient = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildVoiceStates] }); // Run when the bot is ready serverClient.on('ready', async () => { log.INFO(`Logged in as ${serverClient.user.tag}!`); // Add and register commands await addEnabledCommands(serverClient); // Config the discord bot with events await addEnabledEventListeners(serverClient); // Start the RSS Controller serverClient.RSSController = await new RSSController(serverClient); serverClient.RSSController.start(); log.INFO("RSS Controller:", serverClient.RSSController); }); // Startup the discord bot log.INFO(`Logging into discord with ID: ${process.env.DISCORD_TOKEN}`); serverClient.login(process.env.DISCORD_TOKEN);