From 120c527b343656cc27e276a36143872d3e9dc58f Mon Sep 17 00:00:00 2001 From: Jacob Nguyen <76754747+jacoobes@users.noreply.github.com> Date: Mon, 13 Jun 2022 01:17:47 -0500 Subject: [PATCH] feat: loading strategy slightly changed, does not throw error on finding no module in file --- src/handler/events/readyEvent.ts | 19 ++++++++++++------- src/handler/structures/context.ts | 3 +-- src/handler/utilities/readFile.ts | 24 +++++++++++++++++------- 3 files changed, 30 insertions(+), 16 deletions(-) diff --git a/src/handler/events/readyEvent.ts b/src/handler/events/readyEvent.ts index e9c0e78..57ac4d8 100644 --- a/src/handler/events/readyEvent.ts +++ b/src/handler/events/readyEvent.ts @@ -23,11 +23,20 @@ import { match } from 'ts-pattern'; import { SernError } from '../structures/errors'; import type { DefinitelyDefined } from '../../types/handler'; import { CommandType, PluginType } from '../structures/enums'; +import { elseMap, elseMapTo, resultMap } from 'ts-results/rxjs-operators'; +import { errTap } from './observableHandling'; export const onReady = (wrapper: Wrapper) => { const { client, commands } = wrapper; const ready$ = fromEvent(client, 'ready').pipe(take(1), skip(1)); const processCommandFiles$ = Files.buildData(commands).pipe( + errTap(reason => { + wrapper.sernEmitter?.emit('module.register', { + type: 'failure', + module: undefined, + reason, + }); + }), map(({ mod, absPath }) => { return { name: mod?.name ?? Files.fmtFileName(basename(absPath)), @@ -83,16 +92,12 @@ export const onReady = (wrapper: Wrapper) => { if (loadedPluginsCorrectly) { const res = registerModule(mod); if (res.err) { - throw Error( - SernError.NonValidModuleType + - ', or loading modules was handled incorrectly. ' + - 'Check commands path and command files!', - ); + throw Error(SernError.NonValidModuleType); } - wrapper.sernEmitter?.emit('module.register', { success: true, module: mod }); + wrapper.sernEmitter?.emit('module.register', { type: 'success', module: mod }); } else { wrapper.sernEmitter?.emit('module.register', { - success: false, + type: 'failure', module: mod, reason: SernError.PluginFailure, }); diff --git a/src/handler/structures/context.ts b/src/handler/structures/context.ts index 11578cf..18be5d5 100644 --- a/src/handler/structures/context.ts +++ b/src/handler/structures/context.ts @@ -12,10 +12,9 @@ import type { User, } from 'discord.js'; import { None, Option, Some } from 'ts-results'; -import type { ConformedEditOptions, Nullish } from '../../types/handler'; +import type { Nullish } from '../../types/handler'; import { ExternallyUsed } from '../utilities/externallyUsed'; import { SernError } from './errors'; -import { MessagePayload } from 'discord.js'; function firstSome(...args: Option[]): Nullish { for (const op of args) { diff --git a/src/handler/utilities/readFile.ts b/src/handler/utilities/readFile.ts index 47dbdff..d7591fe 100644 --- a/src/handler/utilities/readFile.ts +++ b/src/handler/utilities/readFile.ts @@ -1,8 +1,11 @@ import { ApplicationCommandType, ComponentType } from 'discord.js'; import { readdirSync, statSync } from 'fs'; import { join } from 'path'; -import { from, Observable } from 'rxjs'; +import { from, Observable, throwError } from 'rxjs'; import type { Module } from '../structures/module'; +import { SernError } from '../structures/errors'; +import type { Result } from 'ts-results'; +import { Err, Ok } from 'ts-results'; //Maybe move this? this probably doesnt belong in utlities/ export const BothCommands = new Map(); @@ -45,15 +48,22 @@ export const fmtFileName = (n: string) => n.substring(0, n.length - 3); * @param commandDir */ -export function buildData(commandDir: string): Observable<{ - mod: Module; - absPath: string; -}> { +export function buildData(commandDir: string): Observable< + Result< + { + mod: Module; + absPath: string; + }, + SernError.UndefinedModule + > +> { return from( getCommands(commandDir).map(absPath => { // eslint-disable-next-line @typescript-eslint/no-var-requires - const mod = require(absPath).default; - return { mod, absPath }; + const mod = require(absPath).default; + if (mod !== undefined) { + return Ok({ mod, absPath }); + } else return Err(SernError.UndefinedModule as const); }), ); }