diff --git a/src/core/contracts/moduleManager.ts b/src/core/contracts/moduleManager.ts index 4e7f0a7..5a7296d 100644 --- a/src/core/contracts/moduleManager.ts +++ b/src/core/contracts/moduleManager.ts @@ -1,3 +1,5 @@ +import { CommandModule } from '../../types/module'; +import { importModule } from '../module-loading'; import type { ModuleStore } from '../structures'; /** * @since 2.0.0 @@ -5,6 +7,7 @@ import type { ModuleStore } from '../structures'; export interface ModuleManager { get(id: string): string | undefined; set(id: string, path: string): void; + getPublishableCommands() : Promise } /** * @since 2.0.0 @@ -12,10 +15,19 @@ export interface ModuleManager { export class DefaultModuleManager implements ModuleManager { constructor(private moduleStore: ModuleStore) {} get(id: string) { - return this.moduleStore.Commands.get(id); + return this.moduleStore.get(id); } set(id: string, path: string): void { - this.moduleStore.Commands.set(id, path) + this.moduleStore.set(id, path) + } + //not tested + getPublishableCommands(): Promise { + const entries = this.moduleStore.entries(); + const publishable = 0b000000110; + return Promise.all( + Array.from(entries) + .filter(([id,]) => (Number.parseInt(id.at(-1)!) & publishable) !== 0) + .map(([, path]) => importModule(path))) } } diff --git a/src/core/module-loading.ts b/src/core/module-loading.ts index e7f44c2..54b5014 100644 --- a/src/core/module-loading.ts +++ b/src/core/module-loading.ts @@ -10,23 +10,21 @@ import { basename, join, resolve } from 'path'; export type ModuleResult = Promise, SernError>> export type Loader = (absPath: string) => ModuleResult - +export async function importModule(absPath: string) { + /// #if MODE === 'esm' + return (await import(absPath)).default as T + /// #elif MODE === 'cjs' + return require(absPath).default as T; // eslint-disable-line + /// #endif +} export async function defaultModuleLoader( absPath: string, ): ModuleResult { // prettier-ignore - let module: T | undefined - /// #if MODE === 'esm' - = (await import(absPath)).default - /// #elif MODE === 'cjs' - = require(absPath).default; // eslint-disable-line - /// #endif + const module = await importModule(absPath); if (module === undefined) { - return Err(SernError.UndefinedModule); + return Err(SernError.UndefinedModule); } - try { - module = new (module as unknown as new () => T)(); - } catch {} checkIsProcessed(module) return Ok(module); } diff --git a/src/core/operators.ts b/src/core/operators.ts index d2ce8e4..8a86f42 100644 --- a/src/core/operators.ts +++ b/src/core/operators.ts @@ -4,11 +4,9 @@ * and independent of each other */ import { concatMap, defaultIfEmpty, EMPTY, every, fromEvent, map, Observable, of, OperatorFunction, pipe, share, switchMap } from 'rxjs'; -import type { AnyModule } from '../types/module'; import type { PluginResult, VoidResult } from '../types/plugin'; import { Result } from 'ts-results-es'; import { Awaitable } from '../types/handler'; -import { ImportPayload, Processed } from '../types/core'; import { EventEmitter } from 'node:events'; /** * if {src} is true, mapTo V, else ignore diff --git a/src/core/predicates.ts b/src/core/predicates.ts index 71bac96..5925ac9 100644 --- a/src/core/predicates.ts +++ b/src/core/predicates.ts @@ -10,7 +10,6 @@ type AnyCommandInteraction = ChatInputCommandInteraction | MessageContextMenuCom export function isMessageComponent(i: InteractionTypable): i is AnyMessageComponentInteraction { return i.type === InteractionType.MessageComponent; } - export function isCommand(i: InteractionTypable): i is AnyCommandInteraction { return i.type === InteractionType.ApplicationCommand; } diff --git a/src/core/structures/moduleStore.ts b/src/core/structures/moduleStore.ts index 2bd32d4..423e0c9 100644 --- a/src/core/structures/moduleStore.ts +++ b/src/core/structures/moduleStore.ts @@ -4,8 +4,8 @@ * Storing all command modules * This dependency is usually injected into ModuleManager */ -export class ModuleStore { - readonly Commands = new Map(); +export class ModuleStore extends Map { + } diff --git a/src/handler/sern.ts b/src/handler/sern.ts index a02d777..0ad0b84 100644 --- a/src/handler/sern.ts +++ b/src/handler/sern.ts @@ -13,7 +13,6 @@ import { getCommands } from '../core/module-loading'; * @example * ```ts title="src/index.ts" * Sern.init({ - * platform: djs('!'), * commands: 'dist/commands', * events: 'dist/events', * containerConfig : {