refactor(*): use enums for payloadtype & fix type warns (#69)

* refactor(*): use enums and fix type warns

* style: pretty pretty prettier
This commit is contained in:
Evo
2022-06-30 11:25:54 +05:30
committed by GitHub
parent 7c97fa9461
commit 9340cf229c
6 changed files with 35 additions and 19 deletions

View File

@@ -13,6 +13,7 @@ import { SernError } from '../structures/errors';
import Context from '../structures/context'; import Context from '../structures/context';
import { controller } from '../sern'; import { controller } from '../sern';
import type { Module } from '../structures/module'; import type { Module } from '../structures/module';
import { PayloadType } from '../structures/enums';
import { import {
isApplicationCommand, isApplicationCommand,
isAutocomplete, isAutocomplete,
@@ -213,10 +214,13 @@ export function onInteractionCreate(wrapper: Wrapper) {
const ePlugArr = await asyncResolveArray(eventPluginRes); const ePlugArr = await asyncResolveArray(eventPluginRes);
if (ePlugArr.every(e => e.ok)) { if (ePlugArr.every(e => e.ok)) {
await execute(); await execute();
wrapper.sernEmitter?.emit('module.activate', { type: 'success', module: mod! }); wrapper.sernEmitter?.emit('module.activate', {
type: PayloadType.Success,
module: mod!,
});
} else { } else {
wrapper.sernEmitter?.emit('module.activate', { wrapper.sernEmitter?.emit('module.activate', {
type: 'failure', type: PayloadType.Failure,
module: mod!, module: mod!,
reason: SernError.PluginFailure, reason: SernError.PluginFailure,
}); });

View File

@@ -6,7 +6,7 @@ import type Wrapper from '../structures/wrapper';
import { fmt } from '../utilities/messageHelpers'; import { fmt } from '../utilities/messageHelpers';
import * as Files from '../utilities/readFile'; import * as Files from '../utilities/readFile';
import { filterCorrectModule, ignoreNonBot } from './observableHandling'; import { filterCorrectModule, ignoreNonBot } from './observableHandling';
import { CommandType } from '../structures/enums'; import { CommandType, PayloadType } from '../structures/enums';
import { SernError } from '../structures/errors'; import { SernError } from '../structures/errors';
import { asyncResolveArray } from '../utilities/asyncResolveArray'; import { asyncResolveArray } from '../utilities/asyncResolveArray';
@@ -54,11 +54,14 @@ export const onMessageCreate = (wrapper: Wrapper) => {
next({ mod, ctx, args, res }) { next({ mod, ctx, args, res }) {
if (res.every(pl => pl.ok)) { if (res.every(pl => pl.ok)) {
Promise.resolve(mod.execute(ctx, args)).then(() => { 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 { } else {
wrapper.sernEmitter?.emit('module.activate', { wrapper.sernEmitter?.emit('module.activate', {
type: 'failure', type: PayloadType.Failure,
module: mod!, module: mod!,
reason: SernError.PluginFailure, reason: SernError.PluginFailure,
}); });

View File

@@ -10,7 +10,7 @@ import type { CommandModule } from '../structures/module';
import { match } from 'ts-pattern'; import { match } from 'ts-pattern';
import { SernError } from '../structures/errors'; import { SernError } from '../structures/errors';
import type { DefinedCommandModule } from '../../types/handler'; import type { DefinedCommandModule } from '../../types/handler';
import { CommandType, PluginType } from '../structures/enums'; import { CommandType, PluginType, PayloadType } from '../structures/enums';
import { errTap } from './observableHandling'; import { errTap } from './observableHandling';
import { processCommandPlugins } from './userDefinedEventsHandling'; import { processCommandPlugins } from './userDefinedEventsHandling';
@@ -22,7 +22,7 @@ export function onReady(wrapper: Wrapper) {
const processCommandFiles$ = Files.buildData<CommandModule>(commands).pipe( const processCommandFiles$ = Files.buildData<CommandModule>(commands).pipe(
errTap(reason => { errTap(reason => {
wrapper.sernEmitter?.emit('module.register', { wrapper.sernEmitter?.emit('module.register', {
type: 'failure', type: PayloadType.Failure,
module: undefined, module: undefined,
reason, reason,
}); });
@@ -73,10 +73,13 @@ export function onReady(wrapper: Wrapper) {
if (res.err) { if (res.err) {
throw Error(SernError.NonValidModuleType); throw Error(SernError.NonValidModuleType);
} }
wrapper.sernEmitter?.emit('module.register', { type: 'success', module: mod }); wrapper.sernEmitter?.emit('module.register', {
type: PayloadType.Success,
module: mod,
});
} else { } else {
wrapper.sernEmitter?.emit('module.register', { wrapper.sernEmitter?.emit('module.register', {
type: 'failure', type: PayloadType.Failure,
module: mod, module: mod,
reason: SernError.PluginFailure, reason: SernError.PluginFailure,
}); });

View File

@@ -4,6 +4,7 @@ import { buildData, ExternalEventEmitters } from '../utilities/readFile';
import { controller } from '../sern'; import { controller } from '../sern';
import type { DefinedCommandModule, DefinedEventModule, SpreadParams } from '../../types/handler'; import type { DefinedCommandModule, DefinedEventModule, SpreadParams } from '../../types/handler';
import type { EventModule } from '../structures/module'; import type { EventModule } from '../structures/module';
import { PayloadType } from '../structures/enums';
import type Wrapper from '../structures/wrapper'; import type Wrapper from '../structures/wrapper';
import { basename } from 'path'; import { basename } from 'path';
import { match } from 'ts-pattern'; import { match } from 'ts-pattern';
@@ -75,7 +76,7 @@ function eventObservable$(
return buildData<EventModule>(eventsDir).pipe( return buildData<EventModule>(eventsDir).pipe(
errTap(reason => errTap(reason =>
sernEmitter?.emit('module.register', { sernEmitter?.emit('module.register', {
type: 'failure', type: PayloadType.Failure,
module: undefined, module: undefined,
reason, reason,
}), }),

View File

@@ -1,7 +1,7 @@
/** /**
* @enum { number }; * @enum { number };
*/ */
enum CommandType { export enum CommandType {
Text = 0b00000000001, Text = 0b00000000001,
Slash = 0b00000000010, Slash = 0b00000000010,
Both = 0b0000011, Both = 0b0000011,
@@ -12,15 +12,19 @@ enum CommandType {
Modal = 0b00001000000, Modal = 0b00001000000,
} }
enum EventType { export enum EventType {
Discord = 0b01, Discord = 0b01,
Sern = 0b10, Sern = 0b10,
External = 0b11, External = 0b11,
} }
enum PluginType { export enum PluginType {
Command = 0b01, Command = 0b01,
Event = 0b10, Event = 0b10,
} }
export { CommandType, PluginType, EventType }; export enum PayloadType {
Success = 'success',
Failure = 'failure',
Warning = 'warning',
}

View File

@@ -1,5 +1,6 @@
import type { CommandInteractionOptionResolver } from 'discord.js'; import type { CommandInteractionOptionResolver } from 'discord.js';
import type { CommandModule, EventModule, Module } from '../handler/structures/module'; import type { CommandModule, EventModule, Module } from '../handler/structures/module';
import type { PayloadType } from '../handler/structures/enums';
export type Nullish<T> = T | undefined | null; export type Nullish<T> = T | undefined | null;
// Thanks to @kelsny // Thanks to @kelsny
@@ -20,9 +21,9 @@ export type DefinitelyDefined<T, K extends keyof T = keyof T> = {
: Required<T>[L]; : Required<T>[L];
} & T; } & T;
type Reconstruct<T> = T extends Omit<infer O, infer _> ? O & Reconstruct<O> : T; export type Reconstruct<T> = T extends Omit<infer O, never> ? O & Reconstruct<O> : T;
type IsOptional<T> = { export type IsOptional<T> = {
[K in keyof T]-?: T[K] extends Required<T>[K] ? false : true; [K in keyof T]-?: T[K] extends Required<T>[K] ? false : true;
}; };
@@ -30,7 +31,7 @@ type IsOptional<T> = {
* Turns a function with a union of array of args into a single union * Turns a function with a union of array of args into a single union
* [ T , V , B ] | [ A ] => T | V | B | A * [ T , V , B ] | [ A ] => T | V | B | A
*/ */
export type SpreadParams<T extends (...args: any) => unknown> = ( export type SpreadParams<T extends (...args: never) => unknown> = (
args: Parameters<T>[number], args: Parameters<T>[number],
) => unknown; ) => unknown;
@@ -42,8 +43,8 @@ export type DefinedModule = DefinitelyDefined<Module, 'name' | 'description'>;
export type DefinedCommandModule = DefinitelyDefined<CommandModule, 'name' | 'description'>; export type DefinedCommandModule = DefinitelyDefined<CommandModule, 'name' | 'description'>;
export type DefinedEventModule = DefinitelyDefined<EventModule, 'name' | 'description'>; export type DefinedEventModule = DefinitelyDefined<EventModule, 'name' | 'description'>;
export type Payload = export type Payload =
| { type: 'success'; module: Module } | { type: PayloadType.Success; module: Module }
| { type: 'failure'; module: Module | undefined; reason: string | Error }; | { type: PayloadType.Failure; module: Module | undefined; reason: string | Error };
export type SernEventsMapping = { export type SernEventsMapping = {
['module.register']: [Payload]; ['module.register']: [Payload];
['module.activate']: [Payload]; ['module.activate']: [Payload];