From 9340cf229c5c6f4a1e21d7413cfb5cc3886b19a5 Mon Sep 17 00:00:00 2001 From: Evo <85353424+EvolutionX-10@users.noreply.github.com> Date: Thu, 30 Jun 2022 11:25:54 +0530 Subject: [PATCH] refactor(*): use enums for payloadtype & fix type warns (#69) * refactor(*): use enums and fix type warns * style: pretty pretty prettier --- src/handler/events/interactionCreate.ts | 8 ++++++-- src/handler/events/messageEvent.ts | 9 ++++++--- src/handler/events/readyEvent.ts | 11 +++++++---- src/handler/events/userDefinedEventsHandling.ts | 3 ++- src/handler/structures/enums.ts | 12 ++++++++---- src/types/handler.ts | 11 ++++++----- 6 files changed, 35 insertions(+), 19 deletions(-) diff --git a/src/handler/events/interactionCreate.ts b/src/handler/events/interactionCreate.ts index 29e4a32..c5c9a96 100644 --- a/src/handler/events/interactionCreate.ts +++ b/src/handler/events/interactionCreate.ts @@ -13,6 +13,7 @@ import { SernError } from '../structures/errors'; import Context from '../structures/context'; import { controller } from '../sern'; import type { Module } from '../structures/module'; +import { PayloadType } from '../structures/enums'; import { isApplicationCommand, isAutocomplete, @@ -213,10 +214,13 @@ export function onInteractionCreate(wrapper: Wrapper) { const ePlugArr = await asyncResolveArray(eventPluginRes); if (ePlugArr.every(e => e.ok)) { await execute(); - wrapper.sernEmitter?.emit('module.activate', { type: 'success', module: mod! }); + wrapper.sernEmitter?.emit('module.activate', { + type: PayloadType.Success, + module: mod!, + }); } else { wrapper.sernEmitter?.emit('module.activate', { - type: 'failure', + type: PayloadType.Failure, module: mod!, reason: SernError.PluginFailure, }); diff --git a/src/handler/events/messageEvent.ts b/src/handler/events/messageEvent.ts index e921fad..f798166 100644 --- a/src/handler/events/messageEvent.ts +++ b/src/handler/events/messageEvent.ts @@ -6,7 +6,7 @@ import type Wrapper from '../structures/wrapper'; import { fmt } from '../utilities/messageHelpers'; import * as Files from '../utilities/readFile'; import { filterCorrectModule, ignoreNonBot } from './observableHandling'; -import { CommandType } from '../structures/enums'; +import { CommandType, PayloadType } from '../structures/enums'; import { SernError } from '../structures/errors'; import { asyncResolveArray } from '../utilities/asyncResolveArray'; @@ -54,11 +54,14 @@ export const onMessageCreate = (wrapper: Wrapper) => { next({ mod, ctx, args, res }) { if (res.every(pl => pl.ok)) { Promise.resolve(mod.execute(ctx, args)).then(() => { - wrapper.sernEmitter?.emit('module.activate', { type: 'success', module: mod! }); + wrapper.sernEmitter?.emit('module.activate', { + type: PayloadType.Success, + module: mod!, + }); }); } else { wrapper.sernEmitter?.emit('module.activate', { - type: 'failure', + type: PayloadType.Failure, module: mod!, reason: SernError.PluginFailure, }); diff --git a/src/handler/events/readyEvent.ts b/src/handler/events/readyEvent.ts index b5bcfa2..ca04ae4 100644 --- a/src/handler/events/readyEvent.ts +++ b/src/handler/events/readyEvent.ts @@ -10,7 +10,7 @@ import type { CommandModule } from '../structures/module'; import { match } from 'ts-pattern'; import { SernError } from '../structures/errors'; import type { DefinedCommandModule } from '../../types/handler'; -import { CommandType, PluginType } from '../structures/enums'; +import { CommandType, PluginType, PayloadType } from '../structures/enums'; import { errTap } from './observableHandling'; import { processCommandPlugins } from './userDefinedEventsHandling'; @@ -22,7 +22,7 @@ export function onReady(wrapper: Wrapper) { const processCommandFiles$ = Files.buildData(commands).pipe( errTap(reason => { wrapper.sernEmitter?.emit('module.register', { - type: 'failure', + type: PayloadType.Failure, module: undefined, reason, }); @@ -73,10 +73,13 @@ export function onReady(wrapper: Wrapper) { if (res.err) { throw Error(SernError.NonValidModuleType); } - wrapper.sernEmitter?.emit('module.register', { type: 'success', module: mod }); + wrapper.sernEmitter?.emit('module.register', { + type: PayloadType.Success, + module: mod, + }); } else { wrapper.sernEmitter?.emit('module.register', { - type: 'failure', + type: PayloadType.Failure, module: mod, reason: SernError.PluginFailure, }); diff --git a/src/handler/events/userDefinedEventsHandling.ts b/src/handler/events/userDefinedEventsHandling.ts index 0ceb0b8..f96dc75 100644 --- a/src/handler/events/userDefinedEventsHandling.ts +++ b/src/handler/events/userDefinedEventsHandling.ts @@ -4,6 +4,7 @@ import { buildData, ExternalEventEmitters } from '../utilities/readFile'; import { controller } from '../sern'; import type { DefinedCommandModule, DefinedEventModule, SpreadParams } from '../../types/handler'; import type { EventModule } from '../structures/module'; +import { PayloadType } from '../structures/enums'; import type Wrapper from '../structures/wrapper'; import { basename } from 'path'; import { match } from 'ts-pattern'; @@ -75,7 +76,7 @@ function eventObservable$( return buildData(eventsDir).pipe( errTap(reason => sernEmitter?.emit('module.register', { - type: 'failure', + type: PayloadType.Failure, module: undefined, reason, }), diff --git a/src/handler/structures/enums.ts b/src/handler/structures/enums.ts index 4d3f053..282eba1 100644 --- a/src/handler/structures/enums.ts +++ b/src/handler/structures/enums.ts @@ -1,7 +1,7 @@ /** * @enum { number }; */ -enum CommandType { +export enum CommandType { Text = 0b00000000001, Slash = 0b00000000010, Both = 0b0000011, @@ -12,15 +12,19 @@ enum CommandType { Modal = 0b00001000000, } -enum EventType { +export enum EventType { Discord = 0b01, Sern = 0b10, External = 0b11, } -enum PluginType { +export enum PluginType { Command = 0b01, Event = 0b10, } -export { CommandType, PluginType, EventType }; +export enum PayloadType { + Success = 'success', + Failure = 'failure', + Warning = 'warning', +} diff --git a/src/types/handler.ts b/src/types/handler.ts index d10b526..88436f8 100644 --- a/src/types/handler.ts +++ b/src/types/handler.ts @@ -1,5 +1,6 @@ import type { CommandInteractionOptionResolver } from 'discord.js'; import type { CommandModule, EventModule, Module } from '../handler/structures/module'; +import type { PayloadType } from '../handler/structures/enums'; export type Nullish = T | undefined | null; // Thanks to @kelsny @@ -20,9 +21,9 @@ export type DefinitelyDefined = { : Required[L]; } & T; -type Reconstruct = T extends Omit ? O & Reconstruct : T; +export type Reconstruct = T extends Omit ? O & Reconstruct : T; -type IsOptional = { +export type IsOptional = { [K in keyof T]-?: T[K] extends Required[K] ? false : true; }; @@ -30,7 +31,7 @@ type IsOptional = { * Turns a function with a union of array of args into a single union * [ T , V , B ] | [ A ] => T | V | B | A */ -export type SpreadParams unknown> = ( +export type SpreadParams unknown> = ( args: Parameters[number], ) => unknown; @@ -42,8 +43,8 @@ export type DefinedModule = DefinitelyDefined; export type DefinedCommandModule = DefinitelyDefined; export type DefinedEventModule = DefinitelyDefined; export type Payload = - | { type: 'success'; module: Module } - | { type: 'failure'; module: Module | undefined; reason: string | Error }; + | { type: PayloadType.Success; module: Module } + | { type: PayloadType.Failure; module: Module | undefined; reason: string | Error }; export type SernEventsMapping = { ['module.register']: [Payload]; ['module.activate']: [Payload];