feat: handler callbacks

This commit is contained in:
MARCROCK22 2024-03-26 18:21:38 -04:00
parent 0136b6aae0
commit caf85b63af
7 changed files with 46 additions and 10 deletions

View File

@ -2,7 +2,7 @@ import { join } from 'node:path';
import { ApiHandler, Router } from '../api'; import { ApiHandler, Router } from '../api';
import type { Adapter } from '../cache'; import type { Adapter } from '../cache';
import { Cache, MemoryAdapter } 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 type { InferWithPrefix, MiddlewareContext } from '../commands/applications/shared';
import { CommandHandler, type CommandHandlerLike } from '../commands/handler'; import { CommandHandler, type CommandHandlerLike } from '../commands/handler';
import { import {
@ -25,6 +25,7 @@ import {
} from '../common'; } from '../common';
import type { DeepPartial, IntentStrings, OmitInsert, When } from '../common/types/util'; import type { DeepPartial, IntentStrings, OmitInsert, When } from '../common/types/util';
import type { ComponentCommand, ModalCommand } from '../components';
import { ComponentHandler, type ComponentHandlerLike } from '../components/handler'; import { ComponentHandler, type ComponentHandlerLike } from '../components/handler';
import { LangsHandler, type LangsHandlerLike } from '../langs/handler'; import { LangsHandler, type LangsHandlerLike } from '../langs/handler';
import type { import type {
@ -116,13 +117,34 @@ export class BaseClient {
} }
if (handlers) { if (handlers) {
if ('components' in 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) { 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) { 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) { if (langs) {
@ -322,8 +344,8 @@ export interface ServicesOptions {
}; };
middlewares?: Record<string, MiddlewareContext>; middlewares?: Record<string, MiddlewareContext>;
handlers?: { handlers?: {
components?: ComponentHandlerLike; components?: ComponentHandlerLike | ((component: ComponentCommand | ModalCommand) => void);
commands?: CommandHandlerLike; commands?: CommandHandlerLike | ((command: Command | ContextMenuCommand) => void);
langs?: LangsHandlerLike; langs?: LangsHandlerLike | ((locale: string, record: Record<string, unknown>) => void);
}; };
} }

View File

@ -1,5 +1,5 @@
import { parentPort, workerData } from 'node:worker_threads'; 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 { import {
GatewayIntentBits, GatewayIntentBits,
type DeepPartial, type DeepPartial,
@ -38,7 +38,7 @@ export class Client<Ready extends boolean = boolean> extends BaseClient {
}: ServicesOptions & { }: ServicesOptions & {
gateway?: ShardManager; gateway?: ShardManager;
handlers?: ServicesOptions['handlers'] & { handlers?: ServicesOptions['handlers'] & {
events?: EventHandlerLike; events?: EventHandlerLike | ((event: ClientEvent) => any);
}; };
}) { }) {
super.setServices(rest); super.setServices(rest);
@ -52,7 +52,14 @@ export class Client<Ready extends boolean = boolean> extends BaseClient {
this.gateway = gateway; this.gateway = gateway;
} }
if (rest.handlers && 'events' in rest.handlers) { 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;
}
} }
} }

View File

@ -69,6 +69,7 @@ export class CommandHandler extends BaseHandler {
if (commandInstance instanceof ContextMenuCommand) { if (commandInstance instanceof ContextMenuCommand) {
this.values.push(commandInstance); this.values.push(commandInstance);
commandInstance.__filePath = command.path; commandInstance.__filePath = command.path;
await this.__callback?.(commandInstance);
continue; continue;
} }
if (!(commandInstance instanceof Command)) { if (!(commandInstance instanceof Command)) {
@ -120,6 +121,8 @@ export class CommandHandler extends BaseHandler {
this.__parseCommandLocales(i, client); this.__parseCommandLocales(i, client);
} }
} }
await this.__callback?.(commandInstance);
} }
return this.values; return this.values;

View File

@ -94,6 +94,7 @@ export function filterSplit<Element, Predicate extends (value: Element) => boole
* Represents a base handler class. * Represents a base handler class.
*/ */
export class BaseHandler { export class BaseHandler {
__callback?: (...args: any[]) => any;
/** /**
* Initializes a new instance of the BaseHandler class. * Initializes a new instance of the BaseHandler class.
* @param logger The logger instance. * @param logger The logger instance.

View File

@ -184,6 +184,7 @@ export class ComponentHandler extends BaseHandler {
if (!(component instanceof ModalCommand) && !(component instanceof ComponentCommand)) continue; if (!(component instanceof ModalCommand) && !(component instanceof ComponentCommand)) continue;
component.__filePath = paths[i].path; component.__filePath = paths[i].path;
this.commands.push(component); this.commands.push(component);
await this.__callback?.(component);
} }
} }

View File

@ -50,6 +50,7 @@ export class EventHandler extends BaseHandler {
} }
instance.__filePath = i.path; instance.__filePath = i.path;
this.values[ReplaceRegex.snake(instance.data.name).toUpperCase() as GatewayEvents] = instance as EventValue; this.values[ReplaceRegex.snake(instance.data.name).toUpperCase() as GatewayEvents] = instance as EventValue;
await this.__callback?.(instance);
} }
} }

View File

@ -49,6 +49,7 @@ export class LangsHandler extends BaseHandler {
for (const i of files) { for (const i of files) {
const locale = i.name.split('.').slice(0, -1).join('.'); const locale = i.name.split('.').slice(0, -1).join('.');
this.values[locale] = i.file; this.values[locale] = i.file;
await this.__callback?.(locale, i.file);
} }
} }
} }