@shadow-dev/core
Version:
A modular core framework for Discord bot development, providing commands, buttons, menus, middleware, and more.
199 lines (198 loc) • 8.54 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Bot = void 0;
const discord_js_1 = require("discord.js");
const commandManager_1 = require("./command/commandManager");
const eventManager_1 = require("./event/eventManager");
const buttonManager_1 = require("./button/buttonManager");
const menuManager_1 = require("./menu/menuManager");
const glob_1 = require("glob");
const path_1 = __importDefault(require("path"));
const fs_1 = __importDefault(require("fs"));
const util_1 = require("./util");
const plugin_1 = require("./plugin");
class Bot {
constructor(token, intents, debug = false) {
this.client = new discord_js_1.Client({
intents,
});
this.debug = debug;
this.commandManager = new commandManager_1.CommandManager(this.client);
this.eventManager = new eventManager_1.EventManager(this.client);
this.buttonManager = new buttonManager_1.ButtonManager(this.client);
this.menuManager = new menuManager_1.MenuManager(this.client);
this.pluginLoader = new plugin_1.PluginLoader(this);
(0, util_1.registerModule)("events", this.eventManager, this.client, this.debug).then(() => {
this.client.login(token).then(async () => {
await this.registerModules();
});
});
// this.registerEvents().then(() => {
// this.client.login(token).then(async () => {
// await this.registerModules();
// ButtonManager.LogAllButtons();
// });
// });
}
async registerModules() {
if (this.debug)
console.log("🔍 Registering modules...");
await (0, util_1.registerModule)("commands", this.commandManager, this.client, this.debug);
await (0, util_1.registerModule)("buttons", this.buttonManager, this.client, this.debug);
await (0, util_1.registerModule)("menus", this.menuManager, this.client, this.debug);
console.log(`✅ Successfully loaded ${commandManager_1.CommandManager.getAllCommands().size} commands, ${buttonManager_1.ButtonManager.getAllButtons().size} buttons, ${menuManager_1.MenuManager.getAllMenus().size} menus.`);
// await this.registerCommands();
// await this.registerButtons();
// await this.registerMenus();
await this.pluginLoader.registerPlugins();
if (this.debug)
console.log("✅ All modules registered.");
}
async registerCommands() {
if (this.debug)
console.log("🔍 Scanning for commands...");
const baseDir = process.cwd();
// Detect if running from `src/commands` (development) or `dist/commands` (production)
const isDev = fs_1.default.existsSync(path_1.default.join(baseDir, "src/commands"));
const commandsDir = isDev ? "src/commands" : "dist/commands";
if (this.debug)
console.log("🟢 Using commands directory:", commandsDir);
// Automatically use `.ts` in dev, `.js` in production
const ext = isDev ? "ts" : "js";
const commandFiles = await (0, glob_1.glob)(`**/*.${ext}`, {
cwd: path_1.default.join(baseDir, commandsDir),
absolute: true,
});
if (this.debug)
console.log("🔍 Found command files:", commandFiles);
if (commandFiles.length === 0) {
console.warn("⚠️ No command files found. Check your folder structure.");
}
// Use forEach with async handling
commandFiles.forEach(async (filePath) => {
try {
const command = await (0, util_1.importFile)(filePath);
if (!command?.name)
return;
this.commandManager.registerCommand(command);
if (this.debug)
console.log(`✅ Registered command: ${command.name}`);
}
catch (err) {
console.error(`❌ Error loading command at ${filePath}:`, err);
}
});
if (this.debug) {
console.log(`✅ Successfully loaded ${commandManager_1.CommandManager.getAllCommands().size} commands.`);
}
}
async registerEvents() {
if (this.debug)
console.log("🔍 Scanning for events...");
const baseDir = process.cwd();
// Detect if running from `src/events` (development) or `dist/events` (production)
const isDev = fs_1.default.existsSync(path_1.default.join(baseDir, "src/events"));
const eventsDir = isDev ? "src/events" : "dist/events";
if (this.debug)
console.log("🟢 Using Events directory:", eventsDir);
// Automatically use `.ts` in dev, `.js` in production
const ext = isDev ? "ts" : "js";
const eventFiles = await (0, glob_1.glob)(`**/*.${ext}`, {
cwd: path_1.default.join(baseDir, eventsDir),
absolute: true,
});
for (const filePath of eventFiles) {
try {
const event = await (0, util_1.importFile)(filePath);
if (!event || !event.event || typeof event.run !== "function") {
console.warn(`⚠️ Skipping invalid event file: ${filePath}`);
continue;
}
if (event.once) {
this.client.once(event.event, (...args) => {
event.run(...args);
});
}
else {
this.client.on(event.event, (...args) => event.run(...args));
}
}
catch (err) {
console.error(`❌ Error loading event at ${filePath}:`, err);
}
}
if (this.debug)
console.log("✅ Events registered.");
}
async registerButtons() {
if (this.debug)
console.log("🔍 Scanning for buttons...");
const baseDir = process.cwd();
// Detect if running from `src/commands` (development) or `dist/commands` (production)
const isDev = fs_1.default.existsSync(path_1.default.join(baseDir, "src/buttons"));
const buttonsDir = isDev ? "src/buttons" : "dist/buttons";
const ext = isDev ? "ts" : "js";
const buttonFiles = await (0, glob_1.glob)(`**/*.${ext}`, {
cwd: path_1.default.join(baseDir, buttonsDir),
absolute: true,
});
for (const filePath of buttonFiles) {
try {
const button = await (0, util_1.importFile)(filePath);
console.log(button);
this.buttonManager.registerButton(button);
}
catch (err) {
console.error(`❌ Error loading button at ${filePath}:`, err);
}
}
if (this.debug)
console.log("✅ Buttons registered.");
}
async registerMenus() {
if (this.debug)
console.log("🔍 Scanning for select menus...");
const baseDir = process.cwd();
// Detect if running from `src/commands` (development) or `dist/commands` (production)
const isDev = fs_1.default.existsSync(path_1.default.join(baseDir, "src/menus"));
const menuDir = isDev ? "src/menus" : "dist/menus";
if (this.debug)
console.log("🟢 Using menus directory:", menuDir);
// Automatically use `.ts` in dev, `.js` in production
const ext = isDev ? "ts" : "js";
const menuFiles = await (0, glob_1.glob)(`**/*.${ext}`, {
cwd: path_1.default.join(baseDir, menuDir),
absolute: true,
});
for (const filePath of menuFiles) {
try {
const menu = await (0, util_1.importFile)(filePath);
this.menuManager.registerMenu(menu);
}
catch (err) {
console.error(`❌ Error loading menu at ${filePath}:`, err);
}
}
if (this.debug)
console.log("✅ Select menus registered.");
}
getCommandManager() {
return this.commandManager;
}
getEventManager() {
return this.eventManager;
}
getButtonManager() {
return this.buttonManager;
}
getMenuManager() {
return this.menuManager;
}
getClient() {
return this.client;
}
}
exports.Bot = Bot;