UNPKG

@ad1m/djb

Version:

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

139 lines (131 loc) 4.14 kB
// src/djb.ts import { Client as BaseClient, Collection } from "discord.js"; import "dotenv/config"; // src/lib/mongo.ts import mongoose from "mongoose"; import { log } from "@ad1m/logger"; var connectToDb = async (uri) => { try { const c = await mongoose.connect(uri, { connectTimeoutMS: 5e4 }); log.success( "mongodb", `Connected to database '${c.connection.name}' successfully.` ); } catch (error) { log.error("mongodb", error); } }; // src/utils/handler.ts import fs from "node:fs"; import path from "node:path"; // src/utils/url.ts import { fileURLToPath } from "url"; import { dirname, resolve } from "path"; import { existsSync } from "fs"; 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 dirname(fileURLToPath(import.meta.url)); } }; var getAppPath = () => { const distAppPath = resolve(process.cwd(), "dist", "app"); const appPath = resolve(process.cwd(), "app"); if (existsSync(distAppPath)) return distAppPath; else if (existsSync(appPath)) return appPath; else throw new Error("No /app directory found."); }; // src/utils/handler.ts import { pathToFileURL } from "node:url"; var EXT_PRIORITY = ["js", "mjs", "cjs"]; var getClientConfig = async () => { const dirPath = path.join(getAppPath(), ".."); if (!fs.existsSync(dirPath)) return; const configFile = fs.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 = path.join(dirPath, configFile.name); const configFiledata = await import(pathToFileURL(configPath).href); return configFiledata?.config; }; var initHandlers = async (client) => { console.log("curr", getCurrenDir()); const handlersPath = path.join(getCurrenDir(), "handlers"); if (!fs.existsSync(handlersPath)) { return []; } const handlerFiles = fs.readdirSync(handlersPath).filter((v) => v.endsWith(".js")); for (const handlerFile of handlerFiles) { const filePath = path.join(handlersPath, handlerFile); const handlerFileData = await import(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 BaseClient { constructor(options, djbOptions) { super(options); this.cache = new Collection(); this.commands = new Collection(); this.selectMenus = new Collection(); this.modals = new Collection(); this.buttons = new 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 import { log as log2 } from "@ad1m/logger"; var djbStart = () => { log2.info("djb", "Starting bot..."); const client = new DJBClient({ intents: [] }, { mongoDb: true }); client.start(); }; // src/cli/djb.dev.ts var djbDev = () => { djbStart(); }; export { djbDev };