UNPKG

@ad1m/djb

Version:

A streamlined library for creating Discord bots with Discord.js, featuring a simple command and event handler structure.

110 lines (107 loc) 3.83 kB
// src/utils/handler.ts import fs from "node:fs"; import path from "node:path"; // src/utils/url.ts import { fileURLToPath } from "url"; import { dirname, resolve } from "path"; import { existsSync } from "fs"; var getCurrentModuleType = () => { if (typeof __dirname !== "undefined") { return "cjs"; } else if (typeof import.meta?.url !== "undefined") { return "esm"; } else { throw new Error("Unable to determine module type."); } }; var getCurrenDir = () => { const mType = getCurrentModuleType(); switch (mType) { case "cjs": return __dirname; case "esm": return dirname(fileURLToPath(import.meta.url)); } }; var getAppPath = () => { const distAppPath = resolve(process.cwd(), "dist", "app"); const appPath = resolve(process.cwd(), "app"); if (existsSync(distAppPath)) return distAppPath; else if (existsSync(appPath)) return appPath; else throw new Error("No /app directory found."); }; // src/utils/handler.ts import { pathToFileURL } from "node:url"; var EXT_PRIORITY = ["js", "mjs", "cjs"]; var getClientConfig = async () => { const dirPath = path.join(getAppPath(), ".."); if (!fs.existsSync(dirPath)) return; const configFile = fs.readdirSync(dirPath, { withFileTypes: true }).filter((entry) => entry.isFile()).find((entry) => { return EXT_PRIORITY.some((ext) => entry.name.endsWith(`.${ext}`)); }); if (!configFile) throw new Error( `No config file found, please mrovide a valid config.js file. Valid extentions: ${EXT_PRIORITY.join(",")}` ); const configPath = path.join(dirPath, configFile.name); const configFiledata = await import(pathToFileURL(configPath).href); return configFiledata?.config; }; var loadFiles = async (dirPath) => { if (!fs.existsSync(dirPath)) return []; const entries = fs.readdirSync(dirPath, { withFileTypes: true, recursive: true }); const fileMap = /* @__PURE__ */ new Map(); for (const entry of entries) { if (entry.isDirectory()) continue; const match = entry.name.match(/^(.+)\.([^.]+)$/); if (!match) continue; const [, baseName, ext] = match; if (!EXT_PRIORITY.includes(ext)) continue; const existingEntry = fileMap.get(baseName); if (!existingEntry || EXT_PRIORITY.indexOf(ext) < EXT_PRIORITY.indexOf(existingEntry.name.split(".").pop())) { const entryPath = path.join(entry.parentPath, entry.name); const fileData = await import(pathToFileURL(entryPath).href); const resolvedFiledata = fileData.default?.default ? fileData.default : fileData; const file = { parent: path.basename(dirPath), name: entry.name.replace( new RegExp(`\\.(${EXT_PRIORITY.join("|")})$`), "" ), data: { config: resolvedFiledata?.config, execute: resolvedFiledata?.default && typeof resolvedFiledata.default === "function" ? resolvedFiledata.default : void 0 } }; fileMap.set(baseName, file); } } return fileMap.values(); }; var initHandlers = async (client) => { console.log("curr", getCurrenDir()); const handlersPath = path.join(getCurrenDir(), "handlers"); if (!fs.existsSync(handlersPath)) { return []; } const handlerFiles = fs.readdirSync(handlersPath).filter((v) => v.endsWith(".js")); for (const handlerFile of handlerFiles) { const filePath = path.join(handlersPath, handlerFile); const handlerFileData = await import(pathToFileURL(filePath).href); const handlerFunction = handlerFileData.default?.default ?? handlerFileData.default; if (!handlerFunction || typeof handlerFunction !== "function") throw new Error( `${handlerFile} missing required default execute function.` ); await handlerFunction(client, getAppPath()); } }; export { getClientConfig, initHandlers, loadFiles };