From 0b60586847accf9b3701d7fe314e3e42e53bbc77 Mon Sep 17 00:00:00 2001 From: MARCROCK22 <57925328+MARCROCK22@users.noreply.github.com> Date: Tue, 21 May 2024 21:19:23 -0400 Subject: [PATCH] fix: implement stopIffail in reloadAll --- src/components/handler.ts | 16 ++++++++++------ src/events/handler.ts | 12 +++++++++--- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/components/handler.ts b/src/components/handler.ts index dbf404e..3be4f3c 100644 --- a/src/components/handler.ts +++ b/src/components/handler.ts @@ -196,7 +196,7 @@ export class ComponentHandler extends BaseHandler { x.__filePath?.endsWith(path) || x.__filePath === path, ); - if (!component || !component.__filePath) return null; + if (!component?.__filePath) return null; delete require.cache[component.__filePath]; const index = this.client.components.commands.findIndex(x => x.__filePath === component.__filePath!); if (index === -1) return null; @@ -208,11 +208,15 @@ export class ComponentHandler extends BaseHandler { return imported; } - async reloadAll() { - if (!this.client.components) return; - for (const i of this.client.components.commands) { - if (!i.__filePath) return this.logger.warn('Unknown command dont have __filePath property', i); - await this.reload(i.__filePath); + async reloadAll(stopIfFail = true) { + for (const i of this.commands) { + try { + await this.reload(i.__filePath ?? ''); + } catch (e) { + if (stopIfFail) { + throw e; + } + } } } diff --git a/src/events/handler.ts b/src/events/handler.ts index 67f7cdb..4c29bbe 100644 --- a/src/events/handler.ts +++ b/src/events/handler.ts @@ -92,7 +92,7 @@ export class EventHandler extends BaseHandler { async reload(name: ClientNameEvents) { const eventName = ReplaceRegex.snake(name).toUpperCase() as GatewayEvents; const event = this.values[eventName]; - if (!event) return null; + if (!event?.__filePath) return null; delete require.cache[event.__filePath]; const imported = await magicImport(event.__filePath).then(x => x.default ?? x); imported.__filePath = event.__filePath; @@ -100,9 +100,15 @@ export class EventHandler extends BaseHandler { return imported; } - async reloadAll() { + async reloadAll(stopIfFail = true) { for (const i in this.values) { - await this.reload(ReplaceRegex.camel(i) as ClientNameEvents); + try { + await this.reload(ReplaceRegex.camel(i) as ClientNameEvents); + } catch (e) { + if (stopIfFail) { + throw e; + } + } } }