From 2d2880095344d33f79b32e8a8187a17aedd295ec Mon Sep 17 00:00:00 2001 From: Jacob Nguyen <76754747+jacoobes@users.noreply.github.com> Date: Thu, 19 May 2022 22:54:46 -0500 Subject: [PATCH] style: Run prettier --- src/handler/events/interactionCreate.ts | 126 +++++++++++++---------- src/handler/events/messageEvent.ts | 27 ++--- src/handler/events/observableHandling.ts | 2 +- src/handler/events/readyEvent.ts | 18 ++-- src/handler/plugins/plugin.ts | 16 +-- src/handler/sern.ts | 12 +-- src/handler/sernEmitter.ts | 21 ++-- src/handler/structures/context.ts | 22 ++-- src/handler/structures/module.ts | 7 +- src/handler/structures/structxports.ts | 1 + src/handler/structures/wrapper.ts | 2 +- src/handler/utilities/markup.ts | 8 +- src/handler/utilities/predicates.ts | 15 +-- src/types/handler.ts | 3 +- 14 files changed, 154 insertions(+), 126 deletions(-) diff --git a/src/handler/events/interactionCreate.ts b/src/handler/events/interactionCreate.ts index 2f7a4e0..2a54b90 100644 --- a/src/handler/events/interactionCreate.ts +++ b/src/handler/events/interactionCreate.ts @@ -1,9 +1,4 @@ -import type { - CommandInteraction, - Interaction, - MessageComponentInteraction, - SelectMenuInteraction, -} from 'discord.js'; +import type { CommandInteraction, Interaction, MessageComponentInteraction, SelectMenuInteraction } from 'discord.js'; import { concatMap, fromEvent, map, Observable, of, throwError } from 'rxjs'; import type Wrapper from '../structures/wrapper'; import * as Files from '../utilities/readFile'; @@ -15,7 +10,8 @@ import type { Module } from '../structures/module'; import { isButton, isChatInputCommand, - isMessageCtxMenuCmd, isPromise, + isMessageCtxMenuCmd, + isPromise, isSelectMenu, isUserContextMenuCmd, } from '../utilities/predicates'; @@ -24,34 +20,42 @@ import { CommandType } from '../structures/enums'; import type { Result } from 'ts-results'; function applicationCommandHandler(mod: Module | undefined, interaction: CommandInteraction) { - const mod$ = (cmdTy : T) => of(mod).pipe( - filterCorrectModule(cmdTy) + const mod$ = (cmdTy: T) => of(mod).pipe( + filterCorrectModule(cmdTy), ); return match(interaction) .when(isChatInputCommand, i => { - const ctx = Context.wrap(i); - //SUPPORT COMMANDTYPE.BOTH - return mod$(CommandType.Slash).pipe( - concatMap(m => { - return of(m.onEvent?.map(e => e.execute( - [ctx, ['slash', i.options]], - controller - )) ?? []).pipe(map(res => ({ mod, res, execute() { return m.execute(ctx, ['slash', i.options]); } }) )); - }), - ); + const ctx = Context.wrap(i); + //SUPPORT COMMANDTYPE.BOTH + return mod$(CommandType.Slash).pipe( + concatMap(m => { + return of(m.onEvent?.map(e => e.execute( + [ctx, ['slash', i.options]], + controller, + )) ?? []).pipe(map(res => ({ + mod, res, execute() { + return m.execute(ctx, ['slash', i.options]); + }, + }))); + }), + ); }, ) //Todo: refactor so that we dont have to have two separate branches. They're near identical!! //Only thing that differs is type of interaction .when(isMessageCtxMenuCmd, ctx => { - return mod$(CommandType.MenuMsg).pipe( - concatMap(m => { - return of(m.onEvent?.map(e => e.execute( - [ctx], - controller - )) ?? []).pipe(map(res => ({ mod, res, execute() { return m.execute(ctx); } }) )); - }), - ); + return mod$(CommandType.MenuMsg).pipe( + concatMap(m => { + return of(m.onEvent?.map(e => e.execute( + [ctx], + controller, + )) ?? []).pipe(map(res => ({ + mod, res, execute() { + return m.execute(ctx); + }, + }))); + }), + ); }, ) .when(isUserContextMenuCmd, ctx => { @@ -59,8 +63,12 @@ function applicationCommandHandler(mod: Module | undefined, interaction: Command concatMap(m => { return of(m.onEvent?.map(e => e.execute( [ctx], - controller - )) ?? []).pipe(map(res => ({ mod, res, execute() { return m.execute(ctx); } }) )); + controller, + )) ?? []).pipe(map(res => ({ + mod, res, execute() { + return m.execute(ctx); + }, + }))); }), ); }) @@ -72,7 +80,7 @@ function messageComponentInteractionHandler( interaction: MessageComponentInteraction, ) { - const mod$ = (ty : T) => of(mod).pipe( filterCorrectModule(ty)); + const mod$ = (ty: T) => of(mod).pipe(filterCorrectModule(ty)); //Todo: refactor so that we dont have to have two separate branches. They're near identical!! //Only thing that differs is type of interaction return match(interaction) @@ -81,25 +89,33 @@ function messageComponentInteractionHandler( concatMap(m => { return of(m.onEvent?.map(e => e.execute( [ctx], - controller - )) ?? []).pipe(map(res => ({ mod, res, execute() { return m.execute(ctx); } }) )); + controller, + )) ?? []).pipe(map(res => ({ + mod, res, execute() { + return m.execute(ctx); + }, + }))); }), ); }) .when(isSelectMenu, (ctx: SelectMenuInteraction) => { - return mod$(CommandType.MenuSelect).pipe( + return mod$(CommandType.MenuSelect).pipe( concatMap(m => { return of(m.onEvent?.map(e => e.execute( [ctx], - controller - )) ?? []).pipe(map(res => ({ mod, res, execute() { return m.execute(ctx); } }) )); + controller, + )) ?? []).pipe(map(res => ({ + mod, res, execute() { + return m.execute(ctx); + }, + }))); }), ); }) .otherwise(() => throwError(() => SernError.NotSupportedInteraction)); } -export function onInteractionCreate (wrapper: Wrapper) { +export function onInteractionCreate(wrapper: Wrapper) { const { client } = wrapper; const interactionEvent$ = >fromEvent(client, 'interactionCreate'); @@ -122,23 +138,27 @@ export function onInteractionCreate (wrapper: Wrapper) { } else return throwError(() => SernError.NotSupportedInteraction); }), ).subscribe({ - async next({ mod, res : eventPluginRes, execute}) { - const ePlugArr : Result[] = []; - for await ( const res of eventPluginRes) { - if(isPromise(res)) { - ePlugArr.push(res); - } - ePlugArr.push(res as Awaited>); + async next({ mod, res: eventPluginRes, execute }) { + const ePlugArr: Result[] = []; + for await (const res of eventPluginRes) { + if (isPromise(res)) { + ePlugArr.push(res); } - if(ePlugArr.every(e => e.ok)) { - await execute(); - wrapper.sernEmitter?.emit('module.activate', { success: true, module: mod! }); - } else { - wrapper.sernEmitter?.emit('module.activate', { success: false, module: mod!, reason : SernError.PluginFailure }); - } - }, - error(err) { - wrapper.sernEmitter?.emit('error', err); + ePlugArr.push(res as Awaited>); } - }); + if (ePlugArr.every(e => e.ok)) { + await execute(); + wrapper.sernEmitter?.emit('module.activate', { success: true, module: mod! }); + } else { + wrapper.sernEmitter?.emit('module.activate', { + success: false, + module: mod!, + reason: SernError.PluginFailure, + }); + } + }, + error(err) { + wrapper.sernEmitter?.emit('error', err); + }, + }); } \ No newline at end of file diff --git a/src/handler/events/messageEvent.ts b/src/handler/events/messageEvent.ts index 278e345..b10505a 100644 --- a/src/handler/events/messageEvent.ts +++ b/src/handler/events/messageEvent.ts @@ -1,6 +1,5 @@ import type { Message } from 'discord.js'; -import { concatMap, filter, from, fromEvent, map, Observable, of } from 'rxjs'; -import { Err } from 'ts-results'; +import { concatMap, from, fromEvent, map, Observable, of } from 'rxjs'; import type { Args } from '../..'; import { controller } from '../sern'; import Context from '../structures/context'; @@ -52,17 +51,21 @@ export const onMessageCreate = (wrapper: Wrapper) => { ); processEventPlugins$.subscribe({ - next({ mod, ctx, args, res }) { + next({ mod, ctx, args, res }) { if (res.every(pl => pl.ok)) { Promise.resolve(mod.execute(ctx, args)).then(() => { wrapper.sernEmitter?.emit('module.activate', { success: true, module: mod! }); - }); - } else { - wrapper.sernEmitter?.emit('module.activate', { success: false, module: mod!, reason: SernError.PluginFailure }); - } - }, - error(e) { - wrapper.sernEmitter?.emit('error', e); + }); + } else { + wrapper.sernEmitter?.emit('module.activate', { + success: false, + module: mod!, + reason: SernError.PluginFailure, + }); } - }); - }; + }, + error(e) { + wrapper.sernEmitter?.emit('error', e); + }, + }); +}; diff --git a/src/handler/events/observableHandling.ts b/src/handler/events/observableHandling.ts index ddc301b..afabebc 100644 --- a/src/handler/events/observableHandling.ts +++ b/src/handler/events/observableHandling.ts @@ -6,7 +6,7 @@ import type { Module, ModuleDefs } from '../structures/module'; import { correctModuleType } from '../utilities/predicates'; export function filterCorrectModule(cmdType: T) { - return (src: Observable) => + return (src: Observable) => new Observable(subscriber => { return src.subscribe({ next(mod) { diff --git a/src/handler/events/readyEvent.ts b/src/handler/events/readyEvent.ts index 76d256d..1c8e22f 100644 --- a/src/handler/events/readyEvent.ts +++ b/src/handler/events/readyEvent.ts @@ -4,11 +4,11 @@ import * as Files from '../utilities/readFile'; import type Wrapper from '../structures/wrapper'; import { controller } from '../sern'; import type { Result } from 'ts-results'; +import { Err, Ok } from 'ts-results'; import type { Awaitable } from 'discord.js'; +import { ApplicationCommandType, ComponentType } from 'discord.js'; import type { Module } from '../structures/module'; import { match } from 'ts-pattern'; -import { ApplicationCommandType, ComponentType } from 'discord.js'; -import { Err, Ok } from 'ts-results'; import { SernError } from '../structures/errors'; import type { DefinitelyDefined } from '../../types/handler'; import { CommandType, PluginType } from '../structures/enums'; @@ -40,7 +40,7 @@ export const onReady = (wrapper: Wrapper) => { ( concat(ready$, processPlugins$) as Observable<{ - mod: DefinitelyDefined; + mod: DefinitelyDefined; cmdPluginsRes: { execute: Awaitable>; type: PluginType.Command; @@ -60,17 +60,21 @@ export const onReady = (wrapper: Wrapper) => { const loadedPluginsCorrectly = cmdPluginsRes.every(res => res.execute.ok); if (loadedPluginsCorrectly) { const res = registerModule(mod); - if(res.err) { + if (res.err) { throw Error(SernError.NonValidModuleType); } - wrapper.sernEmitter?.emit('module.register', { success : true, module : mod } ); + wrapper.sernEmitter?.emit('module.register', { success: true, module: mod }); } else { - wrapper.sernEmitter?.emit('module.register', { success : false, module : mod, reason : SernError.PluginFailure } ); + wrapper.sernEmitter?.emit('module.register', { + success: false, + module: mod, + reason: SernError.PluginFailure, + }); } }); }; -function registerModule(mod: DefinitelyDefined) : Result { +function registerModule(mod: DefinitelyDefined): Result { const name = mod.name; return match(mod) .with({ type: CommandType.Text }, mod => { diff --git a/src/handler/plugins/plugin.ts b/src/handler/plugins/plugin.ts index 86b9eb4..c13ce45 100644 --- a/src/handler/plugins/plugin.ts +++ b/src/handler/plugins/plugin.ts @@ -17,7 +17,6 @@ import type { Module, Override } from '../..'; import type { BaseModule, ModuleDefs } from '../structures/module'; import type { PluginType } from '../structures/enums'; import type { ValueOf } from 'ts-pattern/dist/types/helpers'; -import type { CommandType } from '../..'; export interface Controller { @@ -26,7 +25,7 @@ export interface Controller { } type BasePlugin = Override; export type CommandPlugin = Override = Override, controller: Controller) => Awaitable>; }>; -export function plugins(...plug: CommandPlugin[]) : CommandPlugin[]; -export function plugins(...plug: EventPlugin[]) : EventPlugin[]; +export function plugins(...plug: CommandPlugin[]): CommandPlugin[]; +export function plugins(...plug: EventPlugin[]): EventPlugin[]; export function plugins(...plug: EventPlugin[] | CommandPlugin[]) { return plug; } type ModuleNoPlugins = ValueOf<{ - [T in keyof ModuleDefs] : Omit + [T in keyof ModuleDefs]: Omit }> + //TODO: I WANT BETTER TYPINGS AHHHHHHHHHHHHHHH export function sernModule( - plugins: (CommandPlugin)[], mod : ModuleNoPlugins -) : Module { + plugins: (CommandPlugin)[], mod: ModuleNoPlugins, +): Module { return { plugins, - ...mod + ...mod, }; } \ No newline at end of file diff --git a/src/handler/sern.ts b/src/handler/sern.ts index cf581ea..bea58df 100644 --- a/src/handler/sern.ts +++ b/src/handler/sern.ts @@ -15,9 +15,9 @@ import { isDiscordEvent } from './utilities/predicates'; export function init(wrapper: Wrapper) { const { events, client } = wrapper; - if (events !== undefined) { - eventObserver(client, events); - } + if (events !== undefined) { + eventObserver(client, events); + } onReady(wrapper); onMessageCreate(wrapper); onInteractionCreate(wrapper); @@ -25,10 +25,10 @@ export function init(wrapper: Wrapper) { function eventObserver(client: Client, events: (DiscordEvent | EventEmitterRegister)[]) { events.forEach((event) => { - if(isDiscordEvent(event)) { - fromEvent(client, event[0], event[1]).subscribe(); + if (isDiscordEvent(event)) { + fromEvent(client, event[0], event[1]).subscribe(); } else { - fromEvent(event[0], event[1], event[2]).subscribe(); + fromEvent(event[0], event[1], event[2]).subscribe(); } }); } diff --git a/src/handler/sernEmitter.ts b/src/handler/sernEmitter.ts index b482d93..7f81c05 100644 --- a/src/handler/sernEmitter.ts +++ b/src/handler/sernEmitter.ts @@ -1,27 +1,28 @@ import { EventEmitter } from 'events'; import type { Module } from './structures/module'; -import type { Err } from 'ts-results'; type Payload = - { success : true, module : Module } - | { success : false, module: Module | undefined, reason : string | Error } + { success: true, module: Module } + | { success: false, module: Module | undefined, reason: string | Error } type SernEventsMapping = { - ['module.register'] : [ Payload ]; - ['module.activate'] : [ Payload ]; - ['error'] : [ Error | string ]; + ['module.register']: [Payload]; + ['module.activate']: [Payload]; + ['error']: [Error | string]; } export default class SernEmitter extends EventEmitter { public override on(eventName: T, listener: (...args: SernEventsMapping[T][]) => void): this { - return super.on(eventName,listener); + return super.on(eventName, listener); } + public override once(eventName: T, listener: (...args: SernEventsMapping[T][]) => void): this { - return super.once(eventName,listener); + return super.once(eventName, listener); } - public override emit(eventName: T, ...args : SernEventsMapping[T]): boolean { - return super.emit(eventName, ...args); + + public override emit(eventName: T, ...args: SernEventsMapping[T]): boolean { + return super.emit(eventName, ...args); } } diff --git a/src/handler/structures/context.ts b/src/handler/structures/context.ts index 3e6e022..c9c2512 100644 --- a/src/handler/structures/context.ts +++ b/src/handler/structures/context.ts @@ -33,17 +33,6 @@ export default class Context { this.oInterac = oInterac; } - static wrap(wrappable: ChatInputCommandInteraction | Message): Context { - if ('token' in wrappable) { - return new Context(None, Some(wrappable)); - } - return new Context(Some(wrappable), None); - } - - public isEmpty() { - return this.oMsg.none && this.oInterac.none; - } - public get message() { return this.oMsg.unwrap(); } @@ -104,6 +93,17 @@ export default class Context { ); } + static wrap(wrappable: ChatInputCommandInteraction | Message): Context { + if ('token' in wrappable) { + return new Context(None, Some(wrappable)); + } + return new Context(Some(wrappable), None); + } + + public isEmpty() { + return this.oMsg.none && this.oInterac.none; + } + /* * Returns the underlying Context but allows for doing other operations */ diff --git a/src/handler/structures/module.ts b/src/handler/structures/module.ts index ec1194a..070d237 100644 --- a/src/handler/structures/module.ts +++ b/src/handler/structures/module.ts @@ -6,14 +6,13 @@ import type { SelectMenuInteraction, UserContextMenuCommandInteraction, } from 'discord.js'; -import type { Override } from '../../types/handler'; -import type { Args } from '../../types/handler'; +import type { Args, Override } from '../../types/handler'; import type { CommandPlugin, EventPlugin } from '../plugins/plugin'; import type Context from './context'; import { CommandType, PluginType } from './enums'; export interface BaseModule { - type : CommandType | PluginType + type: CommandType | PluginType; name?: string; description: string; execute: (ctx: Context, args: Args) => Awaitable; @@ -56,7 +55,7 @@ export type ContextMenuMsg = Override Awaitable }>; -export type ButtonCommand = Override[]; plugins?: CommandPlugin[]; diff --git a/src/handler/structures/structxports.ts b/src/handler/structures/structxports.ts index dcf827c..a9fed98 100644 --- a/src/handler/structures/structxports.ts +++ b/src/handler/structures/structxports.ts @@ -1,5 +1,6 @@ import Context from './context'; import type { BothCommand, Module, SlashCommand, TextCommand } from './module'; import type Wrapper from './wrapper'; + export * from './enums'; export { Context, SlashCommand, TextCommand, BothCommand, Module, Wrapper }; diff --git a/src/handler/structures/wrapper.ts b/src/handler/structures/wrapper.ts index 646ba5c..1e19058 100644 --- a/src/handler/structures/wrapper.ts +++ b/src/handler/structures/wrapper.ts @@ -12,7 +12,7 @@ import type SernEmitter from '../sernEmitter'; */ interface Wrapper { readonly client: Client; - readonly sernEmitter? : SernEmitter + readonly sernEmitter?: SernEmitter; readonly defaultPrefix?: string; readonly commands: string; readonly events?: (DiscordEvent | EventEmitterRegister)[]; diff --git a/src/handler/utilities/markup.ts b/src/handler/utilities/markup.ts index 233bde2..f41ccb1 100644 --- a/src/handler/utilities/markup.ts +++ b/src/handler/utilities/markup.ts @@ -270,6 +270,10 @@ class FormatInner { this.raw = raw; } + static wrap(raw: string, what: string) { + return `${what}${raw}${what}`; + } + toString() { return this.raw; } @@ -328,10 +332,6 @@ class FormatInner { const ret = this.static.wrap(escaped, Strings[key]); return new this.static(ret); } - - static wrap(raw: string, what: string) { - return `${what}${raw}${what}`; - } } /** diff --git a/src/handler/utilities/predicates.ts b/src/handler/utilities/predicates.ts index 4ba76e5..6d1c197 100644 --- a/src/handler/utilities/predicates.ts +++ b/src/handler/utilities/predicates.ts @@ -12,7 +12,6 @@ import type { import type { DiscordEvent, EventEmitterRegister } from '../..'; - export function correctModuleType( plug: Module | undefined, type: T, @@ -24,26 +23,28 @@ export function isChatInputCommand(i: CommandInteraction): i is ChatInputCommand return i.isChatInputCommand(); } -export function isButton(i : MessageComponentInteraction) : i is ButtonInteraction { +export function isButton(i: MessageComponentInteraction): i is ButtonInteraction { return i.isButton(); } -export function isSelectMenu(i : MessageComponentInteraction) : i is SelectMenuInteraction { + +export function isSelectMenu(i: MessageComponentInteraction): i is SelectMenuInteraction { return i.isSelectMenu(); } -export function isMessageCtxMenuCmd(i : CommandInteraction) : i is MessageContextMenuCommandInteraction { + +export function isMessageCtxMenuCmd(i: CommandInteraction): i is MessageContextMenuCommandInteraction { return i.isMessageContextMenuCommand(); } -export function isUserContextMenuCmd(i : CommandInteraction) : i is UserContextMenuCommandInteraction { +export function isUserContextMenuCmd(i: CommandInteraction): i is UserContextMenuCommandInteraction { return i.isUserContextMenuCommand(); } -export function isPromise(promiseLike : Awaitable) : promiseLike is Promise { +export function isPromise(promiseLike: Awaitable): promiseLike is Promise { const keys = new Set(Object.keys(promiseLike)); return keys.has('then') && keys.has('catch'); } -export function isDiscordEvent(el : DiscordEvent | EventEmitterRegister) : el is DiscordEvent { +export function isDiscordEvent(el: DiscordEvent | EventEmitterRegister): el is DiscordEvent { return el.length === 2; } diff --git a/src/types/handler.ts b/src/types/handler.ts index 87cc4b5..35fd0c6 100644 --- a/src/types/handler.ts +++ b/src/types/handler.ts @@ -17,8 +17,7 @@ export type ParseType = { export type Args = ParseType<{ text: string[]; slash: SlashOptions }>; export type DiscordEvent = ParseType<{ [K in keyof ClientEvents]: (...args: ClientEvents[K]) => Awaitable }>; -export type EventEmitterRegister = [ emitter: EventEmitter, k : string, cb : (...args: unknown[]) => Awaitable]; - +export type EventEmitterRegister = [emitter: EventEmitter, k: string, cb: (...args: unknown[]) => Awaitable]; export type SlashOptions = Omit;