feat: Adding sern event listeners, overriding and typing methods

This commit is contained in:
Jacob Nguyen
2022-05-19 22:29:45 -05:00
parent 66b9f51fa7
commit 115d1a49b5
6 changed files with 33 additions and 6 deletions

View File

@@ -1,5 +1,4 @@
import type {
Awaitable,
CommandInteraction,
Interaction,
MessageComponentInteraction,
@@ -28,7 +27,6 @@ function applicationCommandHandler(mod: Module | undefined, interaction: Command
const mod$ = <T extends CommandType>(cmdTy : T) => of(mod).pipe(
filterCorrectModule(cmdTy)
);
return match(interaction)
.when(isChatInputCommand, i => {
const ctx = Context.wrap(i);
@@ -133,14 +131,16 @@ export function onInteractionCreate (wrapper: Wrapper) {
ePlugArr.push(res as Awaited<Result<void, void>>);
}
if(ePlugArr.every(e => e.ok)) {
wrapper.sernEmitter?.emit('sern.command.success', [mod!]);
await execute();
} else {
wrapper.sernEmitter?.emit('sern.command.fail', [mod!]);
console.log(ePlugArr);
console.log(mod, 'failed');
}
},
error(err) {
console.log(err);
wrapper.sernEmitter?.emit('sern.error', err);
}
});
}

View File

@@ -14,7 +14,7 @@ export function filterCorrectModule<T extends keyof ModuleDefs>(cmdType: T) {
return throwError(() => SernError.UndefinedModule);
}
if (correctModuleType(mod, cmdType)) {
subscriber.next(mod);
subscriber.next(mod!);
} else {
return throwError(() => SernError.MismatchModule);
}

View File

@@ -0,0 +1,25 @@
import { EventEmitter } from 'events';
import type { Module } from './structures/module';
import type { Nullish } from '../types/handler';
type SernEventsMapping = {
['sern.command.registered'] : [ Module ];
['sern.command.success'] : [ Module ];
['sern.command.fail'] : [ Nullish<Module> ];
['sern.error'] : [ Error ];
}
export default class SernEmitter extends EventEmitter {
public override on<T extends keyof SernEventsMapping>(eventName: T, listener: (...args: SernEventsMapping[T][]) => void): this {
return super.on(eventName,listener);
}
public override once<T extends keyof SernEventsMapping>(eventName: T, listener: (...args: SernEventsMapping[T][]) => void): this {
return super.once(eventName,listener);
}
public override emit<T extends keyof SernEventsMapping>(eventName: T, args : SernEventsMapping[T]): boolean {
return super.emit(eventName, ...args);
}
}

View File

@@ -6,4 +6,5 @@ export enum SernError {
MismatchModule = `A module type mismatched with event emitted!`,
NotImplemented = 'This feature has not yet been implemented',
NotSupportedInteraction = `This interaction is not supported.`,
NotValidEventName = `Supplied a non valid event name`,
}

View File

@@ -1,5 +1,6 @@
import type { Client } from 'discord.js';
import type { DiscordEvent, EventEmitterRegister } from '../../types/handler';
import type SernEmitter from '../sernEmitter';
/**
* An object to be passed into Sern.Handler constructor.
@@ -7,11 +8,11 @@ import type { DiscordEvent, EventEmitterRegister } from '../../types/handler';
* @property {readonly Client} client
* @property {readonly string} defaultPrefix
* @property {readonly string} commands
* @prop {(handler : Handler) => void)} init
* @prop { readonly DiscordEvent[] } events
*/
interface Wrapper {
readonly client: Client;
readonly sernEmitter? : SernEmitter
readonly defaultPrefix?: string;
readonly commands: string;
readonly events?: (DiscordEvent | EventEmitterRegister)[];

View File

@@ -1,4 +1,4 @@
export * as Sern from './handler/sern';
export * from './types/handler';
export * from './handler/structures/structxports';
export * from './handler/plugins/plugin';
export * from './handler/plugins/plugin';