UNPKG

@ad1m/djb

Version:

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

176 lines (164 loc) 6.46 kB
#!/usr/bin/env node "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 __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 )); // package.json var version = "1.3.5"; var description = "A streamlined library for creating Discord bots with Discord.js, featuring a simple command and event handler structure."; // src/bin/djb.ts var import_commander = require("commander"); // src/djb.ts var import_discord = require("discord.js"); var import_config = require("dotenv/config"); // src/lib/mongo.ts var import_mongoose = __toESM(require("mongoose")); var import_logger = require("@ad1m/logger"); var connectToDb = async (uri) => { try { const c = await import_mongoose.default.connect(uri, { connectTimeoutMS: 5e4 }); import_logger.log.success( "mongodb", `Connected to database '${c.connection.name}' successfully.` ); } catch (error) { import_logger.log.error("mongodb", error); } }; // 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_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 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()); } }; // src/djb.ts var DJBClient = class extends import_discord.Client { constructor(options, djbOptions) { super(options); this.cache = new import_discord.Collection(); this.commands = new import_discord.Collection(); this.selectMenus = new import_discord.Collection(); this.modals = new import_discord.Collection(); this.buttons = new import_discord.Collection(); this.djbOptions = djbOptions; } async setupClient() { this.config = await getClientConfig(); initHandlers(this); } setupDatabase() { if (!process.env.DJB_MONGODB_URI) { throw new Error( "DJB_MONGODB_URI env must be provided when mongoDb is true" ); } connectToDb(process.env.DJB_MONGODB_URI ?? ""); } start() { if (this.djbOptions?.mongoDb) this.setupDatabase(); this.setupClient(); if (!process.env.DJB_TOKEN) { throw new Error("DJB_TOKEN env must be provided"); } this.login(process.env.DJB_TOKEN); } }; // src/cli/djb-start.ts var import_logger2 = require("@ad1m/logger"); var djbStart = () => { import_logger2.log.info("djb", "Starting bot..."); const client = new DJBClient({ intents: [] }, { mongoDb: true }); client.start(); }; // src/cli/djb-create.ts var djbCreate = (projectName) => { }; // src/bin/djb.ts import_commander.program.version(version).description(description); import_commander.program.command("start").description("Start the bot app").action(djbStart); import_commander.program.command("create [project-name]").description("Create a new DJB project").action((projectName) => { djbCreate(projectName); }); import_commander.program.parse(process.argv);