UNPKG

@shadow-dev/core

Version:

A modular core framework for Discord bot development, providing commands, buttons, menus, middleware, and more.

61 lines (60 loc) 2.8 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Middleware = void 0; const node_path_1 = __importDefault(require("node:path")); const fs_1 = __importDefault(require("fs")); const glob_1 = require("glob"); const util_1 = require("../util"); class Middleware { // Public method to load all middleware static async loadAllMiddleware() { // Load middleware if not loaded yet if (this.commandSpecificMiddleware.size === 0) { await this.loadCommandMiddleware(); } return { commandMiddleware: this.commandSpecificMiddleware }; } static async loadCommandMiddleware() { const baseDir = process.cwd(); const isDev = fs_1.default.existsSync(node_path_1.default.join(baseDir, "src", "middleware", "command")); const commandMiddlewareFolder = isDev ? node_path_1.default.join(baseDir, "src", "middleware", "command") : node_path_1.default.join(baseDir, "dist", "middleware", "command"); const ext = isDev ? "ts" : "js"; const commandFiles = await (0, glob_1.glob)(`**/*.${ext}`, { cwd: commandMiddlewareFolder, absolute: true, }); // Handle global middleware await this.loadGlobalMiddleware(commandMiddlewareFolder, ext); // Process each middleware file await Promise.all(commandFiles.map(async (file) => { try { const middleware = await (0, util_1.importFile)(file); // Import the middleware instance directly if (middleware) { const commandName = node_path_1.default.basename(file, `.${ext}`); this.commandSpecificMiddleware.set(commandName, middleware); // Store the instance } } catch (err) { console.error(`❌ Error importing file: ${file}`, err); } })); } static async loadGlobalMiddleware(commandMiddlewareFolder, ext) { const globalMiddlewarePath = node_path_1.default.join(commandMiddlewareFolder, `global.${ext}`); // Check if the global middleware file exists if (fs_1.default.existsSync(globalMiddlewarePath)) { const globalMiddlewareInstance = await (0, util_1.importFile)(globalMiddlewarePath); // Import the instance directly if (globalMiddlewareInstance) { // Store the instance in the middleware map this.commandSpecificMiddleware.set("global", globalMiddlewareInstance); } } } } exports.Middleware = Middleware; Middleware.commandSpecificMiddleware = new Map();