diff --git a/src/client/base.ts b/src/client/base.ts index 00d2b65..9c93870 100644 --- a/src/client/base.ts +++ b/src/client/base.ts @@ -2,7 +2,7 @@ import { join } from 'node:path'; import { ApiHandler, Router } from '../api'; import type { Adapter } from '../cache'; import { Cache, MemoryAdapter } from '../cache'; -import type { RegisteredMiddlewares } from '../commands'; +import type { Command, ContextMenuCommand, RegisteredMiddlewares } from '../commands'; import type { InferWithPrefix, MiddlewareContext } from '../commands/applications/shared'; import { CommandHandler, type CommandHandlerLike } from '../commands/handler'; import { @@ -25,6 +25,7 @@ import { } from '../common'; import type { DeepPartial, IntentStrings, OmitInsert, When } from '../common/types/util'; +import type { ComponentCommand, ModalCommand } from '../components'; import { ComponentHandler, type ComponentHandlerLike } from '../components/handler'; import { LangsHandler, type LangsHandlerLike } from '../langs/handler'; import type { @@ -116,13 +117,34 @@ export class BaseClient { } if (handlers) { if ('components' in handlers) { - this.components = handlers.components; + if (!handlers.components) { + this.components = undefined; + } else if (typeof handlers.components === 'function') { + this.components = new ComponentHandler(this.logger, this); + (this.components as ComponentHandler).__callback = handlers.components; + } else { + this.components = handlers.components; + } } if ('commands' in handlers) { - this.commands = handlers.commands; + if (!handlers.commands) { + this.commands = undefined; + } else if (typeof handlers.commands === 'function') { + this.commands = new CommandHandler(this.logger, this); + (this.commands as CommandHandler).__callback = handlers.commands; + } else { + this.commands = handlers.commands; + } } if ('langs' in handlers) { - this.langs = handlers.langs; + if (!handlers.langs) { + this.langs = undefined; + } else if (typeof handlers.langs === 'function') { + this.langs = new LangsHandler(this.logger); + (this.langs as LangsHandler).__callback = handlers.langs; + } else { + this.langs = handlers.langs; + } } } if (langs) { @@ -322,8 +344,8 @@ export interface ServicesOptions { }; middlewares?: Record; handlers?: { - components?: ComponentHandlerLike; - commands?: CommandHandlerLike; - langs?: LangsHandlerLike; + components?: ComponentHandlerLike | ((component: ComponentCommand | ModalCommand) => void); + commands?: CommandHandlerLike | ((command: Command | ContextMenuCommand) => void); + langs?: LangsHandlerLike | ((locale: string, record: Record) => void); }; } diff --git a/src/client/client.ts b/src/client/client.ts index 1903cb9..bc02346 100644 --- a/src/client/client.ts +++ b/src/client/client.ts @@ -1,5 +1,5 @@ import { parentPort, workerData } from 'node:worker_threads'; -import type { Command, CommandContext, EventHandlerLike, Message, SubCommand } from '..'; +import type { ClientEvent, Command, CommandContext, EventHandlerLike, Message, SubCommand } from '..'; import { GatewayIntentBits, type DeepPartial, @@ -38,7 +38,7 @@ export class Client extends BaseClient { }: ServicesOptions & { gateway?: ShardManager; handlers?: ServicesOptions['handlers'] & { - events?: EventHandlerLike; + events?: EventHandlerLike | ((event: ClientEvent) => any); }; }) { super.setServices(rest); @@ -52,7 +52,14 @@ export class Client extends BaseClient { this.gateway = gateway; } if (rest.handlers && 'events' in rest.handlers) { - this.events = rest.handlers.events; + if (!rest.handlers.events) { + this.events = undefined; + } else if (typeof rest.handlers.events === 'function') { + this.events = new EventHandler(this.logger); + (this.events as EventHandler).__callback = rest.handlers.events; + } else { + this.events = rest.handlers.events; + } } } diff --git a/src/commands/handler.ts b/src/commands/handler.ts index 3e5e87b..ee1c1df 100644 --- a/src/commands/handler.ts +++ b/src/commands/handler.ts @@ -69,6 +69,7 @@ export class CommandHandler extends BaseHandler { if (commandInstance instanceof ContextMenuCommand) { this.values.push(commandInstance); commandInstance.__filePath = command.path; + await this.__callback?.(commandInstance); continue; } if (!(commandInstance instanceof Command)) { @@ -120,6 +121,8 @@ export class CommandHandler extends BaseHandler { this.__parseCommandLocales(i, client); } } + + await this.__callback?.(commandInstance); } return this.values; diff --git a/src/common/it/utils.ts b/src/common/it/utils.ts index ab526f0..f32cb5a 100644 --- a/src/common/it/utils.ts +++ b/src/common/it/utils.ts @@ -94,6 +94,7 @@ export function filterSplit boole * Represents a base handler class. */ export class BaseHandler { + __callback?: (...args: any[]) => any; /** * Initializes a new instance of the BaseHandler class. * @param logger The logger instance. diff --git a/src/components/handler.ts b/src/components/handler.ts index 3e694ab..f30fb60 100644 --- a/src/components/handler.ts +++ b/src/components/handler.ts @@ -184,6 +184,7 @@ export class ComponentHandler extends BaseHandler { if (!(component instanceof ModalCommand) && !(component instanceof ComponentCommand)) continue; component.__filePath = paths[i].path; this.commands.push(component); + await this.__callback?.(component); } } diff --git a/src/events/handler.ts b/src/events/handler.ts index 7b78500..13e606c 100644 --- a/src/events/handler.ts +++ b/src/events/handler.ts @@ -50,6 +50,7 @@ export class EventHandler extends BaseHandler { } instance.__filePath = i.path; this.values[ReplaceRegex.snake(instance.data.name).toUpperCase() as GatewayEvents] = instance as EventValue; + await this.__callback?.(instance); } } diff --git a/src/langs/handler.ts b/src/langs/handler.ts index 2621f69..aba1432 100644 --- a/src/langs/handler.ts +++ b/src/langs/handler.ts @@ -49,6 +49,7 @@ export class LangsHandler extends BaseHandler { for (const i of files) { const locale = i.name.split('.').slice(0, -1).join('.'); this.values[locale] = i.file; + await this.__callback?.(locale, i.file); } } }