@shadow-dev/core
Version:
A modular core framework for Discord bot development, providing commands, buttons, menus, middleware, and more.
66 lines (65 loc) • 3.31 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.EventManager = void 0;
const discord_js_1 = require("discord.js");
const commandManager_1 = require("../command/commandManager");
const buttonManager_1 = require("../button/buttonManager");
const menuManager_1 = require("../menu/menuManager");
const middleware_1 = require("../middleware");
class EventManager {
constructor(client) {
this.client = client;
this.buttonManager = new buttonManager_1.ButtonManager(client);
this.menuManager = new menuManager_1.MenuManager(client);
this.client.on("interactionCreate", async (interaction) => {
const middleware = await middleware_1.Middleware.loadAllMiddleware();
if (interaction.isChatInputCommand()) {
const command = commandManager_1.CommandManager.getAllCommands().get(interaction.commandName);
if (!command) {
console.log(`❌ Command "${interaction.commandName}" not found in command manager.`);
return await interaction.reply({ content: "Command not found!", flags: [discord_js_1.MessageFlags.Ephemeral] });
}
const commandName = interaction.commandName;
const commandMiddleware = middleware.commandMiddleware;
try {
const globalMiddleware = commandMiddleware.get('global');
if (globalMiddleware) {
const proceed = await globalMiddleware.beforeExecution(interaction, command);
if (!proceed)
return;
}
const specificMiddleware = commandMiddleware.get(commandName);
if (specificMiddleware) {
const proceed = await specificMiddleware.beforeExecution(interaction, command);
if (!proceed)
return;
}
await command.middleware(interaction, this.client, interaction.options, command);
if (globalMiddleware) {
await globalMiddleware.afterExecution(interaction, command);
}
if (specificMiddleware) {
await specificMiddleware.afterExecution(interaction, command);
}
}
catch (error) {
console.error(`❌ Error executing command "${interaction.commandName}":`, error);
await interaction.reply({ content: "An error occurred while executing this command.", flags: [discord_js_1.MessageFlags.Ephemeral] });
}
}
else if (interaction.isButton()) {
await this.buttonManager.handleInteraction(interaction);
}
else if (interaction.isStringSelectMenu()) {
await this.menuManager.handleInteraction(interaction);
}
if (this.customInteractionHandler) {
await this.customInteractionHandler(interaction);
}
});
}
setCustomInteractionHandler(handler) {
this.customInteractionHandler = handler;
}
}
exports.EventManager = EventManager;