From 5e4a7e2792c7f2e32fc969d1fd2b8c14efdae219 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20Susa=C3=B1a?= Date: Mon, 29 Jul 2024 17:55:03 -0400 Subject: [PATCH] A way to set values in handlers (#228) * fix: handlers seteables values * fix: fix * fix: reload throw error in cw --- src/commands/handler.ts | 30 +++++++++++++++++++++++++++++- src/components/handler.ts | 21 ++++++++++++++++++++- src/events/handler.ts | 33 ++++++++++++++++++++++++++++++--- src/langs/handler.ts | 22 +++++++++++++++++----- 4 files changed, 96 insertions(+), 10 deletions(-) diff --git a/src/commands/handler.ts b/src/commands/handler.ts index f517d97..96a4844 100644 --- a/src/commands/handler.ts +++ b/src/commands/handler.ts @@ -12,7 +12,7 @@ import { } from '../types'; import { basename, dirname } from 'node:path'; import type { Logger, MakeRequired, NulleableCoalising, OmitInsert } from '../common'; -import { BaseHandler } from '../common'; +import { BaseHandler, isCloudfareWorker } from '../common'; import { Command, type CommandOption, SubCommand } from './applications/chat'; import { ContextMenuCommand } from './applications/menu'; import type { UsingClient } from './applications/shared'; @@ -31,6 +31,9 @@ export class CommandHandler extends BaseHandler { } async reload(resolve: string | Command) { + if (isCloudfareWorker()) { + throw new Error('Reload in cloudfare worker is not supported'); + } if (typeof resolve === 'string') { return this.values.find(x => x.name === resolve)?.reload(); } @@ -202,6 +205,30 @@ export class CommandHandler extends BaseHandler { return false; } + set(commands: SeteableCommand[]) { + this.values ??= []; + for (const command of commands) { + let commandInstance; + try { + commandInstance = this.onCommand(command) as Command; + if (!commandInstance) continue; + } catch (e) { + this.logger.warn(`${command.name} ins't a resolvable command`); + this.logger.error(e); + continue; + } + commandInstance.props = this.client.options.commands?.defaults?.props ?? {}; + const isCommand = this.stablishCommandDefaults(commandInstance); + if (isCommand) { + for (const option of commandInstance.options ?? []) { + if (option instanceof SubCommand) this.stablishSubCommandDefaults(commandInstance, option); + } + } else this.stablishContextCommandDefaults(commandInstance); + this.parseLocales(commandInstance); + this.values.push(commandInstance); + } + } + async load(commandsDir: string, client: UsingClient) { const result = await this.loadFilesK>(await this.getFiles(commandsDir)); this.values = []; @@ -475,4 +502,5 @@ export type FileLoaded = { } & Record>; export type HandleableCommand = new () => Command | SubCommand | ContextMenuCommand; +export type SeteableCommand = new () => Extract, SubCommand>; export type HandleableSubCommand = new () => SubCommand; diff --git a/src/components/handler.ts b/src/components/handler.ts index 04c07cd..5e4ff73 100644 --- a/src/components/handler.ts +++ b/src/components/handler.ts @@ -2,7 +2,7 @@ import type { ComponentCallback, ListenerOptions, ModalSubmitCallback } from '.. import { LimitedCollection } from '../collection'; import { BaseCommand, type RegisteredMiddlewares, type UsingClient } from '../commands'; import type { FileLoaded } from '../commands/handler'; -import { BaseHandler, magicImport, type Logger, type OnFailCallback } from '../common'; +import { BaseHandler, isCloudfareWorker, magicImport, type Logger, type OnFailCallback } from '../common'; import type { ComponentInteraction, ModalSubmitInteraction, StringSelectMenuInteraction } from '../structures'; import { ComponentCommand, InteractionCommandType } from './componentcommand'; import type { ComponentContext } from './componentcontext'; @@ -163,6 +163,22 @@ export class ComponentHandler extends BaseHandler { component.onAfterRun ??= this.client.options?.[is]?.defaults?.onAfterRun; } + set(instances: (new () => ComponentCommands)[]) { + for (const i of instances) { + let component; + try { + component = this.callback(i); + if (!component) continue; + } catch (e) { + this.logger.warn(e, i); + continue; + } + this.stablishDefaults(component); + + this.commands.push(component); + } + } + async load(componentsDir: string) { const paths = await this.loadFilesK ComponentCommands>>(await this.getFiles(componentsDir)); @@ -194,6 +210,9 @@ export class ComponentHandler extends BaseHandler { async reload(path: string) { if (!this.client.components) return; + if (isCloudfareWorker()) { + throw new Error('Reload in cloudfare worker is not supported'); + } const component = this.client.components.commands.find( x => x.__filePath?.endsWith(`${path}.js`) || diff --git a/src/events/handler.ts b/src/events/handler.ts index e1ddc6a..f2c4766 100644 --- a/src/events/handler.ts +++ b/src/events/handler.ts @@ -5,7 +5,14 @@ import type { GatewayMessageDeleteDispatch, } from '../types'; import type { Client, WorkerClient } from '../client'; -import { BaseHandler, ReplaceRegex, magicImport, type MakeRequired, type SnakeCase } from '../common'; +import { + BaseHandler, + ReplaceRegex, + isCloudfareWorker, + magicImport, + type MakeRequired, + type SnakeCase, +} from '../common'; import type { ClientEvents } from '../events/hooks'; import * as RawEvents from '../events/hooks'; import type { ClientEvent, CustomEvents, CustomEventsKeys, ClientNameEvents } from './event'; @@ -26,8 +33,25 @@ export class EventHandler extends BaseHandler { values: Partial> = {}; + discordEvents = Object.keys(RawEvents).map(x => ReplaceRegex.camel(x.toLowerCase())) as ClientNameEvents[]; + + set(events: ClientEvent[]) { + for (const event of events) { + const instance = this.callback(event); + if (!instance) continue; + if (typeof instance?.run !== 'function') { + this.logger.warn('Missing event run function'); + continue; + } + this.values[ + this.discordEvents.includes(instance.data.name) + ? (ReplaceRegex.snake(instance.data.name).toUpperCase() as GatewayEvents) + : (instance.data.name as CustomEventsKeys) + ] = instance as EventValue; + } + } + async load(eventsDir: string) { - const discordEvents = Object.keys(RawEvents).map(x => ReplaceRegex.camel(x.toLowerCase())) as ClientNameEvents[]; const paths = await this.loadFilesK<{ file: ClientEvent }>(await this.getFiles(eventsDir)); for (const { events, file } of paths.map(x => ({ events: this.onFile(x.file), file: x }))) { @@ -44,7 +68,7 @@ export class EventHandler extends BaseHandler { } instance.__filePath = file.path; this.values[ - discordEvents.includes(instance.data.name) + this.discordEvents.includes(instance.data.name) ? (ReplaceRegex.snake(instance.data.name).toUpperCase() as GatewayEvents) : (instance.data.name as CustomEventsKeys) ] = instance as EventValue; @@ -141,6 +165,9 @@ export class EventHandler extends BaseHandler { } async reload(name: GatewayEvents | CustomEventsKeys) { + if (isCloudfareWorker()) { + throw new Error('Reload in cloudfare worker is not supported'); + } const event = this.values[name]; if (!event?.__filePath) return null; delete require.cache[event.__filePath]; diff --git a/src/langs/handler.ts b/src/langs/handler.ts index d342d93..27fe977 100644 --- a/src/langs/handler.ts +++ b/src/langs/handler.ts @@ -37,12 +37,22 @@ export class LangsHandler extends BaseHandler { return LangRouter(locale, this.defaultLang ?? locale, this.values)(); } - async load(dir: string, instances?: { name: string; file: Record }[]) { - const files = instances ?? (await this.loadFilesK>(await this.getFiles(dir))); + async load(dir: string) { + const files = await this.loadFilesK>(await this.getFiles(dir)); for (const i of files) { - const locale = i.name.split('.').slice(0, -1).join('.') || i.name; - const result = this.onFile(locale, i.file); - if (result) this.values[locale] = result; + this.parse(i); + } + } + + parse(file: EventInstance) { + const locale = file.name.split('.').slice(0, -1).join('.') || file.name; + const result = this.onFile(locale, file.file); + if (result) this.values[locale] = result; + } + + set(instances: EventInstance[]) { + for (const i of instances) { + this.parse(i); } } @@ -50,3 +60,5 @@ export class LangsHandler extends BaseHandler { return file.default ?? false; } } + +export type EventInstance = { name: string; file: Record };