From 0488f45677ec4d9cc4b65ff643064e149170d9b9 Mon Sep 17 00:00:00 2001 From: jacob Date: Wed, 1 May 2024 16:46:12 -0500 Subject: [PATCH] refactor allat --- src/core/id.ts | 29 ++++++++++++++-------------- src/core/ioc/dependency-injection.ts | 9 ++------- src/core/operators.ts | 3 +-- src/core/structures/context.ts | 3 +-- src/handlers/event-utils.ts | 6 +++--- src/handlers/message-event.ts | 24 +++++++++++------------ src/handlers/ready-event.ts | 2 +- src/handlers/user-defined-events.ts | 2 +- src/types/core-modules.ts | 1 - src/types/core-plugin.ts | 2 +- src/types/dependencies.d.ts | 4 ++++ src/types/ioc.ts | 1 - src/types/utility.ts | 8 ++++---- 13 files changed, 44 insertions(+), 50 deletions(-) diff --git a/src/core/id.ts b/src/core/id.ts index 3ef4669..173d25c 100644 --- a/src/core/id.ts +++ b/src/core/id.ts @@ -20,22 +20,21 @@ export function reconstruct(event: T) { * * A magic number to represent any commandtype that is an ApplicationCommand. */ -const appBitField = 0b000000001111; +const PUBLISHABLE = 0b000000001111; -const TypeMap = new Map([ - [CommandType.Text, 0], - [CommandType.Both, 0], - [CommandType.Slash, ApplicationCommandType.ChatInput], - [CommandType.CtxUser, ApplicationCommandType.User], - [CommandType.CtxMsg, ApplicationCommandType.Message], - [CommandType.Button, ComponentType.Button], - [CommandType.Modal, InteractionType.ModalSubmit], - [CommandType.StringSelect, ComponentType.StringSelect], - [CommandType.UserSelect, ComponentType.UserSelect], - [CommandType.MentionableSelect, ComponentType.MentionableSelect], - [CommandType.RoleSelect, ComponentType.RoleSelect], - [CommandType.ChannelSelect, ComponentType.ChannelSelect]]); +const TypeMap = new Map([[CommandType.Text, 0], + [CommandType.Both, 0], + [CommandType.Slash, ApplicationCommandType.ChatInput], + [CommandType.CtxUser, ApplicationCommandType.User], + [CommandType.CtxMsg, ApplicationCommandType.Message], + [CommandType.Button, ComponentType.Button], + [CommandType.Modal, InteractionType.ModalSubmit], + [CommandType.StringSelect, ComponentType.StringSelect], + [CommandType.UserSelect, ComponentType.UserSelect], + [CommandType.MentionableSelect, ComponentType.MentionableSelect], + [CommandType.RoleSelect, ComponentType.RoleSelect], + [CommandType.ChannelSelect, ComponentType.ChannelSelect]]); /* * Generates an id based on name and CommandType. @@ -52,7 +51,7 @@ export function create(name: string, type: CommandType | EventType) { if(type == CommandType.Modal) { return `${name}_M`; } - const am = (appBitField & type) !== 0 ? 'A' : 'C'; + const am = (PUBLISHABLE & type) !== 0 ? 'A' : 'C'; return `${name}_${am}${TypeMap.get(type)!}` } diff --git a/src/core/ioc/dependency-injection.ts b/src/core/ioc/dependency-injection.ts index ac1b369..e5fa4ed 100644 --- a/src/core/ioc/dependency-injection.ts +++ b/src/core/ioc/dependency-injection.ts @@ -3,14 +3,11 @@ import type { IntoDependencies } from '../../types/ioc'; import { useContainerRaw } from './base'; /** - * @__PURE__ * @since 2.0.0. * Creates a singleton object. * @param cb */ -export function single(cb: () => T) { - return cb; -} +export function single(cb: () => T) { return cb; } /** * @__PURE__ @@ -18,9 +15,7 @@ export function single(cb: () => T) { * Creates a transient object * @param cb */ -export function transient(cb: () => () => T) { - return cb; -} +export function transient(cb: () => () => T) { return cb; } /** * The new Service api, a cleaner alternative to useContainer * To obtain intellisense, ensure a .d.ts file exists in the root of compilation. diff --git a/src/core/operators.ts b/src/core/operators.ts index af87b13..572a375 100644 --- a/src/core/operators.ts +++ b/src/core/operators.ts @@ -9,7 +9,6 @@ import { EMPTY, every, fromEvent, - map, Observable, of, OperatorFunction, @@ -46,7 +45,7 @@ export function callPlugin(args: unknown): OperatorFunction (Array.isArray(src) ? (src as unknown[]) : [src])); +export const arrayifySource = (src: T) => (Array.isArray(src) ? (src as unknown[]) : [src]); /** * Checks if the stream of results is all ok. diff --git a/src/core/structures/context.ts b/src/core/structures/context.ts index fb37c05..d434800 100644 --- a/src/core/structures/context.ts +++ b/src/core/structures/context.ts @@ -105,8 +105,7 @@ export class Context extends CoreContext { this.ctx .map(m => m.reply(content as MessageReplyOptions)) .mapErr(i => - i.reply(content as InteractionReplyOptions).then(() => i.fetchReply()), - ), + i.reply(content as InteractionReplyOptions).then(() => i.fetchReply())), ); } diff --git a/src/handlers/event-utils.ts b/src/handlers/event-utils.ts index 932164c..2d0ca68 100644 --- a/src/handlers/event-utils.ts +++ b/src/handlers/event-utils.ts @@ -32,7 +32,7 @@ import { PayloadType } from '../core/structures/enums' import { Err, Ok, Result } from 'ts-results-es'; import type { Awaitable } from '../types/utility'; import type { ControlPlugin } from '../types/core-plugin'; -import type { AnyModule, CommandMeta, CommandModule, Module, Processed } from '../types/core-modules'; +import type { CommandMeta, CommandModule, Module, Processed } from '../types/core-modules'; import { EventEmitter } from 'node:events'; import * as assert from 'node:assert'; import { Context } from '../core/structures/context'; @@ -50,7 +50,7 @@ function contextArgs(wrappable: Message | BaseInteraction, messageArgs?: string[ function intoPayload(module: Processed, ) { - return pipe(arrayifySource, + return pipe(map(arrayifySource), map(args => ({ module, args }))); } @@ -244,7 +244,7 @@ export function createResultResolver< * Calls a module's init plugins and checks for Err. If so, call { onStop } and * ignore the module */ -export function callInitPlugins>(sernEmitter: Emitter) { +export function callInitPlugins>(sernEmitter: Emitter) { return concatMap( createResultResolver({ createStream: args => from(args.module.plugins).pipe(callPlugin(args)), diff --git a/src/handlers/message-event.ts b/src/handlers/message-event.ts index b12412c..1dbbddc 100644 --- a/src/handlers/message-event.ts +++ b/src/handlers/message-event.ts @@ -21,7 +21,7 @@ function hasPrefix(prefix: string, content: string) { } export function messageHandler( - [emitter, err, log, modules, client]: DependencyList, + [emitter, err, log, client]: DependencyList, defaultPrefix: string | undefined, ) { if (!defaultPrefix) { @@ -31,15 +31,15 @@ export function messageHandler( return EMPTY; } const messageStream$ = sharedEventStream(client, 'messageCreate'); - const handle = createMessageHandler(messageStream$, defaultPrefix, modules); - - const msgCommands$ = handle(isNonBot(defaultPrefix)); - - return msgCommands$.pipe( - filterTap((e) => emitter.emit('warning', resultPayload(PayloadType.Warning, undefined, e))), - concatMap(makeModuleExecutor(module => { - const result = resultPayload(PayloadType.Failure, module, SernError.PluginFailure); - emitter.emit('module.activate', result); - })), - mergeMap(payload => executeModule(emitter, log, err, payload))); +// const handle = createMessageHandler(messageStream$, defaultPrefix, modules); +// +// const msgCommands$ = handle(isNonBot(defaultPrefix)); +// +// return msgCommands$.pipe( +// filterTap((e) => emitter.emit('warning', resultPayload(PayloadType.Warning, undefined, e))), +// concatMap(makeModuleExecutor(module => { +// const result = resultPayload(PayloadType.Failure, module, SernError.PluginFailure); +// emitter.emit('module.activate', result); +// })), +// mergeMap(payload => executeModule(emitter, log, err, payload))); } diff --git a/src/handlers/ready-event.ts b/src/handlers/ready-event.ts index 6274638..2be7d27 100644 --- a/src/handlers/ready-event.ts +++ b/src/handlers/ready-event.ts @@ -9,7 +9,7 @@ const once = (log: Logging | undefined) => pipe( ignoreElements()) export function readyHandler( - [sEmitter, , log ,, client]: DependencyList, + [sEmitter, , log, client]: DependencyList, allPaths: ObservableInput, ) { //Todo: add module manager on on ready diff --git a/src/handlers/user-defined-events.ts b/src/handlers/user-defined-events.ts index 9051374..a62974a 100644 --- a/src/handlers/user-defined-events.ts +++ b/src/handlers/user-defined-events.ts @@ -7,7 +7,7 @@ import type { DependencyList } from '../types/ioc'; import type { EventModule, Processed } from '../types/core-modules'; export function eventsHandler( - [emitter, err, log, moduleManager, client]: DependencyList, + [emitter, err, log, client]: DependencyList, allPaths: ObservableInput, ) { //code smell diff --git a/src/types/core-modules.ts b/src/types/core-modules.ts index aa1b4b7..2533a87 100644 --- a/src/types/core-modules.ts +++ b/src/types/core-modules.ts @@ -142,7 +142,6 @@ export type CommandModule = | RoleSelectCommand | ModalSubmitCommand; -export type AnyModule = CommandModule | EventModule; //https://stackoverflow.com/questions/64092736/alternative-to-switch-statement-for-typescript-discriminated-union // Explicit Module Definitions for mapping export interface CommandModuleDefs { diff --git a/src/types/core-plugin.ts b/src/types/core-plugin.ts index 0576236..6f1621e 100644 --- a/src/types/core-plugin.ts +++ b/src/types/core-plugin.ts @@ -48,7 +48,7 @@ import type { UserContextMenuCommandInteraction, UserSelectMenuInteraction, } from 'discord.js'; -import { VoidResult } from '../core/_internal'; +import type { VoidResult } from '../core/_internal'; export type PluginResult = Awaitable; diff --git a/src/types/dependencies.d.ts b/src/types/dependencies.d.ts index 2290f37..123e37b 100644 --- a/src/types/dependencies.d.ts +++ b/src/types/dependencies.d.ts @@ -10,3 +10,7 @@ declare global { interface Dependencies extends CoreDependencies {} } + +declare module './handler.js' { + declare const commands : Map +} diff --git a/src/types/ioc.ts b/src/types/ioc.ts index 303ed24..45bce58 100644 --- a/src/types/ioc.ts +++ b/src/types/ioc.ts @@ -15,7 +15,6 @@ export type DependencyList = [ Contracts.Emitter, Contracts.ErrorHandling, Contracts.Logging | undefined, - null, Contracts.Emitter, ]; diff --git a/src/types/utility.ts b/src/types/utility.ts index 3b05e34..95d18fc 100644 --- a/src/types/utility.ts +++ b/src/types/utility.ts @@ -1,6 +1,6 @@ import type { CommandInteractionOptionResolver, InteractionReplyOptions, MessageReplyOptions } from 'discord.js'; import type { PayloadType } from '../core/structures/enums'; -import type { AnyModule } from './core-modules'; +import type { Module } from './core-modules'; export type Awaitable = PromiseLike | T; @@ -18,14 +18,14 @@ export type Args = ParseType<{ text: string[]; slash: SlashOptions }>; export interface SernEventsMapping { 'module.register': [Payload]; 'module.activate': [Payload]; - error: [{ type: PayloadType.Failure; module?: AnyModule; reason: string | Error }]; + error: [{ type: PayloadType.Failure; module?: Module; reason: string | Error }]; warning: [Payload]; modulesLoaded: [never?]; } export type Payload = - | { type: PayloadType.Success; module: AnyModule } - | { type: PayloadType.Failure; module?: AnyModule; reason: string | Error } + | { type: PayloadType.Success; module: Module } + | { type: PayloadType.Failure; module?: Module; reason: string | Error } | { type: PayloadType.Warning; module: undefined; reason: string };