feat(sern.ts) Add basic event manager

This commit is contained in:
Jacob Nguyen
2022-03-08 16:36:51 -06:00
parent 84fc853dd6
commit 0dd95be59a
6 changed files with 29 additions and 11 deletions

View File

View File

@@ -1,5 +1,6 @@
import * as Files from './utilities/readFile';
import type {
DiscordEvent,
possibleOutput,
} from '../types/handler';
@@ -17,11 +18,19 @@ 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';
/**
* @class
*/
export class Handler {
export function init( { client, events} : Wrapper) {
if (events !== undefined) eventObserver(client, events);
}
function eventObserver(client: Client, events: DiscordEvent[] ) {
events.forEach( ( [event, cb] ) => {
fromEvent(client, event, cb).subscribe();
});
}
export class Handler {
private wrapper: Wrapper;
private defaultLogger: Logger = new Logger();
/**
@@ -29,8 +38,7 @@ export class Handler {
* @constructor
* @param {Wrapper} wrapper The data that is required to run sern handler
*/
constructor(wrapper: Wrapper) {
constructor(wrapper: Wrapper) {
this.wrapper = wrapper;
this.client
@@ -249,7 +257,7 @@ export class Handler {
* @returns {string} Directory of the commands folder
*/
get commandDklir(): string {
get commandDir(): string {
return this.wrapper.commands;
}

View File

@@ -0,0 +1,5 @@
import Context from './context';
import type Module from './module';
import type Wrapper from './wrapper';
export { Context, Module, Wrapper };

View File

@@ -1,4 +1,5 @@
import type { Client } from 'discord.js';
import type { DiscordEvent } from '../../types/handler';
/**
* An object to be passed into Sern.Handler constructor.
@@ -15,6 +16,7 @@ interface Wrapper {
readonly commands: string;
init?: (handler: Wrapper) => void;
readonly privateServers: { test: boolean; id: string }[];
readonly events? : DiscordEvent[];
}
export default Wrapper;

View File

@@ -1,6 +1,7 @@
import * as Sern from './handler/sern';
import * as Utils from './handler/utilities/utilsExports';
import * as Types from './types/handler';
import * as Structures from './handler/structures/structxports';
module.exports = { Sern, Utils, Types };
export { Sern, Utils, Types };
module.exports = { Sern, Utils, Types, Structures };
export { Sern, Utils, Types, Structures };

View File

@@ -14,6 +14,7 @@ export type Visibility = 'private' | 'public';
// Anything that can be sent in a `<TextChannel>#send` or `<CommandInteraction>#reply`
export type possibleOutput<T = string> = T | (MessagePayload & MessageOptions);
export type execute = Module<unknown>['execute'];
// Thanks @cursorsdottsx
export type ParseType<T> = {
[K in keyof T]: T[K] extends unknown ? [k: K, args: T[K]] : never;
@@ -22,6 +23,7 @@ export type ParseType<T> = {
export type Arg = ParseType<{ text: string[]; slash: SlashOptions }>;
// TypeAlias for interaction.options
export type eventConfig = { [K in keyof ClientEvents] : (...args : ClientEvents[K] ) => Awaitable<void>}[];
export type DiscordEvent =
ParseType< { [K in keyof ClientEvents ] : (...args : ClientEvents[K]) => Awaitable<void> }>;
export type SlashOptions = Omit<CommandInteractionOptionResolver, 'getMessage' | 'getFocused'>;