mirror of
https://github.com/sern-handler/handler
synced 2026-06-18 05:42:15 +00:00
feat(handler) new ready event handling
Also added new enum errors, removed useless promise fn sign, and more
This commit is contained in:
@@ -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())
|
||||
|
||||
1
src/handler/readyEvent.ts
Normal file
1
src/handler/readyEvent.ts
Normal file
@@ -0,0 +1 @@
|
||||
|
||||
@@ -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) => {
|
||||
|
||||
4
src/handler/structures/errors.ts
Normal file
4
src/handler/structures/errors.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
export enum SernError {
|
||||
RESERVED_EVENT = 'Cannot register the reserved ready event. Please use the init property.'
|
||||
|
||||
}
|
||||
4
src/handler/utilities/events/readEvent.ts
Normal file
4
src/handler/utilities/events/readEvent.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
export function registerModules () {
|
||||
|
||||
|
||||
}
|
||||
@@ -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));
|
||||
}
|
||||
|
||||
0
src/handler/utilities/readyEvent.ts
Normal file
0
src/handler/utilities/readyEvent.ts
Normal file
Reference in New Issue
Block a user