mirror of
https://github.com/tiramisulabs/seyfert.git
synced 2025-07-01 20:46:08 +00:00
A way to set values in handlers (#228)
* fix: handlers seteables values * fix: fix * fix: reload throw error in cw
This commit is contained in:
parent
2f1ba85a94
commit
5e4a7e2792
@ -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<FileLoaded<null>>(await this.getFiles(commandsDir));
|
||||
this.values = [];
|
||||
@ -475,4 +502,5 @@ export type FileLoaded<T = null> = {
|
||||
} & Record<string, NulleableCoalising<T, HandleableCommand>>;
|
||||
|
||||
export type HandleableCommand = new () => Command | SubCommand | ContextMenuCommand;
|
||||
export type SeteableCommand = new () => Extract<InstanceType<HandleableCommand>, SubCommand>;
|
||||
export type HandleableSubCommand = new () => SubCommand;
|
||||
|
@ -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<FileLoaded<new () => 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`) ||
|
||||
|
@ -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<Record<GatewayEvents | CustomEventsKeys, EventValue>> = {};
|
||||
|
||||
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];
|
||||
|
@ -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<string, any> }[]) {
|
||||
const files = instances ?? (await this.loadFilesK<Record<string, any>>(await this.getFiles(dir)));
|
||||
async load(dir: string) {
|
||||
const files = await this.loadFilesK<Record<string, any>>(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<string, any> };
|
||||
|
Loading…
x
Reference in New Issue
Block a user