nhandler
Version:
The easy to use, all-in-one command, event and component handler.
117 lines (116 loc) • 5.3 kB
JavaScript
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.ComponentHandler = void 0;
const fs_1 = require("fs");
const path = __importStar(require("path"));
const ExecutionError_1 = require("../errors/ExecutionError");
const BaseHandler_1 = require("./BaseHandler");
class ComponentHandler extends BaseHandler_1.BaseHandler {
constructor() {
super(...arguments);
this.components = [];
}
componentExists(name) {
return this.components.some((cmp) => cmp.customId === name);
}
register(...components) {
for (const component of components) {
if (this.componentExists(component.customId))
throw new Error(`Cannot register component with duplicate customId: '${component.customId}'.`);
this.debugLog(`Registered component ${component.customId}.`);
component.client = this.client;
this.components.push(component);
}
return this;
}
/**
* registerFromDir automatically loads files & creates class instances in the directory specified.
* If recurse is true, it will also load commands from subdirectories.
* Auto-load commands 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 components 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 (!ComponentHandler.isInstanceOfComponent(instance)) {
this.debugLog(`File ${absolutePath} does not correctly implement Component.`);
continue;
}
this.register(instance);
}
}
return this;
}
static isInstanceOfComponent(object) {
return object.customId !== undefined && object.run !== undefined;
}
runComponent(event, metadata = {}) {
const component = this.components.find((component) => {
if (component.findFn && typeof component.findFn === "function")
return component.findFn(event);
return component.customId === event.customId;
});
if (!component)
return this.debugLog(`runComponent(): Component ${event.customId} not found (did not match any findFn).`);
this.debugLog(`Running component ${component.customId}.`);
if (!component.run || typeof component.run !== "function") {
return this.debugLog(`runComponent(): Component ${event.customId} has no run() method implemented.`);
}
const promise = component.run(event, metadata);
if (!(typeof promise === "object" && promise instanceof Promise)) {
throw new Error("Component run() method must return a promise.");
}
promise.catch((execError) => {
if (!(execError instanceof ExecutionError_1.ExecutionError)) {
throw execError;
}
this.callErrorIfPresent(component, event, execError);
});
}
callErrorIfPresent(component, event, error) {
if (!component.error || typeof component.error !== "function") {
return this.debugLog(`Component ${event.customId} has no error() method implemented.`);
}
component.error(event, error);
}
}
exports.ComponentHandler = ComponentHandler;
;