From 1e1398fade5514bf8f408ceafd89fb46437f04c3 Mon Sep 17 00:00:00 2001 From: Jacob Nguyen <76754747+jacoobes@users.noreply.github.com> Date: Sat, 6 May 2023 01:16:53 -0500 Subject: [PATCH] refactor: remove unneeded signatures and fix imports --- src/commands.ts | 1 + src/core/dependencies.ts | 16 ++-------- src/core/index.ts | 1 - src/core/module-loading.ts | 4 +-- src/core/operators.ts | 7 ----- src/core/platform.ts | 40 ------------------------ src/core/plugins/args.ts | 4 +-- src/core/plugins/createPlugin.ts | 8 ++--- src/core/predicates.ts | 19 +++++++----- src/core/structures/context.ts | 1 - src/core/structures/wrapper.ts | 49 ------------------------------ src/handler/events/interactions.ts | 5 ++- src/handler/events/messages.ts | 11 ++++--- src/handler/events/ready.ts | 6 +--- src/handler/sern.ts | 12 +++----- src/types/core.ts | 8 +++++ 16 files changed, 44 insertions(+), 148 deletions(-) delete mode 100644 src/core/platform.ts delete mode 100644 src/core/structures/wrapper.ts diff --git a/src/commands.ts b/src/commands.ts index f1172c6..d03e8ab 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -79,6 +79,7 @@ export function eventModule(mod: InputEvent): EventModule { /** Create event modules from discord.js client events, * This is an {@link eventModule} for discord events, * where typings can be very bad. + * @Experimental * @param mod */ export function discordEvent(mod: { diff --git a/src/core/dependencies.ts b/src/core/dependencies.ts index 7e6315f..d659e65 100644 --- a/src/core/dependencies.ts +++ b/src/core/dependencies.ts @@ -1,11 +1,10 @@ import type { Container } from 'iti'; -import type { AnyDependencies, DependencyConfiguration, MapDeps, ServerlessDependencies, WebsocketDependencies } from '../types/core'; +import type { AnyDependencies, DependencyConfiguration, MapDeps, ServerlessDependencies, WebsocketDependencies, Wrapper } from '../types/core'; import { DefaultErrorHandling, DefaultLogging, DefaultModuleManager } from './contracts'; import { Result } from 'ts-results-es'; import { BehaviorSubject } from 'rxjs'; import { createContainer } from 'iti'; import { ModuleStore, SernEmitter } from './structures'; -import { AnyWrapper, ServerlessWrapper, WebsocketWrapper } from './structures/wrapper'; export const containerSubject = new BehaviorSubject(defaultContainer()); @@ -100,22 +99,11 @@ const requiredDependencyKeys = [ '@sern/logger', ] as const; - -/** - * @overload - */ -export function makeFetcher(containerConfig : WebsocketWrapper['containerConfig']) - : (ks: [...Keys]) => MapDeps; -/** - * @overload - */ -export function makeFetcher(containerConfig: ServerlessWrapper['containerConfig']) - : (ks: [...Keys]) => MapDeps; /** * A way for sern to grab only the necessary dependencies. * Returns a function which allows for the user to call for more dependencies. */ -export function makeFetcher(containerConfig : AnyWrapper['containerConfig']) { +export function makeFetcher(containerConfig : Wrapper['containerConfig']) { return (otherKeys: [...Keys]) => containerConfig.get(...requiredDependencyKeys, ...otherKeys as (keyof AnyDependencies)[]) as MapDeps< Dep, diff --git a/src/core/index.ts b/src/core/index.ts index 691442d..4625c33 100644 --- a/src/core/index.ts +++ b/src/core/index.ts @@ -1,5 +1,4 @@ export * from './contracts'; -export * from './platform'; export * from './plugins'; export * from './structures'; export { single, transient, useContainerRaw, makeDependencies } from './dependencies' diff --git a/src/core/module-loading.ts b/src/core/module-loading.ts index 11389e6..e7f44c2 100644 --- a/src/core/module-loading.ts +++ b/src/core/module-loading.ts @@ -3,12 +3,12 @@ import { type Result, Err, Ok } from 'ts-results-es'; import { Processed } from '../types/core'; import { Module } from '../types/module'; import * as assert from 'node:assert' -import * as util from 'node:util' +import util from 'node:util' import { type Observable, from, mergeMap, ObservableInput } from 'rxjs'; import { readdir, stat } from 'fs/promises'; import { basename, join, resolve } from 'path'; -type ModuleResult = Promise, SernError>> +export type ModuleResult = Promise, SernError>> export type Loader = (absPath: string) => ModuleResult export async function defaultModuleLoader( diff --git a/src/core/operators.ts b/src/core/operators.ts index 59da2f9..d2ce8e4 100644 --- a/src/core/operators.ts +++ b/src/core/operators.ts @@ -50,13 +50,6 @@ export function callPlugin(args: unknown): OperatorFunction< export const arrayifySource = map(src => (Array.isArray(src) ? (src as unknown[]) : [src])); -export const fillDefaults = ({ module, absPath }: ImportPayload) => { - module.description ??= '...' - return { - absPath, - module - } as ImportPayload>; -}; /** * If the current value in Result stream is an error, calls callback. diff --git a/src/core/platform.ts b/src/core/platform.ts deleted file mode 100644 index 084e3ff..0000000 --- a/src/core/platform.ts +++ /dev/null @@ -1,40 +0,0 @@ -export const enum DispatchType { - Websocket, - Serverless -} - -export type PlatformStrategy = - | WebsocketStrategy - | ServerlessStrategy; - -export interface WebsocketStrategy { - type: DispatchType.Websocket; - eventNames: [interactioncreate: string, messagecreate: string, ready: string] - defaultPrefix?: string; -} - -export interface ServerlessStrategy { - type: DispatchType.Serverless; -} - -export function makeWebsocketAdapter( - eventNames: [interactioncreate: string, messagecreate: string, ready: string], - defaultPrefix?: string -): WebsocketStrategy { - return { - type: DispatchType.Websocket, - eventNames, - defaultPrefix - }; -} - -export function makeServerlessAdapter(): ServerlessStrategy { - return { - type: DispatchType.Serverless, - }; -} - -export const discordjs = ( defaultPrefix?: string ) => makeWebsocketAdapter( - ['interactionCreate', 'messageCreate', 'ready'], - defaultPrefix -) diff --git a/src/core/plugins/args.ts b/src/core/plugins/args.ts index 21f9f8a..da5dece 100644 --- a/src/core/plugins/args.ts +++ b/src/core/plugins/args.ts @@ -1,8 +1,6 @@ -import type { CommandType } from '../structures/enums'; -import type { PluginType } from '../structures/enums'; +import type { CommandType, PluginType, EventType } from '../structures/enums'; import type { Module } from '../../types/module'; import type { Processed } from '../../types/core'; -import { EventType } from '../structures/enums'; import { CommandArgsMatrix, EventArgsMatrix } from '../../types/module'; export interface InitArgs> { diff --git a/src/core/plugins/createPlugin.ts b/src/core/plugins/createPlugin.ts index b5022c9..2b66d61 100644 --- a/src/core/plugins/createPlugin.ts +++ b/src/core/plugins/createPlugin.ts @@ -14,7 +14,7 @@ export function makePlugin( } /** * @since 2.5.0 - * + * @__PURE__ */ export function EventInitPlugin( execute: (...args: EventArgs) => PluginResult, @@ -23,7 +23,7 @@ export function EventInitPlugin( } /** * @since 2.5.0 - * + * @__PURE__ */ export function CommandInitPlugin( execute: (...args: CommandArgs) => PluginResult, @@ -32,7 +32,7 @@ export function CommandInitPlugin( } /** * @since 2.5.0 - * + * @__PURE__ */ export function CommandControlPlugin( execute: (...args: CommandArgs) => PluginResult, @@ -41,7 +41,7 @@ export function CommandControlPlugin( } /** * @since 2.5.0 - * + * @__PURE__ */ export function EventControlPlugin( execute: (...args: EventArgs) => PluginResult, diff --git a/src/core/predicates.ts b/src/core/predicates.ts index ce415bb..71bac96 100644 --- a/src/core/predicates.ts +++ b/src/core/predicates.ts @@ -1,18 +1,23 @@ -import { AutocompleteInteraction, CommandInteraction, ModalSubmitInteraction } from "discord.js"; -import { BaseInteraction, InteractionType, MessageComponentInteraction } from "discord.js"; +import { AnySelectMenuInteraction, AutocompleteInteraction, ButtonInteraction, ChatInputCommandInteraction, MessageContextMenuCommandInteraction, ModalSubmitInteraction, UserContextMenuCommandInteraction } from "discord.js"; +import { InteractionType } from "discord.js"; - -export function isMessageComponent(i: BaseInteraction): i is MessageComponentInteraction { +interface InteractionTypable { + type: InteractionType +} +//discord.js pls fix ur typings or i will >:( +type AnyMessageComponentInteraction = AnySelectMenuInteraction | ButtonInteraction; +type AnyCommandInteraction = ChatInputCommandInteraction | MessageContextMenuCommandInteraction | UserContextMenuCommandInteraction; +export function isMessageComponent(i: InteractionTypable): i is AnyMessageComponentInteraction { return i.type === InteractionType.MessageComponent; } -export function isCommand(i: BaseInteraction): i is CommandInteraction { +export function isCommand(i: InteractionTypable): i is AnyCommandInteraction { return i.type === InteractionType.ApplicationCommand; } -export function isAutocomplete(i: BaseInteraction): i is AutocompleteInteraction { +export function isAutocomplete(i: InteractionTypable): i is AutocompleteInteraction { return i.type === InteractionType.ApplicationCommandAutocomplete; } -export function isModal(i: BaseInteraction): i is ModalSubmitInteraction { +export function isModal(i: InteractionTypable): i is ModalSubmitInteraction { return i.type === InteractionType.ModalSubmit; } diff --git a/src/core/structures/context.ts b/src/core/structures/context.ts index 49cf76d..d463882 100644 --- a/src/core/structures/context.ts +++ b/src/core/structures/context.ts @@ -3,7 +3,6 @@ import { SernError } from './errors'; import * as assert from 'node:assert' - /** * @since 3.0.0 */ diff --git a/src/core/structures/wrapper.ts b/src/core/structures/wrapper.ts deleted file mode 100644 index 7009705..0000000 --- a/src/core/structures/wrapper.ts +++ /dev/null @@ -1,49 +0,0 @@ -import type { ServerlessDependencies, WebsocketDependencies } from '../../types/core'; -import { DispatchType, ServerlessStrategy, WebsocketStrategy } from '../platform'; - -export interface DefaultWrapper { - commands: string; - defaultPrefix?: string; - events?: string; - containerConfig: { - get: (...keys: (keyof WebsocketDependencies)[]) => unknown[]; - } -} - - -export interface WebsocketWrapper { - readonly platform: WebsocketStrategy; - commands: string; - /** - * @deprecated - * Please specify this in platform specification - */ - defaultPrefix?: string; - events?: string; - containerConfig: { - get: (...keys: (keyof WebsocketDependencies)[]) => unknown[]; - } -} -/** - * @deprecated - * Type alias for WebsocketWrapper - */ -export type Wrapper = WebsocketWrapper | ServerlessWrapper - -export interface ServerlessWrapper { - readonly platform: ServerlessStrategy - commands: string[]; - events?: string[]; - containerConfig: { - get: (...keys: (keyof ServerlessDependencies)[]) => unknown[]; - } -} - -export type AnyWrapper = - | WebsocketWrapper - | ServerlessWrapper - - -export function isServerless(wrapper: AnyWrapper): wrapper is ServerlessWrapper { - return wrapper.platform.type === DispatchType.Serverless; -} diff --git a/src/handler/events/interactions.ts b/src/handler/events/interactions.ts index 941fcaf..aa93e78 100644 --- a/src/handler/events/interactions.ts +++ b/src/handler/events/interactions.ts @@ -23,10 +23,9 @@ export function makeInteractionCreate([s, err, log, modules, client]: [ Logging | undefined, ModuleManager, EventEmitter -], - platform: WebsocketStrategy +] ) { - const interactionStream$ = sharedObservable(client, platform.eventNames[0]); + const interactionStream$ = sharedObservable(client, 'interactionCreate'); const handle = createInteractionHandler(interactionStream$, modules); const interactionHandler$ = merge( handle(isMessageComponent), diff --git a/src/handler/events/messages.ts b/src/handler/events/messages.ts index b0ad5aa..01a7720 100644 --- a/src/handler/events/messages.ts +++ b/src/handler/events/messages.ts @@ -37,15 +37,16 @@ export function makeMessageCreate( ModuleManager, EventEmitter, ], - platform: WebsocketStrategy + defaultPrefix: string | undefined ) { - if(!platform.defaultPrefix) { + if(!defaultPrefix) { + log?.debug({ message: 'No prefix found. message handler shut down' }) return EMPTY.subscribe() } - const messageStream$ = sharedObservable(client, platform.eventNames[1]); - const handler = createMessageHandler(messageStream$, platform.defaultPrefix, modules); + const messageStream$ = sharedObservable(client, 'messageCreate'); + const handler = createMessageHandler(messageStream$, defaultPrefix, modules); const messageHandler = handler( - ignoreNonBot(platform.defaultPrefix) as (m: Message) => m is Message + ignoreNonBot(defaultPrefix) as (m: Message) => m is Message ) return messageHandler .pipe( diff --git a/src/handler/events/ready.ts b/src/handler/events/ready.ts index 20bf3ce..df08d6c 100644 --- a/src/handler/events/ready.ts +++ b/src/handler/events/ready.ts @@ -14,11 +14,8 @@ import { buildModules } from './generic'; 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)); + const ready$ = fromEvent(client!, 'interactionCreate').pipe(take(1)); return ready$ .pipe( buildModules(input, sEmitter), @@ -36,7 +33,6 @@ export function startReadyEvent( }), ) .subscribe(module => { - console.log(module) const result = registerModule(moduleManager, module as Processed); if (result.err) { errorHandler.crash(Error(SernError.InvalidModuleType)); diff --git a/src/handler/sern.ts b/src/handler/sern.ts index 27734f2..a02d777 100644 --- a/src/handler/sern.ts +++ b/src/handler/sern.ts @@ -4,8 +4,7 @@ import { startReadyEvent } from './events/ready'; import { makeMessageCreate } from './events/messages'; import { makeFetcher, makeDependencies } from '../core/dependencies'; import { err, ok } from '../core/functions'; -import { DefaultWrapper } from '../core/structures/wrapper'; -import { discordjs } from '../core'; +import { Wrapper } from '../types/core'; import { getCommands } from '../core/module-loading'; /** * @since 1.0.0 @@ -23,7 +22,7 @@ import { getCommands } from '../core/module-loading'; * }) * ``` */ -export function init(wrapper: DefaultWrapper) { +export function init(wrapper: Wrapper) { const startTime = performance.now(); const dependenciesAnd = makeFetcher(wrapper.containerConfig); const dependencies = dependenciesAnd(['@sern/modules', '@sern/client']); @@ -32,10 +31,9 @@ export function init(wrapper: DefaultWrapper) { dependenciesAnd(['@sern/client']), wrapper.events, wrapper.containerConfig ); } - const platform = discordjs(wrapper.defaultPrefix); - startReadyEvent(dependencies, getCommands(wrapper.commands), platform); - makeMessageCreate(dependencies, platform); - makeInteractionCreate(dependencies, platform); + startReadyEvent(dependencies, getCommands(wrapper.commands)); + makeMessageCreate(dependencies, wrapper.defaultPrefix); + makeInteractionCreate(dependencies); const endTime = performance.now(); dependencies[2]?.info({ message: `sern : ${(endTime - startTime).toFixed(2)} ms` }); diff --git a/src/types/core.ts b/src/types/core.ts index 4ae0862..c28ffe8 100644 --- a/src/types/core.ts +++ b/src/types/core.ts @@ -60,3 +60,11 @@ export interface ImportPayload { absPath: string }; +export interface Wrapper { + commands: string; + defaultPrefix?: string; + events?: string; + containerConfig: { + get: (...keys: (keyof WebsocketDependencies)[]) => unknown[]; + } +}