diff --git a/src/handler/events/interactionCreate.ts b/src/handler/events/interactionCreate.ts index ff91b2b..713ba61 100644 --- a/src/handler/events/interactionCreate.ts +++ b/src/handler/events/interactionCreate.ts @@ -1,10 +1,11 @@ import type { - ChatInputCommandInteraction, CommandInteraction, Interaction, + MessageComponentInteraction, MessageContextMenuCommandInteraction as MessageCtxInt, UserContextMenuCommandInteraction as UserCtxInt, } from 'discord.js'; +import type { SelectMenuInteraction } from 'discord.js'; import { concatMap, fromEvent, Observable, of, throwError } from 'rxjs'; import type Wrapper from '../structures/wrapper'; import * as Files from '../utilities/readFile'; @@ -14,16 +15,11 @@ import Context from '../structures/context'; import type { Result } from 'ts-results'; import { CommandType, controller } from '../sern'; import type { Args, UnionToTuple } from '../../types/handler'; -import type { MessageComponentInteraction } from 'discord.js'; -import { ComponentType } from 'discord.js'; import type { Module } from '../structures/module'; import type { EventPlugin } from '../plugins/plugin'; +import { isButton, isChatInputCommand, isSelectMenu } from '../utilities/predicates'; -function isChatInputCommand(i: CommandInteraction): i is ChatInputCommandInteraction { - return i.isChatInputCommand(); -} - function applicationCommandHandler(mod: Module | undefined, interaction: CommandInteraction) { if (mod === undefined) { return throwError(() => SernError.UndefinedModule); @@ -33,13 +29,13 @@ function applicationCommandHandler(mod: Module | undefined, interaction: Command .when(isChatInputCommand, i => { const ctx = Context.wrap(i); const res = eventPlugins.map(e => { - return (>e).execute( + return (>e).execute( [ctx, ['slash', i.options]] , controller); }) as Awaited>[]; //Possible unsafe cast // could result in the promises not being resolved - return of({ type: mod.type, res, plugged: mod, ctx }); + return of({ type: CommandType.Slash, res, mod, ctx }); }, ) .when( @@ -51,7 +47,7 @@ function applicationCommandHandler(mod: Module | undefined, interaction: Command [ctx] as UnionToTuple , controller); }) as Awaited>[]; - return of({ type: mod.type, res, plugged: mod, ctx }); + return of({ type: mod.type, res, mod, ctx }); }, ) .run(); @@ -66,13 +62,17 @@ function messageComponentInteractionHandler( } const eventPlugins = mod.onEvent; return match(interaction) - .with({ - componentType: P.union(ComponentType.Button, ComponentType.SelectMenu), - }, (ctx) => { + .when(isButton, ctx => { const res = eventPlugins.map(e => { - return e.execute([ctx] as UnionToTuple, controller); + return (>e).execute([ctx], controller); }) as Awaited>[]; - return of({ type: mod.type, res, plugged: mod, ctx }); + return of({ type: mod.type, res, mod, ctx }); + }) + .when(isSelectMenu, (ctx: SelectMenuInteraction) => { + const res = eventPlugins.map(e => { + return (>e).execute([ctx], controller); + }) as Awaited>[]; + return of({ type: mod.type, res, mod, ctx }); }) .otherwise(() => throwError(() => SernError.NotSupportedInteraction)); } @@ -100,7 +100,9 @@ export const onInteractionCreate = (wrapper: Wrapper) => { } else return throwError(() => SernError.NotSupportedInteraction); }), ) - .subscribe(console.log); + .subscribe(m => { + m; + }); }; diff --git a/src/handler/events/readyEvent.ts b/src/handler/events/readyEvent.ts index 070a8c6..d1d4379 100644 --- a/src/handler/events/readyEvent.ts +++ b/src/handler/events/readyEvent.ts @@ -15,8 +15,8 @@ export const onReady = (wrapper: Wrapper) => { const ready$ = fromEvent(client, 'ready').pipe(take(1), skip(1)); const processCommandFiles$ = Files.buildData(commands).pipe( map(({ mod, absPath }) => { - const name = mod?.name ?? Files.fmtFileName(basename(absPath)); if (mod?.name === undefined) { + const name = Files.fmtFileName(basename(absPath)); return { name, ...mod }; } return mod; diff --git a/src/handler/plugins/plugin.ts b/src/handler/plugins/plugin.ts index 9297a0b..c387b39 100644 --- a/src/handler/plugins/plugin.ts +++ b/src/handler/plugins/plugin.ts @@ -50,6 +50,9 @@ export type EventPlugin = { { execute: (event: Parameters, controller: Controller) => Awaitable>; }>; +export type EventPluginType = { + [K in CommandType] : EventPlugin +} export function plugins(...plug: CommandPlugin[]): CommandPlugin[]; export function plugins(...plug: EventPlugin[]): EventPlugin[]; @@ -58,6 +61,6 @@ export function plugins(...plug: CommandPlugin[] | EventP return plug; } -export function sernModule(mod: Module): Module { +export function sernModule(mod: ModuleDefs[T]): Module { return mod; } diff --git a/src/handler/utilities/predicates.ts b/src/handler/utilities/predicates.ts index cbcc326..a868a7b 100644 --- a/src/handler/utilities/predicates.ts +++ b/src/handler/utilities/predicates.ts @@ -1,8 +1,25 @@ import type { Module, ModuleDefs } from '../structures/module'; +import type { ChatInputCommandInteraction, CommandInteraction } from 'discord.js'; +import type { EventPlugin } from '../../../dist'; +import { CommandType } from '../sern'; +import type { EventPluginType } from '../plugins/plugin'; +import type { ButtonInteraction, MessageComponentInteraction, SelectMenuInteraction } from 'discord.js'; + export function correctModuleType( plug: Module | undefined, type: T, ): plug is ModuleDefs[T] { return plug !== undefined && plug.type === type; +} + +export function isChatInputCommand(i: CommandInteraction): i is ChatInputCommandInteraction { + return i.isChatInputCommand(); +} + +export function isButton(i : MessageComponentInteraction) : i is ButtonInteraction { + return i.isButton(); +} +export function isSelectMenu(i : MessageComponentInteraction) : i is SelectMenuInteraction { + return i.isSelectMenu(); } \ No newline at end of file