mirror of
https://github.com/sern-handler/handler
synced 2026-06-06 01:16:55 +00:00
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:
@@ -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,
|
||||
});
|
||||
|
||||
@@ -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,
|
||||
});
|
||||
|
||||
@@ -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<CommandModule>(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,
|
||||
});
|
||||
|
||||
@@ -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<EventModule>(eventsDir).pipe(
|
||||
errTap(reason =>
|
||||
sernEmitter?.emit('module.register', {
|
||||
type: 'failure',
|
||||
type: PayloadType.Failure,
|
||||
module: undefined,
|
||||
reason,
|
||||
}),
|
||||
|
||||
@@ -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',
|
||||
}
|
||||
|
||||
@@ -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> = T | undefined | null;
|
||||
|
||||
// Thanks to @kelsny
|
||||
@@ -20,9 +21,9 @@ export type DefinitelyDefined<T, K extends keyof T = keyof T> = {
|
||||
: Required<T>[L];
|
||||
} & 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;
|
||||
};
|
||||
|
||||
@@ -30,7 +31,7 @@ type IsOptional<T> = {
|
||||
* 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<T extends (...args: any) => unknown> = (
|
||||
export type SpreadParams<T extends (...args: never) => unknown> = (
|
||||
args: Parameters<T>[number],
|
||||
) => unknown;
|
||||
|
||||
@@ -42,8 +43,8 @@ export type DefinedModule = DefinitelyDefined<Module, 'name' | 'description'>;
|
||||
export type DefinedCommandModule = DefinitelyDefined<CommandModule, 'name' | 'description'>;
|
||||
export type DefinedEventModule = DefinitelyDefined<EventModule, 'name' | 'description'>;
|
||||
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];
|
||||
|
||||
Reference in New Issue
Block a user