@ad1m/djb
Version:
A streamlined library for creating Discord bots with Discord.js, featuring a simple command and event handler structure.
148 lines (144 loc) • 5.97 kB
JavaScript
;
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/utils/handler.ts
var handler_exports = {};
__export(handler_exports, {
getClientConfig: () => getClientConfig,
initHandlers: () => initHandlers,
loadFiles: () => loadFiles
});
module.exports = __toCommonJS(handler_exports);
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_fs = require("fs");
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));
}
};
var getAppPath = () => {
const distAppPath = (0, import_path.resolve)(process.cwd(), "dist", "app");
const appPath = (0, import_path.resolve)(process.cwd(), "app");
if ((0, import_fs.existsSync)(distAppPath)) return distAppPath;
else if ((0, import_fs.existsSync)(appPath)) return appPath;
else throw new Error("No /app directory found.");
};
// src/utils/handler.ts
var import_node_url = require("url");
var EXT_PRIORITY = ["js", "mjs", "cjs"];
var getClientConfig = async () => {
const dirPath = import_node_path.default.join(getAppPath(), "..");
if (!import_node_fs.default.existsSync(dirPath)) return;
const configFile = import_node_fs.default.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 = import_node_path.default.join(dirPath, configFile.name);
const configFiledata = await import((0, import_node_url.pathToFileURL)(configPath).href);
return configFiledata?.config;
};
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();
};
var initHandlers = async (client) => {
console.log("curr", getCurrenDir());
const handlersPath = import_node_path.default.join(getCurrenDir(), "handlers");
if (!import_node_fs.default.existsSync(handlersPath)) {
return [];
}
const handlerFiles = import_node_fs.default.readdirSync(handlersPath).filter((v) => v.endsWith(".js"));
for (const handlerFile of handlerFiles) {
const filePath = import_node_path.default.join(handlersPath, handlerFile);
const handlerFileData = await import((0, import_node_url.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());
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
getClientConfig,
initHandlers,
loadFiles
});