From 0dd95be59ac432e1dddaa81476189f07b01dd37f Mon Sep 17 00:00:00 2001 From: Jacob Nguyen <76754747+jacoobes@users.noreply.github.com> Date: Tue, 8 Mar 2022 16:36:51 -0600 Subject: [PATCH] feat(sern.ts) Add basic event manager --- src/handler/messageEvent.ts | 0 src/handler/sern.ts | 22 +++++++++++++++------- src/handler/structures/structxports.ts | 5 +++++ src/handler/structures/wrapper.ts | 2 ++ src/index.ts | 5 +++-- src/types/handler.ts | 6 ++++-- 6 files changed, 29 insertions(+), 11 deletions(-) create mode 100644 src/handler/messageEvent.ts create mode 100644 src/handler/structures/structxports.ts diff --git a/src/handler/messageEvent.ts b/src/handler/messageEvent.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/handler/sern.ts b/src/handler/sern.ts index 75dd55b..b96892b 100644 --- a/src/handler/sern.ts +++ b/src/handler/sern.ts @@ -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; } diff --git a/src/handler/structures/structxports.ts b/src/handler/structures/structxports.ts new file mode 100644 index 0000000..c2b10ec --- /dev/null +++ b/src/handler/structures/structxports.ts @@ -0,0 +1,5 @@ +import Context from './context'; +import type Module from './module'; +import type Wrapper from './wrapper'; + +export { Context, Module, Wrapper }; diff --git a/src/handler/structures/wrapper.ts b/src/handler/structures/wrapper.ts index 1bdb189..8552128 100644 --- a/src/handler/structures/wrapper.ts +++ b/src/handler/structures/wrapper.ts @@ -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; diff --git a/src/index.ts b/src/index.ts index bb809ac..ed35363 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 }; diff --git a/src/types/handler.ts b/src/types/handler.ts index 223658a..71a576b 100644 --- a/src/types/handler.ts +++ b/src/types/handler.ts @@ -14,6 +14,7 @@ export type Visibility = 'private' | 'public'; // Anything that can be sent in a `#send` or `#reply` export type possibleOutput = T | (MessagePayload & MessageOptions); export type execute = Module['execute']; + // Thanks @cursorsdottsx export type ParseType = { [K in keyof T]: T[K] extends unknown ? [k: K, args: T[K]] : never; @@ -22,6 +23,7 @@ export type ParseType = { export type Arg = ParseType<{ text: string[]; slash: SlashOptions }>; -// TypeAlias for interaction.options -export type eventConfig = { [K in keyof ClientEvents] : (...args : ClientEvents[K] ) => Awaitable}[]; +export type DiscordEvent = + ParseType< { [K in keyof ClientEvents ] : (...args : ClientEvents[K]) => Awaitable }>; + export type SlashOptions = Omit;