From d3245c8a0c80f0acda395cab8470716256bf745e Mon Sep 17 00:00:00 2001 From: Jacob Nguyen <76754747+jacoobes@users.noreply.github.com> Date: Thu, 2 May 2024 01:22:40 -0500 Subject: [PATCH] prototyping linking static handler --- src/_internal.ts | 12 +++++------ src/core/modules.ts | 2 ++ src/index.ts | 6 +----- src/sern.ts | 52 ++++++++++++++++----------------------------- 4 files changed, 26 insertions(+), 46 deletions(-) diff --git a/src/_internal.ts b/src/_internal.ts index 690c56d..f0bf834 100644 --- a/src/_internal.ts +++ b/src/_internal.ts @@ -1,6 +1,7 @@ import type { Interaction } from 'discord.js'; import { mergeMap, merge, concatMap } from 'rxjs'; import { PayloadType } from './core/structures/enums'; +import { shouldHandle } from './core/module-loading' import { isAutocomplete, isCommand, @@ -14,9 +15,8 @@ import { } from './core/_internal'; import { createInteractionHandler, executeModule, makeModuleExecutor } from './handlers/event-utils'; import type { Emitter, ErrorHandling, Logging } from './core/interfaces' -import { Services } from './core/ioc/dependency-injection'; -export function interactionHandler(client: Emitter, +function interactionHandler(client: Emitter, emitter: Emitter, log: Logging, err: ErrorHandling, @@ -35,8 +35,6 @@ export function interactionHandler(client: Emitter, mergeMap(payload => executeModule(emitter, log, err, payload))); } -export const __dependencies = () => - Services('@sern/emitter', - '@sern/errors', - '@sern/logger', - '@sern/client'); +export const __start = (entryPoint: string, wrapper: { defaultPrefix?: string }) => { + +} diff --git a/src/core/modules.ts b/src/core/modules.ts index c6d48ae..a37936f 100644 --- a/src/core/modules.ts +++ b/src/core/modules.ts @@ -22,6 +22,7 @@ export function commandModule(mod: InputCommand): _Module { const { name, absPath } = Files.parseCallsite(initCallsite); mod.name ??= name; + mod.description ??= '...' //@ts-ignore return { ...mod, @@ -44,6 +45,7 @@ export function eventModule(mod: InputEvent): _Module { if(!initCallsite) throw Error("initCallsite is null"); const { name, absPath } = Files.parseCallsite(initCallsite); mod.name ??= name; + mod.description ??= '...' //@ts-ignore return { ...mod, diff --git a/src/index.ts b/src/index.ts index 787eea1..6aac3e6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -36,11 +36,7 @@ export type { AnyCommandPlugin, } from './types/core-plugin'; -export interface Wrapper { - commands: string; - defaultPrefix?: string; - events?: string; -} + export type { Args, SlashOptions, Payload, SernEventsMapping } from './types/utility'; export type { Singleton, Transient, CoreDependencies } from './types/ioc'; diff --git a/src/sern.ts b/src/sern.ts index ee518d1..e3a41c0 100644 --- a/src/sern.ts +++ b/src/sern.ts @@ -2,52 +2,36 @@ import callsites from 'callsites'; import * as Files from './core/module-loading'; import { Services } from './core/ioc'; + + +interface Wrapper { + commands?: string; + defaultPrefix?: string; + events?: string; +} + /** * @since 1.0.0 * @param wrapper Options to pass into sern. * Function to start the handler up * @example * ```ts title="src/index.ts" - * Sern.init({ - * commands: 'dist/commands', - * events: 'dist/events', - * }) + * Sern.init() * ``` */ -export function init() { +export function init(wrapper: Wrapper) { const startTime = performance.now(); const dependencies = Services('@sern/emitter', '@sern/errors', '@sern/logger', '@sern/client'); - const logger = dependencies[2], - errorHandler = dependencies[1]; - - //const wrapper = Files.loadConfig(maybeWrapper, logger); - //if (wrapper.events !== undefined) { - //eventsHandler(dependencies, Files.getFullPathTree(wrapper.events)); - // } - //import(path.resolve("commands.js")) - //.then(({ commands }) => { }) - //.catch(message => logger?.error({ message })) + const initCallsite = callsites()[1].getFileName(); - const presencePath = Files.shouldHandle(initCallsite!, "presence"); - //Ready event: load all modules and when finished, time should be taken and logged -// readyHandler(dependencies, Files.getFullPathTree(wrapper.commands)) -// .add(() => { -// logger?.info({ message: "Client signaled ready, registering modules" }); -// const time = ((performance.now() - startTime) / 1000).toFixed(2); -// dependencies[0].emit('modulesLoaded'); -// logger?.info({ message: `sern: registered in ${time} s`, }); -// if(presencePath.exists) { -// const setPresence = async (p: any) => { -// return (dependencies[4] as Client).user?.setPresence(p); -// } -// presenceHandler(presencePath.path, setPresence).subscribe(); -// } -// }); - //const messages$ = messageHandler(dependencies, wrapper.defaultPrefix); - //const interactions$ = interactionHandler(dependencies); - // listening to the message stream and interaction stream - //merge(messages$, interactions$).pipe(handleCrash(errorHandler, dependencies[0], logger)).subscribe(); + const handlerModule = Files.shouldHandle(initCallsite!, "handler"); + if(!handlerModule.exists) { + throw Error("Cannot locate handler file. Did you run sern build?"); + } + import(handlerModule.path) + .then(({ start }) => start(initCallsite, wrapper)) + .catch(err => dependencies[2].error({ message: err })) }