feat(handler) new ready event handling

Also added new enum errors,
removed useless promise fn sign,
and more
This commit is contained in:
Jacob Nguyen
2022-03-09 00:44:29 -06:00
parent 9ce1314133
commit 08f593792e
7 changed files with 31 additions and 14 deletions

View File

@@ -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())

View File

@@ -0,0 +1 @@

View File

@@ -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) => {

View File

@@ -0,0 +1,4 @@
export enum SernError {
RESERVED_EVENT = 'Cannot register the reserved ready event. Please use the init property.'
}

View File

@@ -0,0 +1,4 @@
export function registerModules () {
}

View File

@@ -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<string, CommandVal>();
export const Alias = new Map<string, CommandVal>();
// Courtesy @Townsy45
async function readPath(dir: string, arrayOfFiles: string[] = []): Promise<string[]> {
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<unknown>; 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<unknown>;
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<unknown>, absPath };
}),
);
}
export async function getCommands(dir: string): Promise<string[]> {
export function getCommands(dir: string): string[] {
return readPath(join(process.cwd(), dir));
}

View File