feat: loading strategy slightly changed, does not throw error on finding no module in file

This commit is contained in:
Jacob Nguyen
2022-06-13 01:17:47 -05:00
parent 4f7f3b6212
commit 120c527b34
3 changed files with 30 additions and 16 deletions

View File

@@ -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,
});

View File

@@ -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<T>(...args: Option<T>[]): Nullish<T> {
for (const op of args) {

View File

@@ -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<string, Module>();
@@ -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 = <Module>require(absPath).default;
return { mod, absPath };
const mod = <Module | undefined>require(absPath).default;
if (mod !== undefined) {
return Ok({ mod, absPath });
} else return Err(SernError.UndefinedModule as const);
}),
);
}