From 084b5adb5cc6a5c74152670d2686b71e35ec03ef Mon Sep 17 00:00:00 2001 From: Jacob Nguyen <76754747+jacoobes@users.noreply.github.com> Date: Fri, 5 May 2023 23:10:10 -0500 Subject: [PATCH] chore: revert multiplatform --- src/handler/events/dispatchers/dispatchers.ts | 8 +-- src/handler/events/dispatchers/provideArgs.ts | 10 +--- src/handler/events/generic.ts | 25 +++++--- src/handler/events/interactions.ts | 2 +- src/handler/events/messages.ts | 9 ++- src/handler/events/observableHandling.ts | 6 +- src/handler/events/ready.ts | 59 +++++++------------ src/handler/events/userDefined.ts | 9 +-- src/handler/sern.ts | 29 ++++----- 9 files changed, 69 insertions(+), 88 deletions(-) diff --git a/src/handler/events/dispatchers/dispatchers.ts b/src/handler/events/dispatchers/dispatchers.ts index a14d8a7..ed0e403 100644 --- a/src/handler/events/dispatchers/dispatchers.ts +++ b/src/handler/events/dispatchers/dispatchers.ts @@ -1,10 +1,10 @@ -import type { Processed } from '../../../types/handler'; +import type { Processed } from '../../../types/core'; import type { AutocompleteInteraction } from 'discord.js'; -import { SernError } from '../../../core/structures'; +import { SernError } from '../../../core/structures/errors'; import { treeSearch } from '../../../core/functions'; import type { BothCommand, CommandModule, Module } from '../../../types/module'; -import { EventEmitter } from 'events'; -import * as assert from 'assert'; +import { EventEmitter } from 'node:events'; +import * as assert from 'node:assert'; import { concatMap, from, fromEvent, map, OperatorFunction, pipe } from 'rxjs'; import { arrayifySource, callPlugin } from '../../../core/operators'; import { createResultResolver } from '../observableHandling'; diff --git a/src/handler/events/dispatchers/provideArgs.ts b/src/handler/events/dispatchers/provideArgs.ts index f112235..12fca6c 100644 --- a/src/handler/events/dispatchers/provideArgs.ts +++ b/src/handler/events/dispatchers/provideArgs.ts @@ -1,7 +1,6 @@ import type { Message, ChatInputCommandInteraction } from 'discord.js'; -import { CoreContext } from '../../../core/structures' import type { Args, SlashOptions } from '../../../types/handler'; -import Context from '../../../classic/context'; +import { Context } from '../../../classic/context'; /* * @overload @@ -29,10 +28,3 @@ export function interactionArg(interaction: T) { return () => [interaction] as [T]; } - -export function multiplatformArgs< - M, - I, - Ctx extends typeof CoreContext ->(c: Ctx) { -} diff --git a/src/handler/events/generic.ts b/src/handler/events/generic.ts index fc9fa74..319f327 100644 --- a/src/handler/events/generic.ts +++ b/src/handler/events/generic.ts @@ -1,17 +1,23 @@ import { BaseInteraction, ChatInputCommandInteraction, Interaction, InteractionType } from "discord.js"; -import { Observable, filter, map, pipe, switchMap} from "rxjs"; -import { CommandType, ModuleManager, SernEmitter, SernError } from "../../core"; -import { filterMap, errTap } from '../../core/operators'; +import { Observable, filter, map } from "rxjs"; +import { CommandType, ModuleManager } from "../../core"; +import { SernError } from '../../core/structures/errors' +import { filterMap } from '../../core/operators'; import { defaultModuleLoader } from "../../core/module-loading"; -import { Processed } from "../../types/handler"; -import { AnyModule, BothCommand, CommandModule } from "../../types/module"; +import { Processed } from "../../types/core"; +import { BothCommand, CommandModule } from "../../types/module"; import { contextArgs, dispatchAutocomplete, dispatchCommand, interactionArg } from "./dispatchers"; import { isAutocomplete } from "../../core/predicates"; import { err } from "../../core/functions"; -import * as Files from '../../core/module-loading'; +import { ObservableInput, pipe, switchMap} from "rxjs"; +import { SernEmitter } from "../../core"; +import { errTap } from '../../core/operators'; +import * as Files from '../../core/io'; import { sernMeta } from "../../commands"; +import { AnyModule } from "../../types/module"; /** + * * Creates an RxJS observable that filters and maps incoming interactions to their respective modules. * @param i An RxJS observable of interactions. * @param mg The module manager instance used to retrieve the module path for each interaction. @@ -83,9 +89,12 @@ function createDispatcher({ } } -export function buildModules(path: string, sernEmitter: SernEmitter) { + +export function buildModules( + input: ObservableInput, sernEmitter: SernEmitter +) { return pipe( - switchMap(() => Files.buildModuleStream(path)), + switchMap(() => Files.buildModuleStream(input)), errTap(error => { sernEmitter.emit('module.register', SernEmitter.failure(undefined, error)); }), diff --git a/src/handler/events/interactions.ts b/src/handler/events/interactions.ts index 5901f97..17038bd 100644 --- a/src/handler/events/interactions.ts +++ b/src/handler/events/interactions.ts @@ -5,7 +5,7 @@ import { finalize, merge, } from 'rxjs'; -import { SernError } from '../../core/structures'; +import { SernError } from '../../core/structures/errors'; import { executeModule, makeModuleExecutor } from './observableHandling'; import { ErrorHandling, handleError } from '../../core/contracts/errorHandling'; import { SernEmitter, WebsocketStrategy } from '../../core'; diff --git a/src/handler/events/messages.ts b/src/handler/events/messages.ts index 76e7579..727bd67 100644 --- a/src/handler/events/messages.ts +++ b/src/handler/events/messages.ts @@ -1,16 +1,15 @@ -import { catchError, concatMap, EMPTY, finalize, map, of, pipe } from 'rxjs'; -import { SernError } from '../../core/structures'; +import { catchError, concatMap, EMPTY, finalize, map, pipe } from 'rxjs'; +import { SernError } from '../../core/structures/errors'; import type { Message } from 'discord.js'; import { executeModule, ignoreNonBot, makeModuleExecutor } from './observableHandling'; import type { CommandModule } from '../../types/module'; import { ErrorHandling, handleError } from '../../core/contracts/errorHandling'; import { contextArgs, dispatchCommand } from './dispatchers'; -import SernEmitter from '../../core/sernEmitter'; -import type { Processed } from '../../types/handler'; +import type { Processed } from '../../types/core'; import { useContainerRaw } from '../../core/dependencies'; import type { Logging, ModuleManager } from '../../core/contracts'; import type { EventEmitter } from 'node:events'; -import { WebsocketStrategy } from '../../core'; +import { WebsocketStrategy, SernEmitter } from '../../core'; import { err } from '../../core/functions'; import { defaultModuleLoader } from '../../core/module-loading'; import { sharedObservable, filterMap } from '../../core/operators'; diff --git a/src/handler/events/observableHandling.ts b/src/handler/events/observableHandling.ts index 281d3da..8f0da0e 100644 --- a/src/handler/events/observableHandling.ts +++ b/src/handler/events/observableHandling.ts @@ -1,12 +1,12 @@ -import type { Awaitable, Message } from 'discord.js'; import { concatMap, EMPTY, filter, from, Observable, of, tap, throwError } from 'rxjs'; import { Result } from 'ts-results-es'; import type { CommandModule, EventModule, Module } from '../../types/module'; import { SernEmitter } from '../../core'; import { callPlugin, everyPluginOk, filterMapTo } from '../../core/operators'; -import type { ImportPayload, Processed } from '../../types/handler'; +import type { ImportPayload, Processed } from '../../types/core'; import type { ControlPlugin, VoidResult } from '../../types/plugin'; - +import { Awaitable } from '../../types/handler'; +import { Message } from 'discord.js' function hasPrefix(prefix: string, content: string) { const prefixInContent = content.slice(0, prefix.length); return prefixInContent.localeCompare(prefix, undefined, { sensitivity: 'accent' }) === 0; diff --git a/src/handler/events/ready.ts b/src/handler/events/ready.ts index c6cbe55..20bf3ce 100644 --- a/src/handler/events/ready.ts +++ b/src/handler/events/ready.ts @@ -1,49 +1,27 @@ -import { Subscription, fromEvent, of, take } from 'rxjs'; +import { ObservableInput, fromEvent, of, take } from 'rxjs'; import { callInitPlugins } from './observableHandling'; -import { CommandType, SernError } from '../../core/structures'; +import { CommandType } from '../../core/structures'; +import { SernError } from '../../core/structures/errors' import { Result } from 'ts-results-es'; -import type { CommandModule } from '../../types/module'; -import type { Processed, ServerlessDependencyList, WebsocketDependencyList } from '../../types/handler'; -import type { ErrorHandling, Logging, ModuleManager } from '../../core/contracts'; -import SernEmitter from '../../core/sernEmitter'; -import type { EventEmitter } from 'node:events'; -import { DispatchType, PlatformStrategy, ServerlessStrategy, WebsocketStrategy } from '../../core'; +import type { ModuleManager } from '../../core/contracts'; +import { SernEmitter, PlatformStrategy, DispatchType } from '../../core'; import { sernMeta } from '../../commands'; +import { Processed, ServerlessDependencyList, WebsocketDependencyList } from '../../types/core'; +import { Module } from '../../types/module'; +import * as assert from 'node:assert'; import { buildModules } from './generic'; -/** - * @overload - */ -export function makeReadyEvent( - dependencies: ServerlessDependencyList, - commandDir: string, - platform: ServerlessStrategy - -): Subscription -export function makeReadyEvent( - dependencies: WebsocketDependencyList, - commandDir: string, - platform: WebsocketStrategy - -): Subscription - -export function makeReadyEvent( - [sEmitter, errorHandler, , moduleManager, client]: [ - SernEmitter, - ErrorHandling, - Logging | undefined, - ModuleManager, - EventEmitter? - ], - commandDir: string, - platform: PlatformStrategy +export function startReadyEvent( + [sEmitter, errorHandler, , moduleManager, client]: ServerlessDependencyList | WebsocketDependencyList, + input: ObservableInput, + platform: PlatformStrategy, ) { const ready$ = platform.type === DispatchType.Serverless ? of(null) : fromEvent(client!, platform.eventNames[2]).pipe(take(1)); return ready$ .pipe( - buildModules(commandDir, sEmitter), + buildModules(input, sEmitter), callInitPlugins({ onStop: module => { sEmitter.emit( @@ -58,20 +36,27 @@ export function makeReadyEvent( }), ) .subscribe(module => { - const result = registerModule(moduleManager, module as Processed); + console.log(module) + const result = registerModule(moduleManager, module as Processed); if (result.err) { errorHandler.crash(Error(SernError.InvalidModuleType)); } }); } -function registerModule>( + +function registerModule>( manager: ModuleManager, module: T, ): Result { const { id, fullPath } = module[sernMeta]; if(module.type === CommandType.Both || module.type === CommandType.Text) { + assert.ok('alias' in module) + assert.ok(Array.isArray(module.alias)) module.alias?.forEach(a => manager.set(`${a}__A0` , fullPath)) } return Result.wrap(() => manager.set(id, fullPath)) } + + + diff --git a/src/handler/events/userDefined.ts b/src/handler/events/userDefined.ts index 69ef3c5..c8efad0 100644 --- a/src/handler/events/userDefined.ts +++ b/src/handler/events/userDefined.ts @@ -1,11 +1,12 @@ import { catchError, finalize, map, mergeAll, of } from 'rxjs'; -import type { Processed, WebsocketDependencies } from '../../types/handler'; +import type { Processed, WebsocketDependencies } from '../../types/core'; import { callInitPlugins } from './observableHandling'; import type { CommandModule, EventModule } from '../../types/module'; -import type { EventEmitter } from 'events'; -import SernEmitter from '../../core/sernEmitter'; +import type { EventEmitter } from 'node:events'; +import { SernEmitter } from '../../core/sernEmitter'; import type { ErrorHandling, Logging } from '../../core/contracts'; -import { SernError, EventType } from '../../core/structures'; +import { EventType } from '../../core/structures' +import { SernError } from '../../core/structures/errors'; import { eventDispatcher } from './dispatchers'; import { handleError } from '../../core/contracts/errorHandling'; import { useContainerRaw } from '../../core/dependencies'; diff --git a/src/handler/sern.ts b/src/handler/sern.ts index 514124c..63cdbe3 100644 --- a/src/handler/sern.ts +++ b/src/handler/sern.ts @@ -1,12 +1,12 @@ import { makeEventsHandler } from './events/userDefined'; import { makeInteractionCreate } from './events/interactions'; -import { makeReadyEvent } from './events/ready'; +import { startReadyEvent } from './events/ready'; import { makeMessageCreate } from './events/messages'; -import type { AnyDependencies, DependencyConfiguration } from '../types/handler'; -import { composeRoot, makeFetcher, useContainer } from '../core/dependencies'; +import { makeFetcher, makeDependencies } from '../core/dependencies'; import { err, ok } from '../core/functions'; import { DefaultWrapper } from '../core/structures/wrapper'; import { discordjs } from '../core'; +import { getCommands } from '../core/io'; /** * @since 1.0.0 * @param wrapper Options to pass into sern. @@ -33,14 +33,21 @@ export function init(wrapper: DefaultWrapper) { ); } const platform = discordjs(wrapper.defaultPrefix); - makeReadyEvent(dependencies, wrapper.commands, platform); + startReadyEvent(dependencies, getCommands(wrapper.commands), platform); makeMessageCreate(dependencies, platform); makeInteractionCreate(dependencies, platform); const endTime = performance.now(); dependencies[2]?.info({ message: `sern : ${(endTime - startTime).toFixed(2)} ms` }); } - +/** + * @deprecated - Please import the function directly: + * ```ts + * import { makeDependencies } from '@sern/handler' + * + * ``` + */ +export { makeDependencies } /** * @since 1.0.0 * The object passed into every plugin to control a command's behavior @@ -50,15 +57,3 @@ export const controller = { stop: err, }; - -/** - * @since 2.0.0 - * @param conf a configuration for creating your project dependencies - */ -export function makeDependencies(conf: DependencyConfiguration) { - //Until there are more optional dependencies, just check if the logger exists - composeRoot(conf); - return useContainer(); -} - -