diff --git a/src/handler/logger.ts b/src/handler/logger.ts index f9a38b8..30ee083 100644 --- a/src/handler/logger.ts +++ b/src/handler/logger.ts @@ -28,9 +28,7 @@ export default class Logger { /** * Utilizes console.table() to print out memory usage of current process. * Optional at startup. - * */ - public tableRam() { console.table( Object.entries(process.memoryUsage()) diff --git a/src/handler/readyEvent.ts b/src/handler/readyEvent.ts new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/handler/readyEvent.ts @@ -0,0 +1 @@ + diff --git a/src/handler/sern.ts b/src/handler/sern.ts index b96892b..9e61999 100644 --- a/src/handler/sern.ts +++ b/src/handler/sern.ts @@ -18,14 +18,29 @@ import { AllTrue } from './utilities/higherOrders'; import type Module from './structures/module'; import Context from './structures/context'; import type Wrapper from './structures/wrapper'; -import { fromEvent } from 'rxjs'; +import { concatMap, first, fromEvent, pipe, tap } from 'rxjs'; +import { SernError } from './structures/errors'; -export function init( { client, events} : Wrapper) { +export function init( wrapper : Wrapper) { + const { events, client, init, commands } = wrapper; if (events !== undefined) eventObserver(client, events); + + fromEvent(client, 'ready') + .pipe( + first(), + tap(() => init?.( wrapper ) ), + concatMap( + pipe( + () => Files.buildData(commands), + ) + ), + ) + .subscribe(console.log); } function eventObserver(client: Client, events: DiscordEvent[] ) { events.forEach( ( [event, cb] ) => { + if (event === 'ready') throw Error(SernError.RESERVED_EVENT); fromEvent(client, event, cb).subscribe(); }); } @@ -48,9 +63,6 @@ export class Handler { **/ .on('ready', async () => { - this.defaultLogger.clear(); - Files.buildData(this) - .then(data => this.registerModules(data)); }) .on('messageCreate', async (message: Message) => { diff --git a/src/handler/structures/errors.ts b/src/handler/structures/errors.ts new file mode 100644 index 0000000..7a09f2a --- /dev/null +++ b/src/handler/structures/errors.ts @@ -0,0 +1,4 @@ +export enum SernError { + RESERVED_EVENT = 'Cannot register the reserved ready event. Please use the init property.' + +} diff --git a/src/handler/utilities/events/readEvent.ts b/src/handler/utilities/events/readEvent.ts new file mode 100644 index 0000000..10e1d11 --- /dev/null +++ b/src/handler/utilities/events/readEvent.ts @@ -0,0 +1,4 @@ +export function registerModules () { + + +} diff --git a/src/handler/utilities/readFile.ts b/src/handler/utilities/readFile.ts index 4392131..8bc6553 100644 --- a/src/handler/utilities/readFile.ts +++ b/src/handler/utilities/readFile.ts @@ -1,5 +1,4 @@ import type { ApplicationCommandOptionData } from 'discord.js'; -import type * as Sern from '../sern'; import type Module from '../structures/module'; import { readdirSync, statSync } from 'fs'; @@ -14,11 +13,11 @@ export const Commands = new Map(); export const Alias = new Map(); // Courtesy @Townsy45 -async function readPath(dir: string, arrayOfFiles: string[] = []): Promise { +function readPath(dir: string, arrayOfFiles: string[] = []): string[] { try { const files = readdirSync(dir); for (const file of files) { - if (statSync(dir + '/' + file).isDirectory()) await readPath(dir + '/' + file, arrayOfFiles); + if (statSync(dir + '/' + file).isDirectory()) readPath(dir + '/' + file, arrayOfFiles); else arrayOfFiles.push(join(dir, '/', file)); } } catch (err) { @@ -36,21 +35,20 @@ export const fmtFileName = (n: string) => n.substring(0, n.length - 3); * @returns {Promise<{ name: string; mod: Module; absPath: string; }[]>} data from command files */ -export async function buildData(handler: Sern.Handler): Promise< +export async function buildData(commandDir: string ): Promise< { name: string; mod: Module; absPath: string; }[] > { - const commandDir = handler.commandDir; return Promise.all( - (await getCommands(commandDir)).map(async (absPath) => { + getCommands(commandDir).map( async (absPath) => { return { name: basename(absPath), mod: (await import(absPath)).default as Module, absPath }; }), ); } -export async function getCommands(dir: string): Promise { +export function getCommands(dir: string): string[] { return readPath(join(process.cwd(), dir)); } diff --git a/src/handler/utilities/readyEvent.ts b/src/handler/utilities/readyEvent.ts new file mode 100644 index 0000000..e69de29