Skip to content

Commit

Permalink
feat: finished interactionCreate.ts handling? (need test)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacoobes committed May 17, 2022
1 parent 972928b commit 97907b7
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 46 deletions.
94 changes: 51 additions & 43 deletions src/handler/events/interactionCreate.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import type { CommandInteraction, Interaction, MessageComponentInteraction, SelectMenuInteraction } from 'discord.js';
import { concatMap, from, fromEvent, map, Observable, of, throwError } from 'rxjs';
import { concatMap, fromEvent, map, Observable, of, throwError } from 'rxjs';
import type Wrapper from '../structures/wrapper';
import * as Files from '../utilities/readFile';
import { match } from 'ts-pattern';
import { SernError } from '../structures/errors';
import Context from '../structures/context';
import type { Result } from 'ts-results';
import { CommandType, controller } from '../sern';
import type { Module } from '../structures/module';
import type { Module, ModuleDefs} from '../structures/module';
import type { EventPlugin } from '../plugins/plugin';
import {
isButton,
Expand All @@ -27,48 +27,42 @@ function applicationCommandHandler(mod: Module | undefined, interaction: Command
const mod$ = <T extends CommandType>(cmdTy : T) => of(mod).pipe(
filterCorrectModule(cmdTy)
);
const eventPlugins = mod.onEvent;

return match(interaction)
.when(isChatInputCommand, i => {
return mod$(CommandType.Slash).pipe(
concatMap(m => {
const ctx = Context.wrap(i);
return from(m.onEvent.map(e => e.execute(
[ctx, ['slash', i.options]],
controller
)));
}),
map( res => ({ mod, res }))
);
const ctx = Context.wrap(i);
return mod$(CommandType.Slash).pipe(
concatMap(m => {
return of(m.onEvent.map(e => e.execute(
[ctx, ['slash', i.options]],
controller
))).pipe(map(res => ({ m, res, execute() { 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 from(m.onEvent.map(e => e.execute(
return of(m.onEvent.map(e => e.execute(
[ctx],
controller
)));
))).pipe(map(res => ({ m, res, execute() { m.execute(ctx); } }) ));
}),
map( res => ({ mod, res }))
);
const res = eventPlugins.map(e => {
return (<EventPlugin<CommandType.MenuMsg>>e).execute([ctx]
, controller);
}) as Awaited<Result<void, void>>[];
//Possible unsafe cast
// could result in the promises not being resolved
return of({ type: mod.type, res, mod, ctx });
},
)
.when(isUserContextMenuCmd, ctx => {
const res = eventPlugins.map( e=> (<EventPlugin<CommandType.MenuUser>>e).execute([ctx]
, controller)
) as Awaited<Result<void, void>>[];
//Possible unsafe cast
// could result in the promises not being resolved
return of({ type: mod.type, res, mod, ctx });
return mod$(CommandType.MenuUser).pipe(
concatMap(m => {
return of(m.onEvent.map(e => e.execute(
[ctx],
controller
))).pipe(map(res => ({ m, res, execute() { m.execute(ctx); } }) ));
}),
);
})
.run();
}
Expand All @@ -80,24 +74,34 @@ function messageComponentInteractionHandler(
if (mod === undefined) {
return throwError(() => SernError.UndefinedModule);
}
const eventPlugins = mod.onEvent;
const mod$ = <T extends CommandType>(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)
.when(isButton, ctx => {
const res = eventPlugins.map(e => {
return (<EventPlugin<CommandType.Button>>e).execute([ctx], controller);
}) as Awaited<Result<void, void>>[];
return of({ type: mod.type, res, mod, ctx });
return mod$(CommandType.Button).pipe(
concatMap(m => {
return of(m.onEvent.map(e => e.execute(
[ctx],
controller
))).pipe(map(res => ({ m, res, execute() { m.execute(ctx); } }) ));
}),
);
})
.when(isSelectMenu, (ctx: SelectMenuInteraction) => {
const res = eventPlugins.map(e => {
return (<EventPlugin<CommandType.MenuSelect>>e).execute([ctx], controller);
}) as Awaited<Result<void, void>>[];
return of({ type: mod.type, res, mod, ctx });
return mod$(CommandType.MenuSelect).pipe(
concatMap(m => {
return of(m.onEvent.map(e => e.execute(
[ctx],
controller
))).pipe(map(res => ({ m, res, execute() { m.execute(ctx); } }) ));
}),
);
})
.otherwise(() => throwError(() => SernError.NotSupportedInteraction));
}

export const onInteractionCreate = (wrapper: Wrapper) => {
export function onInteractionCreate (wrapper: Wrapper) {
const { client } = wrapper;

const interactionEvent$ = <Observable<Interaction>>fromEvent(client, 'interactionCreate');
Expand All @@ -119,10 +123,14 @@ export const onInteractionCreate = (wrapper: Wrapper) => {
return messageComponentInteractionHandler(modul, interaction);
} else return throwError(() => SernError.NotSupportedInteraction);
}),
)
.subscribe(m => {
m;
).subscribe({
next({m, res, execute}) {
// execute();
},
error(err) {
return;
}
});


};
}
3 changes: 0 additions & 3 deletions src/handler/events/observableHandling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ export function filterCorrectModule<T extends keyof ModuleDefs>(cmdType: T) {
if (correctModuleType(mod, cmdType)) {
subscriber.next(mod);
} else {
if (mod === undefined) {
return throwError(() => SernError.UndefinedModule);
}
return throwError(() => SernError.MismatchModule);
}
},
Expand Down

0 comments on commit 97907b7

Please sign in to comment.