@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.3 kB
JavaScript
// src/handlers/button.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/button.ts
import CliTable3 from "cli-table3";
var ButtonHandler = async (client, appPath) => {
const buttonsPath = path2.join(appPath, "buttons");
const buttons = await loadFiles(buttonsPath);
const table = new CliTable3({ head: ["Group", "Name", "Status"] });
for (const button of buttons) {
const { data, name, parent } = button;
if (!data?.execute) {
table.push([
parent,
`${name}`,
"\u274C Missing required default execute function"
]);
continue;
}
client.buttons?.set(name, data);
table.push([parent, `${name}`, "\u2705 Loaded"]);
}
console.log("\n=== Buttons ===");
console.log(table.toString());
};
var button_default = ButtonHandler;
export {
button_default as default
};