chore: revert multiplatform

This commit is contained in:
Jacob Nguyen
2023-05-05 23:10:10 -05:00
parent 397857208e
commit 084b5adb5c
9 changed files with 69 additions and 88 deletions

View File

@@ -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';

View File

@@ -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<T>(interaction: T) {
return () => [interaction] as [T];
}
export function multiplatformArgs<
M,
I,
Ctx extends typeof CoreContext<M, I>
>(c: Ctx) {
}

View File

@@ -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<T extends AnyModule>(path: string, sernEmitter: SernEmitter) {
export function buildModules<T extends AnyModule>(
input: ObservableInput<string>, sernEmitter: SernEmitter
) {
return pipe(
switchMap(() => Files.buildModuleStream<T>(path)),
switchMap(() => Files.buildModuleStream<T>(input)),
errTap(error => {
sernEmitter.emit('module.register', SernEmitter.failure(undefined, error));
}),

View File

@@ -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';

View File

@@ -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';

View File

@@ -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;

View File

@@ -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<string>,
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<CommandModule>);
console.log(module)
const result = registerModule(moduleManager, module as Processed<Module>);
if (result.err) {
errorHandler.crash(Error(SernError.InvalidModuleType));
}
});
}
function registerModule<T extends Processed<CommandModule>>(
function registerModule<T extends Processed<Module>>(
manager: ModuleManager,
module: T,
): Result<void, void> {
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))
}

View File

@@ -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';

View File

@@ -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<const T extends AnyDependencies>(conf: DependencyConfiguration<T>) {
//Until there are more optional dependencies, just check if the logger exists
composeRoot(conf);
return useContainer<T>();
}