import { join, dirname } from 'path'; import { readdirSync } from 'fs'; import { fileURLToPath } from 'url'; import { Collection } from 'discord.js'; 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} nodeIo * @param {any} _commandsPath="./commands" * @returns {any} */ export function addEnabledCommands(serverClient, nodeIo, _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 = join(commandsPath, file); console.log(`Adding enabled command: ${filePath}`); import(`file://${filePath}`).then(command => { if (command.data instanceof Promise) { command.data.then(async (builder) => { command.data = builder; console.log("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 { console.log("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); } }) } } /** * 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} nodeIo * @param {any} _eventsPath="./events" * @returns {any} */ export function addEnabledEventListeners(serverClient, nodeIo, _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); console.log(`Adding enabled event listener: ${filePath}`); import(`file://${filePath}`).then(event => { console.log("Adding event: ", event); if (event.once) { serverClient.once(event.name, (...args) => event.execute(nodeIo, ...args)); } else { serverClient.on(event.name, (...args) => event.execute(nodeIo, ...args)); } }) } }