UNPKG

@ad1m/djb

Version:

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

143 lines (137 loc) 5.19 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/handlers/event.ts var event_exports = {}; __export(event_exports, { default: () => event_default }); module.exports = __toCommonJS(event_exports); var import_node_path2 = __toESM(require("path")); // src/utils/handler.ts var import_node_fs = __toESM(require("fs")); var import_node_path = __toESM(require("path")); // src/utils/url.ts var import_url = require("url"); var import_path = require("path"); var import_meta = {}; 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 (0, import_path.dirname)((0, import_url.fileURLToPath)(import_meta.url)); } }; // src/utils/handler.ts var import_node_url = require("url"); var EXT_PRIORITY = ["js", "mjs", "cjs"]; var loadFiles = async (dirPath) => { if (!import_node_fs.default.existsSync(dirPath)) return []; const entries = import_node_fs.default.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 = import_node_path.default.join(entry.parentPath, entry.name); const fileData = await import((0, import_node_url.pathToFileURL)(entryPath).href); const resolvedFiledata = fileData.default?.default ? fileData.default : fileData; const file = { parent: import_node_path.default.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/event.ts var import_cli_table3 = __toESM(require("cli-table3")); var EventHandler = async (client, appPath) => { const internalEventsPath = import_node_path2.default.join(getCurrenDir(), "..", "core-events"); const eventsPath = import_node_path2.default.join(appPath, "events"); const internalEvents = await loadFiles(internalEventsPath); const events = await loadFiles(eventsPath); const table = new import_cli_table3.default({ head: ["Group", "Name", "Event", "Status"] }); for (const event of [...internalEvents, ...events]) { const { data, name, parent } = event; if (!data?.config?.name) { table.push([ parent, `${name}`, "?", "\u274C Missing required 'name' config property" ]); continue; } if (!data?.execute) { table.push([ parent, `${name}`, data.config.name, "\u274C Missing required default execute function" ]); continue; } if (data.config.once) client.once(data.config.name, (...args) => data.execute(client, ...args)); else client.on(data.config.name, (...args) => data.execute(client, ...args)); table.push([parent, `${name}`, data.config.name, "\u2705 Loaded"]); } console.log("\n=== Events ==="); console.log(table.toString()); }; var event_default = EventHandler;