UNPKG

nhandler

Version:

The easy to use, all-in-one command, event and component handler.

148 lines (147 loc) 6.84 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ContextMenuHandler = void 0; const fs_1 = require("fs"); const path = __importStar(require("path")); const ExecutionError_1 = require("../errors/ExecutionError"); const BaseHandler_1 = require("./BaseHandler"); class ContextMenuHandler extends BaseHandler_1.BaseHandler { constructor() { super(...arguments); this.actions = []; } actionExists(name, type) { return this.actions.some((action) => action.name === name && action.type === type); } register(...actions) { for (const action of actions) { if (this.actionExists(action.name, action.type)) throw new Error(`Cannot register context menu action with duplicate name and type: '${action.name}', type '${action.type}'.`); this.debugLog(`Registered context menu action ${action.name}.`); action.client = this.client; BaseHandler_1.commandsToRegister.push(ContextMenuHandler.actionMapper(action)); this.actions.push(action); this.emit("actionRegistered", action); } return this; } /** * registerFromDir automatically loads files & creates class instances in the directory specified. * If recurse is true, it will also load context menu actions from subdirectories. * Auto-load context menu actions need to have a __default__ export. Otherwise they will be ignored. * @param dir The directory to load files from. * @param recurse Whether to load files from subdirectories. * */ registerFromDir(dir, recurse = true) { if (!this.client) throw new Error("Client not set."); this.debugLog("Loading context menu actions from directory " + dir + "."); const filesInDirectory = (0, fs_1.readdirSync)(dir); for (const file of filesInDirectory) { const absolutePath = path.join(dir, file); if (recurse && (0, fs_1.statSync)(absolutePath).isDirectory()) { this.registerFromDir(absolutePath); } else if (file.endsWith(".js") || file.endsWith(".ts")) { delete require.cache[require.resolve(absolutePath)]; const defaultExport = require(absolutePath).default; if (!defaultExport) { this.debugLog(`File ${absolutePath} does not default-export a class. Ignoring.`); continue; } const instance = new defaultExport(this.client); if (!ContextMenuHandler.isInstanceOfCtxMenuAction(instance)) { this.debugLog(`File ${absolutePath} does not correctly implement ContextMenuAction.`); continue; } this.register(instance); } } return this; } checkConditionals(event, action) { if (action.guildId && event.guildId !== action.guildId) { return new ExecutionError_1.ExecutionError("This action is not available in this guild."); } if (action.allowDm === false && event.guildId === null) { return new ExecutionError_1.ExecutionError("This action is not available in DMs."); } if (action.allowedGuilds && !action.allowedGuilds.includes(event.guildId)) { return new ExecutionError_1.ExecutionError("This action is not available in this guild."); } if (action.allowedUsers && !action.allowedUsers.includes(event.user.id)) { return new ExecutionError_1.ExecutionError("You are not allowed to use this action."); } return undefined; } runAction(event, metadata = {}) { if (!event.commandType || event.commandType < 2) { throw new Error("runAction() only accepts ContextMenuInteraction."); } const action = this.actions.find((action) => action.name === event.commandName && action.type === event.commandType); if (!action) return this.debugLog(`runAction(): Command ${event.commandName} not found.`); this.debugLog(`Running context menu action ${action.name}.`); /* Check preconditions, like allowedGuilds, allowedUsers etc. */ const error = this.checkConditionals(event, action); if (error) { this.callErrorIfPresent(action, event, error); return; } if (!action.run || typeof action.run !== "function") { return this.debugLog(`runAction(): Action ${event.commandName} has no run() method implemented.`); } const promise = action.run(event, metadata); if (!(typeof promise === "object" && promise instanceof Promise)) { throw new Error("Action run method must return a promise."); } promise.catch((execError) => { if (!(execError instanceof ExecutionError_1.ExecutionError)) { throw execError; } this.callErrorIfPresent(action, event, execError); }); } callErrorIfPresent(action, event, error) { if (!action.error || typeof action.error !== "function") { return this.debugLog(`Action ${event.commandName} has no error() method implemented.`); } action.error(event, error); } static isInstanceOfCtxMenuAction(object) { return object.name !== undefined && object.run !== undefined; } static actionMapper(action) { return { type: action.type, name: action.name, guildId: action.guildId, dmPermission: action.allowDm, defaultMemberPermissions: action.defaultMemberPermissions ?? null, }; } } exports.ContextMenuHandler = ContextMenuHandler;