refactor: change from switch -> match

This commit is contained in:
Jacob Nguyen
2022-05-07 22:35:24 -05:00
parent 57dc0bac88
commit d3e8943ca9
2 changed files with 31 additions and 14 deletions

View File

@@ -1,15 +1,16 @@
import { ApplicationCommandType, Interaction } from 'discord.js';
import { fromEvent, Observable, of, concatMap, map, filter } from 'rxjs';
import { ApplicationCommandType, ChatInputCommandInteraction, Interaction } from 'discord.js';
import { fromEvent, Observable, of, concatMap, map, filter, throwError } from 'rxjs';
import { CommandType } from '../sern';
import Context from '../structures/context';
import type { ModuleDefs } from '../structures/modules/commands/moduleHandler';
import type { PluggedModule } from '../structures/modules/module';
import type Wrapper from '../structures/wrapper';
import * as Files from '../utilities/readFile';
import { match, partition } from './observableHandling';
import { match } from 'ts-pattern';
import { isEventPlugin } from './readyEvent';
import { _ } from 'ts-pattern/dist/patterns';
import { SernError } from '../structures/errors';
@@ -19,18 +20,32 @@ export const onInteractionCreate = ( wrapper : Wrapper ) => {
const interactionEvent$ = (<Observable<Interaction>> fromEvent(client, 'interactionCreate'))
const processCommand$ = interactionEvent$.pipe(
interactionEvent$.pipe(
concatMap( interaction => {
if(interaction.isCommand()) {
return of( Files
.ApplicationCommandStore[interaction.commandType]
.get(interaction.commandName)
).pipe(
).pipe(
map ( plug => {
if(plug === undefined) {
return throwError(() => SernError.UndefinedModule)
}
const eventPlugins = plug.plugins.filter(isEventPlugin);
match(interaction)
.when( interaction.isChatInputCommand, (i : ChatInputCommandInteraction) => {
console.log(i, eventPlugins)
})
.when( () => _ , i => {
console.log(i, eventPlugins)
})
return "fsd"
})
)
}
return of()
})
);
).subscribe();

View File

@@ -13,6 +13,8 @@ import { SernError } from './structures/errors';
import { onReady } from './events/readyEvent';
import { onMessageCreate } from './events/messageEvent';
import { onInteractionCreate } from './events/interactionCreate';
import { match } from 'ts-pattern';
import { _ } from 'ts-pattern/dist/patterns';
export function init( wrapper : Wrapper ) {
const { events, client } = wrapper;
@@ -44,12 +46,12 @@ export enum CommandType {
}
export function cmdTypeToDjs(ty: CommandType) {
switch (ty) {
case CommandType.Slash : case CommandType.Both : return ApplicationCommandType.ChatInput;
case CommandType.MenuUser : return ApplicationCommandType.User;
case CommandType.MenuMsg : return ApplicationCommandType.Message;
default : throw new Error(`Cannot turn this CommandType to ApplicationCommandType`)
}
return match(ty)
.with(CommandType.Slash, () => ApplicationCommandType.ChatInput)
.with(CommandType.MenuUser, () => ApplicationCommandType.User)
.with(CommandType.MenuMsg, ()=> ApplicationCommandType.Message)
.with(_, () => { throw new Error(SernError.NonValidModuleType) })
.exhaustive()
}