From 59a13765a5242201d0f914bb559b13ff466927fc Mon Sep 17 00:00:00 2001 From: Jacob Nguyen <76754747+jacoobes@users.noreply.github.com> Date: Thu, 18 May 2023 11:57:28 -0500 Subject: [PATCH] style: pretty --- src/core/contracts/error-handling.ts | 2 - src/core/contracts/init.ts | 2 +- src/core/contracts/module-manager.ts | 3 +- src/core/contracts/module-store.ts | 6 +- src/core/functions.ts | 35 +++++----- src/core/index.ts | 10 +-- src/core/ioc/base.ts | 6 +- src/core/ioc/dependency-injection.ts | 19 +++--- src/core/ioc/types.ts | 6 +- src/core/module-loading.ts | 26 ++++---- src/core/operators.ts | 4 +- src/core/structures/container.ts | 35 +++++----- src/core/structures/context.ts | 1 - src/core/structures/module-store.ts | 2 +- src/core/structures/sern-emitter.ts | 1 - .../structures/services/module-manager.ts | 66 +++++++++---------- src/core/types/modules.ts | 2 - src/core/types/plugins.ts | 33 +++++++++- src/handler/commands.ts | 33 ++++++---- src/handler/events/dispatchers.ts | 29 ++++---- src/handler/events/generic.ts | 53 ++++++++------- src/handler/events/interactions.ts | 19 +++--- src/handler/events/messages.ts | 2 +- src/handler/events/ready.ts | 21 +++--- src/handler/events/user-defined.ts | 8 +-- src/handler/id.ts | 6 +- src/handler/sern.ts | 62 ++++++++--------- src/handler/types.ts | 2 +- src/index.ts | 9 ++- src/shared.ts | 11 +--- tsconfig.json | 1 - tsup.config.js | 10 +-- 32 files changed, 270 insertions(+), 255 deletions(-) diff --git a/src/core/contracts/error-handling.ts b/src/core/contracts/error-handling.ts index 107d664..c7a3950 100644 --- a/src/core/contracts/error-handling.ts +++ b/src/core/contracts/error-handling.ts @@ -18,6 +18,4 @@ export interface ErrorHandling { * @param error */ updateAlive(error: Error): void; - } - diff --git a/src/core/contracts/init.ts b/src/core/contracts/init.ts index 3d8810c..9e5e513 100644 --- a/src/core/contracts/init.ts +++ b/src/core/contracts/init.ts @@ -5,5 +5,5 @@ import { Awaitable } from '../../shared'; * Let dependencies implement this to initiate some logic. */ export interface Init { - init() : Awaitable + init(): Awaitable; } diff --git a/src/core/contracts/module-manager.ts b/src/core/contracts/module-manager.ts index 5834897..2b53512 100644 --- a/src/core/contracts/module-manager.ts +++ b/src/core/contracts/module-manager.ts @@ -9,6 +9,5 @@ export interface ModuleManager { setMetadata(m: Module, c: CommandMeta): void; set(id: string, path: string): void; getPublishableCommands(): Promise; - remove(id: string) : boolean + remove(id: string): boolean; } - diff --git a/src/core/contracts/module-store.ts b/src/core/contracts/module-store.ts index 2d4b0b2..b9bc92c 100644 --- a/src/core/contracts/module-store.ts +++ b/src/core/contracts/module-store.ts @@ -1,9 +1,9 @@ -import { CommandMeta, Module } from "../types/modules"; +import { CommandMeta, Module } from '../types/modules'; /** * Represents a core module store that stores IDs mapped to file paths. */ export interface CoreModuleStore { - commands : Map; - metadata : WeakMap + commands: Map; + metadata: WeakMap; } diff --git a/src/core/functions.ts b/src/core/functions.ts index 8e1c4d0..3396e93 100644 --- a/src/core/functions.ts +++ b/src/core/functions.ts @@ -8,14 +8,19 @@ import { PluginType } from './structures'; export const ok = /* @__PURE__*/ () => Ok.EMPTY; export const err = /* @__PURE__*/ () => Err.EMPTY; -export function partitionPlugins(arr: (AnyEventPlugin|AnyCommandPlugin)[] = []): [Plugin[], Plugin[]] { +export function partitionPlugins( + arr: (AnyEventPlugin | AnyCommandPlugin)[] = [], +): [Plugin[], Plugin[]] { const controlPlugins = []; const initPlugins = []; for (const el of arr) { - switch(el.type) - { - case PluginType.Control: controlPlugins.push(el); break; - case PluginType.Init: initPlugins.push(el); break; + switch (el.type) { + case PluginType.Control: + controlPlugins.push(el); + break; + case PluginType.Init: + initPlugins.push(el); + break; } } return [controlPlugins, initPlugins]; @@ -39,22 +44,22 @@ export function treeSearch( switch (cur.type) { case ApplicationCommandOptionType.Subcommand: case ApplicationCommandOptionType.SubcommandGroup: - { + { for (const option of cur.options ?? []) { _options.push(option); } - } - break; + } + break; default: - { - if (cur.autocomplete) { - const choice = iAutocomplete.options.getFocused(true); - if (cur.name === choice.name && cur.autocomplete) { - autocompleteData = cur; + { + if (cur.autocomplete) { + const choice = iAutocomplete.options.getFocused(true); + if (cur.name === choice.name && cur.autocomplete) { + autocompleteData = cur; + } } } - } - break; + break; } } return autocompleteData; diff --git a/src/core/index.ts b/src/core/index.ts index c78d226..9ac6593 100644 --- a/src/core/index.ts +++ b/src/core/index.ts @@ -24,12 +24,6 @@ export type { EventModuleDefs, BaseOptions, SernAutocompleteData, - SernOptionsData + SernOptionsData, } from './types/modules'; -export type { - Controller, - PluginResult, - InitPlugin, - ControlPlugin, - Plugin -} from './types/plugins'; +export type { Controller, PluginResult, InitPlugin, ControlPlugin, Plugin } from './types/plugins'; diff --git a/src/core/ioc/base.ts b/src/core/ioc/base.ts index 069febb..8796864 100644 --- a/src/core/ioc/base.ts +++ b/src/core/ioc/base.ts @@ -3,8 +3,7 @@ import { composeRoot, useContainer } from './dependency-injection'; import { Dependencies, DependencyConfiguration } from './types'; import { CoreContainer } from '../structures/container'; - -//SIDE EFFECT: GLOBAL DI +//SIDE EFFECT: GLOBAL DI let containerSubject: CoreContainer>; /** @@ -14,7 +13,7 @@ let containerSubject: CoreContainer>; export function useContainerRaw() { assert.ok( containerSubject && containerSubject.isReady(), - "Could not find container or container wasn't ready. Did you call makeDependencies?" + "Could not find container or container wasn't ready. Did you call makeDependencies?", ); return containerSubject; } @@ -33,4 +32,3 @@ export async function makeDependencies( return useContainer(); } - diff --git a/src/core/ioc/dependency-injection.ts b/src/core/ioc/dependency-injection.ts index 007593e..23485cc 100644 --- a/src/core/ioc/dependency-injection.ts +++ b/src/core/ioc/dependency-injection.ts @@ -1,10 +1,15 @@ -import type { DependencyConfiguration, MapDeps, IntoDependencies, Dependencies, CoreDependencies } from './types'; +import type { + DependencyConfiguration, + MapDeps, + IntoDependencies, + Dependencies, + CoreDependencies, +} from './types'; import { DefaultLogging } from '../structures'; import { SernError } from '../structures/errors'; import { useContainerRaw } from './base'; import { CoreContainer } from '../structures/container'; - /** * @__PURE__ * @since 2.0.0. @@ -18,7 +23,7 @@ export function single(cb: () => T) { /** * @__PURE__ * @since 2.0.0 - * Creates a transient object + * Creates a transient object * @param cb */ export function transient(cb: () => () => T) { @@ -42,7 +47,7 @@ export function Services(...keys: [...T] */ export async function composeRoot( container: CoreContainer>, - conf: DependencyConfiguration + conf: DependencyConfiguration, ) { //container should have no client or logger yet. const hasLogger = conf.exclude?.has('@sern/logger'); @@ -71,11 +76,7 @@ export function useContainer() { Warning: using a container hook (useContainer) is not recommended. Could lead to many unwanted side effects. Use the new Service(s) api function instead. - ` - ); + `); return (...keys: [...V]) => keys.map(key => useContainerRaw().get(key as keyof Dependencies)) as MapDeps; } - - - diff --git a/src/core/ioc/types.ts b/src/core/ioc/types.ts index a231427..0a1788b 100644 --- a/src/core/ioc/types.ts +++ b/src/core/ioc/types.ts @@ -3,7 +3,6 @@ import * as Contract from '../contracts'; export type Singleton = () => T; export type Transient = () => () => T; - export interface CoreDependencies { '@sern/logger'?: Singleton; '@sern/emitter': Singleton; @@ -15,10 +14,10 @@ export interface CoreDependencies { export interface Dependencies extends CoreDependencies { '@sern/client': Singleton; } -export type DependencyFromKey = Dependencies[T]; +export type DependencyFromKey = Dependencies[T]; export type IntoDependencies = { - [Index in keyof Tuple]: UnpackFunction&{}>; //Unpack and make NonNullable + [Index in keyof Tuple]: UnpackFunction & {}>; //Unpack and make NonNullable } & { length: Tuple['length'] }; export interface DependencyConfiguration { @@ -38,4 +37,3 @@ export type MapDeps = T ...(MapDeps extends [never] ? [] : MapDeps), ] : [never]; - diff --git a/src/core/module-loading.ts b/src/core/module-loading.ts index d0717fc..63d1843 100644 --- a/src/core/module-loading.ts +++ b/src/core/module-loading.ts @@ -9,19 +9,18 @@ import { CommandExecutable, clazz } from '../handler/commands'; export type ModuleResult = Promise, SernError>>; - function isClassModule(m: unknown): m is typeof CommandExecutable { return m != undefined && Reflect.has(m, clazz); } export async function importModule(absPath: string) { - let module = - /// #if MODE === 'esm' - import(absPath).then(i => i.default); // eslint-disable-line + let module = + /// #if MODE === 'esm' + import(absPath).then(i => i.default); // eslint-disable-line /// #elif MODE === 'cjs' require(absPath).default; // eslint-disable-line /// #endif - return module.then(m => isClassModule(m) ? m.getInstance():m) as T; + return module.then(m => (isClassModule(m) ? m.getInstance() : m)) as T; } export async function defaultModuleLoader(absPath: string): ModuleResult { let module = await importModule(absPath); @@ -32,7 +31,6 @@ export async function defaultModuleLoader(absPath: string): Mo return Ok({ module, absPath }); } - export const fmtFileName = (n: string) => n.substring(0, n.length - 3); /** @@ -64,19 +62,18 @@ async function* readPaths(dir: string, shouldDebug: boolean): AsyncGenerator(e: EventEmitter, eventName: string) => { export function handleError(crashHandler: ErrorHandling, logging?: Logging) { return (pload: unknown, caught: Observable) => { // This is done to fit the ErrorHandling contract - const err = pload instanceof Error - ? pload - : Error(util.inspect(pload, { colors: true })); + const err = pload instanceof Error ? pload : Error(util.inspect(pload, { colors: true })); if (crashHandler.keepAlive == 0) { crashHandler.crash(err); } diff --git a/src/core/structures/container.ts b/src/core/structures/container.ts index 0a7120e..82a22fd 100644 --- a/src/core/structures/container.ts +++ b/src/core/structures/container.ts @@ -1,6 +1,6 @@ import { Container } from 'iti'; import { DefaultErrorHandling, DefaultModuleManager, SernEmitter } from '../'; -import { isAsyncFunction} from 'node:util/types'; +import { isAsyncFunction } from 'node:util/types'; import * as assert from 'node:assert'; import { Subject } from 'rxjs'; import { ModuleStore } from './module-store'; @@ -22,34 +22,35 @@ export class CoreContainer> extends Container new DefaultErrorHandling(), '@sern/emitter': () => new SernEmitter(), '@sern/store': () => new ModuleStore(), - }).add(ctx => { + }) + .add(ctx => { return { '@sern/modules': () => new DefaultModuleManager(ctx['@sern/store']) }; }); } - - private listenForInsertions() { - assert.ok(!this.isReady(), 'listening for init functions should only occur prior to sern being ready.'); - const unsubscriber = this.on('containerUpserted', this.callInitHooks); - this.ready$.subscribe({ - complete: unsubscriber - }); + private listenForInsertions() { + assert.ok( + !this.isReady(), + 'listening for init functions should only occur prior to sern being ready.', + ); + const unsubscriber = this.on('containerUpserted', this.callInitHooks); + + this.ready$.subscribe({ + complete: unsubscriber, + }); } - private async callInitHooks(e: { key: keyof T, newContainer: T[keyof T]|null }) { - + private async callInitHooks(e: { key: keyof T; newContainer: T[keyof T] | null }) { const dep = e.newContainer; assert.ok(dep); //Ignore any dependencies that are not objects or array - if(typeof(dep) !== 'object' || Array.isArray(dep)) { + if (typeof dep !== 'object' || Array.isArray(dep)) { return; } - if('init' in dep && typeof dep.init === 'function') { - isAsyncFunction(dep.init) - ? await dep.init() - : dep.init(); - } + if ('init' in dep && typeof dep.init === 'function') { + isAsyncFunction(dep.init) ? await dep.init() : dep.init(); + } } isReady() { diff --git a/src/core/structures/context.ts b/src/core/structures/context.ts index 9a40d8e..4ccba27 100644 --- a/src/core/structures/context.ts +++ b/src/core/structures/context.ts @@ -92,4 +92,3 @@ export class Context extends CoreContext { function safeUnwrap(res: Result) { return res.val; } - diff --git a/src/core/structures/module-store.ts b/src/core/structures/module-store.ts index e8cb7ee..b7c667c 100644 --- a/src/core/structures/module-store.ts +++ b/src/core/structures/module-store.ts @@ -7,6 +7,6 @@ import { Module, CommandMeta } from '../types/modules'; * For interacting with modules, use the ModuleManager instead. */ export class ModuleStore implements CoreModuleStore { - metadata = new WeakMap(); + metadata = new WeakMap(); commands = new Map(); } diff --git a/src/core/structures/sern-emitter.ts b/src/core/structures/sern-emitter.ts index 44bff6f..21d5f1a 100644 --- a/src/core/structures/sern-emitter.ts +++ b/src/core/structures/sern-emitter.ts @@ -7,7 +7,6 @@ import { Module } from '../types/modules'; * @since 1.0.0 */ export class SernEmitter extends EventEmitter { - constructor() { super({ captureRejections: true }); } diff --git a/src/core/structures/services/module-manager.ts b/src/core/structures/services/module-manager.ts index 84ff640..f9ccd81 100644 --- a/src/core/structures/services/module-manager.ts +++ b/src/core/structures/services/module-manager.ts @@ -3,42 +3,42 @@ import { CoreModuleStore, ModuleManager } from '../../contracts'; import { importModule } from '../../module-loading'; import { CommandMeta, CommandModule, Module } from '../../types/modules'; /** -* @internal -* @since 2.0.0 -* Version 4.0.0 will internalize this api. Please refrain from using ModuleStore! -*/ + * @internal + * @since 2.0.0 + * Version 4.0.0 will internalize this api. Please refrain from using ModuleStore! + */ export class DefaultModuleManager implements ModuleManager { - constructor(private moduleStore: CoreModuleStore) {} - setMetadata(m: Module, c: CommandMeta): void { + constructor(private moduleStore: CoreModuleStore) {} + setMetadata(m: Module, c: CommandMeta): void { this.moduleStore.metadata.set(m, c); - } - - getMetadata(m: Module): CommandMeta { - const maybeModule = this.moduleStore.metadata.get(m); - if(!maybeModule) { - throw Error("Could not find metadata in store for " + maybeModule); - } - return maybeModule; } - remove(id: string): boolean { - throw new Error('Method not implemented.'); - } + getMetadata(m: Module): CommandMeta { + const maybeModule = this.moduleStore.metadata.get(m); + if (!maybeModule) { + throw Error('Could not find metadata in store for ' + maybeModule); + } + return maybeModule; + } - get(id: string) { - return this.moduleStore.commands.get(id); - } - set(id: string, path: string): void { - this.moduleStore.commands.set(id, path); - } - //not tested - getPublishableCommands(): Promise { - const entries = this.moduleStore.commands.entries(); - const publishable = 0b000000110; - return Promise.all( - Array.from(entries) - .filter(([id]) => !(Number.parseInt(id.at(-1)!) & publishable)) - .map(([, path]) => importModule(path)) - ); - } + remove(id: string): boolean { + throw new Error('Method not implemented.'); + } + + get(id: string) { + return this.moduleStore.commands.get(id); + } + set(id: string, path: string): void { + this.moduleStore.commands.set(id, path); + } + //not tested + getPublishableCommands(): Promise { + const entries = this.moduleStore.commands.entries(); + const publishable = 0b000000110; + return Promise.all( + Array.from(entries) + .filter(([id]) => !(Number.parseInt(id.at(-1)!) & publishable)) + .map(([, path]) => importModule(path)), + ); + } } diff --git a/src/core/types/modules.ts b/src/core/types/modules.ts index 327278f..a62dc3b 100644 --- a/src/core/types/modules.ts +++ b/src/core/types/modules.ts @@ -29,8 +29,6 @@ import { Awaitable, SernEventsMapping } from '../../shared'; import { Processed } from '../../handler/types'; import { Args, SlashOptions } from '../../shared'; - - export interface CommandMeta { fullPath: string; id: string; diff --git a/src/core/types/plugins.ts b/src/core/types/plugins.ts index 27abc49..ceb0af3 100644 --- a/src/core/types/plugins.ts +++ b/src/core/types/plugins.ts @@ -12,11 +12,40 @@ */ import type { Err, Ok, Result } from 'ts-results-es'; -import type { BothCommand, ButtonCommand, ChannelSelectCommand, CommandModule, ContextMenuMsg, ContextMenuUser, DiscordEventCommand, EventModule, ExternalEventCommand, MentionableSelectCommand, ModalSubmitCommand, RoleSelectCommand, SernEventCommand, SlashCommand, StringSelectCommand, TextCommand, UserSelectCommand } from './modules'; +import type { + BothCommand, + ButtonCommand, + ChannelSelectCommand, + CommandModule, + ContextMenuMsg, + ContextMenuUser, + DiscordEventCommand, + EventModule, + ExternalEventCommand, + MentionableSelectCommand, + ModalSubmitCommand, + RoleSelectCommand, + SernEventCommand, + SlashCommand, + StringSelectCommand, + TextCommand, + UserSelectCommand, +} from './modules'; import { Args, Awaitable, Payload, SlashOptions } from '../../shared'; import { CommandType, Context, EventType, PluginType } from '../structures'; import { InitArgs, Processed } from '../../handler/types'; -import { ButtonInteraction, ChannelSelectMenuInteraction, ClientEvents, MentionableSelectMenuInteraction, MessageContextMenuCommandInteraction, ModalSubmitInteraction, RoleSelectMenuInteraction, StringSelectMenuInteraction, UserContextMenuCommandInteraction, UserSelectMenuInteraction } from 'discord.js'; +import { + ButtonInteraction, + ChannelSelectMenuInteraction, + ClientEvents, + MentionableSelectMenuInteraction, + MessageContextMenuCommandInteraction, + ModalSubmitInteraction, + RoleSelectMenuInteraction, + StringSelectMenuInteraction, + UserContextMenuCommandInteraction, + UserSelectMenuInteraction, +} from 'discord.js'; export type PluginResult = Awaitable; export type VoidResult = Result; diff --git a/src/handler/commands.ts b/src/handler/commands.ts index e2fe14f..9f10acc 100644 --- a/src/handler/commands.ts +++ b/src/handler/commands.ts @@ -1,11 +1,23 @@ import { ClientEvents } from 'discord.js'; import { CommandType, EventType, PluginType } from '../core/structures'; -import { AnyCommandPlugin, AnyEventPlugin, CommandArgs, ControlPlugin, EventArgs, InitPlugin } from '../core/types/plugins'; -import { CommandModule, EventModule, InputCommand, InputEvent, Module } from '../core/types/modules'; +import { + AnyCommandPlugin, + AnyEventPlugin, + CommandArgs, + ControlPlugin, + EventArgs, + InitPlugin, +} from '../core/types/plugins'; +import { + CommandModule, + EventModule, + InputCommand, + InputEvent, + Module, +} from '../core/types/modules'; import { partitionPlugins } from '../core/functions'; import { Awaitable } from '../shared'; - export const clazz = Symbol('@sern/class'); /** * @since 1.0.0 The wrapper function to define command modules for sern @@ -29,7 +41,7 @@ export function eventModule(mod: InputEvent): EventModule { return { ...mod, plugins, - onEvent + onEvent, } as EventModule; } @@ -66,20 +78,19 @@ function prepareClassPlugins(c: Module) { export abstract class CommandExecutable { abstract type: Type; plugins: AnyCommandPlugin[] = []; - private static _instance : CommandModule; + private static _instance: CommandModule; static readonly [clazz] = true; static getInstance() { if (!CommandExecutable._instance) { //@ts-ignore CommandExecutable._instance = new this(); - prepareClassPlugins(CommandExecutable._instance); + prepareClassPlugins(CommandExecutable._instance); } return CommandExecutable._instance; } - abstract execute(...args: CommandArgs) : Awaitable - + abstract execute(...args: CommandArgs): Awaitable; } /** @@ -90,7 +101,7 @@ export abstract class EventExecutable { abstract type: Type; plugins: AnyEventPlugin[] = []; static readonly [clazz] = true; - private static _instance : EventModule; + private static _instance: EventModule; static getInstance() { if (!EventExecutable._instance) { //@ts-ignore @@ -98,8 +109,6 @@ export abstract class EventExecutable { prepareClassPlugins(EventExecutable._instance); } return EventExecutable._instance; - } + } abstract execute(...args: EventArgs): Awaitable; } - - diff --git a/src/handler/events/dispatchers.ts b/src/handler/events/dispatchers.ts index cc59fdb..98f42b4 100644 --- a/src/handler/events/dispatchers.ts +++ b/src/handler/events/dispatchers.ts @@ -12,12 +12,9 @@ import { Processed } from '../types'; import { BothCommand, CommandModule, Module } from '../../core/types/modules'; import { Args } from '../../shared'; -export function dispatchInteraction< - T extends CommandModule, - V extends BaseInteraction | Message ->( +export function dispatchInteraction( payload: { module: Processed; event: V }, - createArgs: (m: typeof payload.event) => unknown[] + createArgs: (m: typeof payload.event) => unknown[], ) { return { module: payload.module, @@ -28,11 +25,14 @@ export function dispatchInteraction< export function dispatchMessage(module: Processed, args: [Context, Args]) { return { module, - args + args, }; } -export function dispatchAutocomplete(payload: { module: Processed, event: AutocompleteInteraction }) { +export function dispatchAutocomplete(payload: { + module: Processed; + event: AutocompleteInteraction; +}) { const option = treeSearch(payload.event, payload.module.options); if (option !== undefined) { return { @@ -43,21 +43,14 @@ export function dispatchAutocomplete(payload: { module: Processed, throw Error( SernError.NotSupportedInteraction + ` There is no autocomplete tag for this option`, ); - } - - -export function contextArgs( - wrappable: Message | BaseInteraction, - messageArgs?: string[], -) { +export function contextArgs(wrappable: Message | BaseInteraction, messageArgs?: string[]) { const ctx = Context.wrap(wrappable); const args = ctx.isMessage() ? ['text', messageArgs!] : ['slash', ctx.options]; return [ctx, args] as [Context, Args]; } - export function interactionArg(interaction: T) { return [interaction] as [T]; } @@ -101,7 +94,11 @@ export function createDispatcher(payload: { }) { switch (payload.module.type) { case CommandType.Text: - throw Error(SernError.MismatchEvent + ' Found a text module in interaction stream. ' + payload.module); + throw Error( + SernError.MismatchEvent + + ' Found a text module in interaction stream. ' + + payload.module, + ); case CommandType.Slash: case CommandType.Both: { if (isAutocomplete(payload.event)) { diff --git a/src/handler/events/generic.ts b/src/handler/events/generic.ts index 9d94671..207a5e9 100644 --- a/src/handler/events/generic.ts +++ b/src/handler/events/generic.ts @@ -1,8 +1,15 @@ +import { Interaction, Message } from 'discord.js'; import { - Interaction, - Message, -} from 'discord.js'; -import { EMPTY, Observable, concatMap, filter, from, of, throwError, tap, MonoTypeOperatorFunction } from 'rxjs'; + EMPTY, + Observable, + concatMap, + filter, + from, + of, + throwError, + tap, + MonoTypeOperatorFunction, +} from 'rxjs'; import { ModuleManager } from '../../core'; import { SernError } from '../../core/structures/errors'; import { callPlugin, everyPluginOk, filterMap, filterMapTo } from '../../core/operators'; @@ -61,40 +68,40 @@ export function createMessageHandler( if (fullPath === undefined) { return Err(SernError.UndefinedModule + ' No full path found in module store'); } - return defaultModuleLoader>(fullPath) - .then(result => { - const args = contextArgs(event, rest); - return result.map(payload => dispatchMessage(payload.module, args)); - }); + return defaultModuleLoader>(fullPath).then(result => { + const args = contextArgs(event, rest); + return result.map(payload => dispatchMessage(payload.module, args)); + }); }); } /** - * IMPURE SIDE EFFECT - * This function assigns remaining, incomplete data to each imported module. - */ -function assignDefaults(moduleManager: ModuleManager): MonoTypeOperatorFunction> { - return tap( - ({ module, absPath }) => { + * IMPURE SIDE EFFECT + * This function assigns remaining, incomplete data to each imported module. + */ +function assignDefaults( + moduleManager: ModuleManager, +): MonoTypeOperatorFunction> { + return tap(({ module, absPath }) => { module.name ??= Files.filename(absPath); module.description ??= '...'; - moduleManager.setMetadata( - module, { fullPath: absPath, id: `${module.name}_${uniqueId(module.type)}` } - ); - } - ); + moduleManager.setMetadata(module, { + fullPath: absPath, + id: `${module.name}_${uniqueId(module.type)}`, + }); + }); } export function buildModules( input: ObservableInput, sernEmitter: SernEmitter, - moduleManager: ModuleManager + moduleManager: ModuleManager, ) { return pipe( switchMap(() => Files.buildModuleStream(input)), errTap(error => { sernEmitter.emit('module.register', SernEmitter.failure(undefined, error)); }), - assignDefaults(moduleManager) + assignDefaults(moduleManager), ); } @@ -206,5 +213,3 @@ export function makeModuleExecutor< }), ); } - - diff --git a/src/handler/events/interactions.ts b/src/handler/events/interactions.ts index 610bd0e..58304f5 100644 --- a/src/handler/events/interactions.ts +++ b/src/handler/events/interactions.ts @@ -1,5 +1,5 @@ import { Interaction } from 'discord.js'; -import { concatMap, merge } from 'rxjs'; +import { concatMap, merge } from 'rxjs'; import { SernError } from '../../core/structures/errors'; import { SernEmitter } from '../../core'; import { sharedObservable } from '../../core/operators'; @@ -7,8 +7,7 @@ import { isAutocomplete, isCommand, isMessageComponent, isModal } from '../../co import { createInteractionHandler, executeModule, makeModuleExecutor } from './generic'; import { DependencyList } from '../types'; -export function makeInteractionHandler([emitter,,, modules, client]: DependencyList ) { - +export function makeInteractionHandler([emitter, , , modules, client]: DependencyList) { const interactionStream$ = sharedObservable(client, 'interactionCreate'); const handle = createInteractionHandler(interactionStream$, modules); @@ -18,12 +17,10 @@ export function makeInteractionHandler([emitter,,, modules, client]: DependencyL handle(isCommand), handle(isModal), ); - return interactionHandler$ - .pipe( - makeModuleExecutor(module => { - emitter.emit('module.activate', SernEmitter.failure(module, SernError.PluginFailure)); - }), - concatMap(payload => executeModule(emitter, payload)), - ); + return interactionHandler$.pipe( + makeModuleExecutor(module => { + emitter.emit('module.activate', SernEmitter.failure(module, SernError.PluginFailure)); + }), + concatMap(payload => executeModule(emitter, payload)), + ); } - diff --git a/src/handler/events/messages.ts b/src/handler/events/messages.ts index bbb0c67..7049761 100644 --- a/src/handler/events/messages.ts +++ b/src/handler/events/messages.ts @@ -21,7 +21,7 @@ export function fmt(msg: string, prefix: string): string[] { } export function makeMessageHandler( - [emitter,, log, modules, client]: DependencyList, + [emitter, , log, modules, client]: DependencyList, defaultPrefix: string | undefined, ) { if (!defaultPrefix) { diff --git a/src/handler/events/ready.ts b/src/handler/events/ready.ts index fc876c1..4065746 100644 --- a/src/handler/events/ready.ts +++ b/src/handler/events/ready.ts @@ -10,7 +10,7 @@ import { AnyModule } from '../../core/types/modules'; import * as assert from 'node:assert'; export function startReadyEvent( - [sEmitter,,, moduleManager, client]: DependencyList, + [sEmitter, , , moduleManager, client]: DependencyList, allPaths: ObservableInput, ) { const ready$ = fromEvent(client!, 'ready').pipe(take(1)); @@ -19,7 +19,10 @@ export function startReadyEvent( buildModules>(allPaths, sEmitter, moduleManager), callInitPlugins({ onStop: module => { - sEmitter.emit('module.register', SernEmitter.failure(module, SernError.PluginFailure)); + sEmitter.emit( + 'module.register', + SernEmitter.failure(module, SernError.PluginFailure), + ); }, onNext: ({ module }) => { sEmitter.emit('module.register', SernEmitter.success(module)); @@ -30,7 +33,7 @@ export function startReadyEvent( .subscribe(module => { const result = registerModule(moduleManager, module); if (result.err) { - throw Error(SernError.InvalidModuleType + " " + result.val); + throw Error(SernError.InvalidModuleType + ' ' + result.val); } }); } @@ -39,16 +42,14 @@ function registerModule>( manager: ModuleManager, module: T, ): Result { - const { id, fullPath } = manager.getMetadata(module); - assert.ok(module.type > 0 && module.type < 1<<10, `Found ${module.name} at ${fullPath}, which does not have a valid type`); - if (module.type === CommandType.Both - || module.type === CommandType.Text - ) { + assert.ok( + module.type > 0 && module.type < 1 << 10, + `Found ${module.name} at ${fullPath}, which does not have a valid type`, + ); + if (module.type === CommandType.Both || module.type === CommandType.Text) { module.alias?.forEach(a => manager.set(`${a}_A0`, fullPath)); } return Result.wrap(() => manager.set(id, fullPath)); } - - diff --git a/src/handler/events/user-defined.ts b/src/handler/events/user-defined.ts index b21da99..3e2af87 100644 --- a/src/handler/events/user-defined.ts +++ b/src/handler/events/user-defined.ts @@ -10,13 +10,10 @@ import { Service, useContainerRaw } from '../../core/ioc'; import { DependencyList, Processed } from '../types'; import { Dependencies } from '../../core/ioc/types'; - - export function makeEventsHandler( [emitter, err, log, moduleManager, client]: DependencyList, allPaths: ObservableInput, ) { - //code smell const intoDispatcher = (e: Processed) => { switch (e.type) { @@ -37,7 +34,10 @@ export function makeEventsHandler( buildModules>(allPaths, emitter, moduleManager), callInitPlugins({ onStop: module => - emitter.emit('module.register', SernEmitter.failure(module, SernError.PluginFailure)), + emitter.emit( + 'module.register', + SernEmitter.failure(module, SernError.PluginFailure), + ), onNext: ({ module }) => { emitter.emit('module.register', SernEmitter.success(module)); return module; diff --git a/src/handler/id.ts b/src/handler/id.ts index 92a50ee..041bbcb 100644 --- a/src/handler/id.ts +++ b/src/handler/id.ts @@ -27,7 +27,7 @@ const appBitField = 0b000000011111; * This corresponds to an ApplicationCommandType or ComponentType * TextCommands are 0 as they aren't either or. */ -function apiType(t: CommandType|EventType) { +function apiType(t: CommandType | EventType) { if (t === CommandType.Both || t === CommandType.Modal) return 1; const log = Math.log2(t); return (appBitField & t) !== 0 ? log : log - 2; @@ -38,9 +38,7 @@ function apiType(t: CommandType|EventType) { * A is for any ApplicationCommand. C is for any ComponentCommand * Then, another number generated by apiType function is appended */ -export function uniqueId(t: CommandType|EventType) { +export function uniqueId(t: CommandType | EventType) { const am = (appBitField & t) !== 0 ? 'A' : 'C'; return am + apiType(t); } - - diff --git a/src/handler/sern.ts b/src/handler/sern.ts index 7ffe9bc..8c6d182 100644 --- a/src/handler/sern.ts +++ b/src/handler/sern.ts @@ -23,7 +23,6 @@ import { Wrapper } from '../shared'; */ export function init(wrapper: Wrapper) { - const startTime = performance.now(); const dependencies = useDependencies(); @@ -32,50 +31,47 @@ export function init(wrapper: Wrapper) { const mode = debugModuleLoading(wrapper.mode ?? process.env.MODE); if (wrapper.events !== undefined) { - makeEventsHandler( - dependencies, - getFullPathTree(wrapper.events, mode), - ); + makeEventsHandler(dependencies, getFullPathTree(wrapper.events, mode)); } - startReadyEvent( - dependencies, - getFullPathTree(wrapper.commands, mode) - ).add(() => { + startReadyEvent(dependencies, getFullPathTree(wrapper.commands, mode)).add(() => { const endTime = performance.now(); - logger?.info({ message: `sern: registered all modules in ${((endTime - startTime) / 1000).toFixed(2)} s` }); + logger?.info({ + message: `sern: registered all modules in ${((endTime - startTime) / 1000).toFixed( + 2, + )} s`, + }); }); - const messages$ = makeMessageHandler(dependencies, wrapper.defaultPrefix); const interactions$ = makeInteractionHandler(dependencies); - merge( - messages$, - interactions$ - ).pipe( - catchError(handleError(errorHandler, logger)), - finalize(() => { - logger?.info({ message: 'A stream closed or reached end of lifetime' }); - useContainerRaw() - ?.disposeAll() - .then(() => logger?.info({ message: 'Cleaning container and crashing' })); - }) - ).subscribe(); - + merge(messages$, interactions$) + .pipe( + catchError(handleError(errorHandler, logger)), + finalize(() => { + logger?.info({ message: 'A stream closed or reached end of lifetime' }); + useContainerRaw() + ?.disposeAll() + .then(() => logger?.info({ message: 'Cleaning container and crashing' })); + }), + ) + .subscribe(); } -function debugModuleLoading(mode: string|undefined) { - console.info(`Detected mode: "${mode}"`) - if(mode === undefined) { - console.info("No mode found in process.env, assuming DEV"); +function debugModuleLoading(mode: string | undefined) { + console.info(`Detected mode: "${mode}"`); + if (mode === undefined) { + console.info('No mode found in process.env, assuming DEV'); } - switch(mode) { - case 'PROD': return false; + switch (mode) { + case 'PROD': + return false; case 'DEV': - case undefined: return true; + case undefined: + return true; default: { - console.warn(mode + " is not a valid. Should be PROD or DEV"); + console.warn(mode + ' is not a valid. Should be PROD or DEV'); return false; } } @@ -87,7 +83,7 @@ function useDependencies() { '@sern/errors', '@sern/logger', '@sern/modules', - '@sern/client' + '@sern/client', ); } diff --git a/src/handler/types.ts b/src/handler/types.ts index 2cee378..756e67c 100644 --- a/src/handler/types.ts +++ b/src/handler/types.ts @@ -20,5 +20,5 @@ export interface InitArgs> { export interface ImportPayload { module: T; absPath: string; - [key: string]: unknown + [key: string]: unknown; } diff --git a/src/index.ts b/src/index.ts index 0aa9e78..94f6520 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,11 @@ export * as Sern from './handler/sern'; export * from './core'; -export { commandModule, eventModule, discordEvent, EventExecutable, CommandExecutable } from './handler/commands'; +export { + commandModule, + eventModule, + discordEvent, + EventExecutable, + CommandExecutable, +} from './handler/commands'; export { controller } from './handler/sern'; export type { Wrapper, Args } from './shared'; - diff --git a/src/shared.ts b/src/shared.ts index 977535d..5b1d818 100644 --- a/src/shared.ts +++ b/src/shared.ts @@ -24,21 +24,18 @@ export interface SernEventsMapping { warning: [Payload]; } - export type Awaitable = PromiseLike | T; - export type Deprecated = [never, Message]; - export interface Wrapper { commands: string; defaultPrefix?: string; events?: string; /** - * Overload to enable mode in case developer does not use a .env file. - */ - mode?: 'DEV' | 'PROD' + * Overload to enable mode in case developer does not use a .env file. + */ + mode?: 'DEV' | 'PROD'; /* * @deprecated */ @@ -47,7 +44,6 @@ export interface Wrapper { }; } - // Thanks to @kelsny export type ParseType = { [K in keyof T]: T[K] extends unknown ? [k: K, args: T[K]] : never; @@ -56,4 +52,3 @@ export type ParseType = { export type Args = ParseType<{ text: string[]; slash: SlashOptions }>; export type SlashOptions = Omit; - diff --git a/tsconfig.json b/tsconfig.json index d98f34e..9bb4c50 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,4 +1,3 @@ - { "extends": "./tsconfig-esm.json" } diff --git a/tsup.config.js b/tsup.config.js index b8e9c14..5282735 100644 --- a/tsup.config.js +++ b/tsup.config.js @@ -12,7 +12,7 @@ const shared = { correctVarValueBeforeDeclaration: true, //need this to treeshake esm discord.js empty import annotations: true, }, - dts: false + dts: false, }; export default defineConfig([ { @@ -50,12 +50,12 @@ export default defineConfig([ await writeFile('./dist/cjs/package.json', JSON.stringify({ type: 'commonjs' })); }, ...shared, - }, + }, { dts: { - only: true + only: true, }, entry: ['src/index.ts'], - outDir: 'dist' - } + outDir: 'dist', + }, ]);