UNPKG

@ad1m/djb

Version:

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

70 lines (67 loc) 2.28 kB
// src/handlers/modal.ts import path2 from "node:path"; // src/utils/handler.ts import fs from "node:fs"; import path from "node:path"; import { pathToFileURL } from "node:url"; var EXT_PRIORITY = ["js", "mjs", "cjs"]; 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(); }; // src/handlers/modal.ts import CliTable3 from "cli-table3"; var ModalHandler = async (client, appPath) => { const modalsPath = path2.join(appPath, "modals"); const modals = await loadFiles(modalsPath); const table = new CliTable3({ head: ["Group", "Name", "Status"] }); for (const modal of modals) { const { data, name, parent } = modal; if (!data?.execute) { table.push([ parent, `${name}`, "\u274C Missing required default execute function" ]); continue; } client.modals?.set(name, data); table.push([parent, `${name}`, "\u2705 Loaded"]); } console.log("\n=== Modals ==="); console.log(table.toString()); }; var modal_default = ModalHandler; export { modal_default as default };