@ad1m/djb
Version:
A streamlined library for creating Discord bots with Discord.js, featuring a simple command and event handler structure.
109 lines (105 loc) • 4.21 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/handlers/command.ts
var command_exports = {};
__export(command_exports, {
default: () => command_default
});
module.exports = __toCommonJS(command_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"));
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/command.ts
var import_cli_table3 = __toESM(require("cli-table3"));
var CommandHandler = async (client, appPath) => {
const commandsPath = import_node_path2.default.join(appPath, "commands");
const commands = await loadFiles(commandsPath);
const table = new import_cli_table3.default({ head: ["Group", "Name", "Status"] });
for (const command of commands) {
const { data, name, parent } = command;
if (!data?.config?.description) {
table.push([
parent,
`${name}`,
"\u274C Missing required 'description' config property"
]);
continue;
}
if (!data?.execute) {
table.push([
parent,
`${name}`,
"\u274C Missing required default execute function"
]);
continue;
}
client.commands?.set(name, data);
table.push([parent, `${name}`, "\u2705 Loaded"]);
}
console.log("\n=== Commands ===");
console.log(table.toString());
};
var command_default = CommandHandler;