mirror of
https://github.com/tiramisulabs/seyfert.git
synced 2025-07-02 04:56:07 +00:00
feat: handler callbacks
This commit is contained in:
parent
0136b6aae0
commit
caf85b63af
@ -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<string, MiddlewareContext>;
|
||||
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<string, unknown>) => void);
|
||||
};
|
||||
}
|
||||
|
@ -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<Ready extends boolean = boolean> 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<Ready extends boolean = boolean> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
|
@ -94,6 +94,7 @@ export function filterSplit<Element, Predicate extends (value: Element) => 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.
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user