From 683d19d63f639b315465b4b3277a8e266223941d Mon Sep 17 00:00:00 2001 From: Tim Smart Date: Wed, 7 Dec 2022 11:33:17 +1300 Subject: [PATCH] feat: simplify interaction success type --- src/Interactions/context.ts | 5 ++--- src/Interactions/definitions.ts | 22 ++++++++++++---------- src/Interactions/gateway.ts | 4 +--- src/Interactions/handlers.ts | 8 ++++---- src/Interactions/index.ts | 15 ++++++++++++--- src/Interactions/webhook.ts | 12 +----------- 6 files changed, 32 insertions(+), 34 deletions(-) diff --git a/src/Interactions/context.ts b/src/Interactions/context.ts index 935cd53..bb54a6f 100644 --- a/src/Interactions/context.ts +++ b/src/Interactions/context.ts @@ -1,5 +1,4 @@ import { InteractionResponse } from "./definitions.js" -import { DefinitionNotFound } from "./handlers.js" import * as Arr from "@fp-ts/data/ReadonlyArray" import { optionsMap } from "dfx/Helpers/interactions" import { EffectTypeId, Effect } from "@effect/io/Effect" @@ -57,7 +56,7 @@ export class SubCommandNotFound { } export const handleSubCommands = < - NER extends Record>>, + NER extends Record>, >( commands: NER, ): Effect< @@ -77,7 +76,7 @@ export const handleSubCommands = < ? E : never) | SubCommandNotFound, - Maybe + InteractionResponse > => Effect.service(ApplicationCommandContext).flatMap((data) => pipe( diff --git a/src/Interactions/definitions.ts b/src/Interactions/definitions.ts index f08fbb3..03d114e 100644 --- a/src/Interactions/definitions.ts +++ b/src/Interactions/definitions.ts @@ -41,7 +41,7 @@ export class GlobalApplicationCommand { readonly _tag = "GlobalApplicationCommand" constructor( readonly command: Discord.CreateGlobalApplicationCommandParams, - readonly handle: Effect>, + readonly handle: Effect, ) {} } @@ -53,7 +53,7 @@ export const global = < command: A, handle: DescriptionMissing extends true ? "command description is missing" - : Effect>, + : Effect, ) => new GlobalApplicationCommand< Exclude, @@ -64,7 +64,7 @@ export class GuildApplicationCommand { readonly _tag = "GuildApplicationCommand" constructor( readonly command: Discord.CreateGuildApplicationCommandParams, - readonly handle: Effect>, + readonly handle: Effect, ) {} } @@ -76,7 +76,7 @@ export const guild = < command: A, handle: DescriptionMissing extends true ? "command description is missing" - : Effect>, + : Effect, ) => new GuildApplicationCommand< Exclude, @@ -87,13 +87,13 @@ export class MessageComponent { readonly _tag = "MessageComponent" constructor( readonly predicate: (customId: string) => Effect, - readonly handle: Effect>, + readonly handle: Effect, ) {} } export const messageComponent = ( pred: (customId: string) => Effect, - handle: Effect>, + handle: Effect, ) => new MessageComponent< Exclude, @@ -104,13 +104,13 @@ export class ModalSubmit { readonly _tag = "ModalSubmit" constructor( readonly predicate: (customId: string) => Effect, - readonly handle: Effect>, + readonly handle: Effect, ) {} } export const modalSubmit = ( pred: (customId: string) => Effect, - handle: Effect>, + handle: Effect, ) => new ModalSubmit< Exclude, @@ -121,17 +121,19 @@ export class Autocomplete { readonly _tag = "Autocomplete" constructor( readonly predicate: ( + data: Discord.ApplicationCommandDatum, focusedOption: Discord.ApplicationCommandInteractionDataOption, ) => Effect, - readonly handle: Effect>, + readonly handle: Effect, ) {} } export const autocomplete = ( pred: ( + data: Discord.ApplicationCommandDatum, focusedOption: Discord.ApplicationCommandInteractionDataOption, ) => Effect, - handle: Effect>, + handle: Effect, ) => new Autocomplete< Exclude< diff --git a/src/Interactions/gateway.ts b/src/Interactions/gateway.ts index 66b5417..cd007ce 100644 --- a/src/Interactions/gateway.ts +++ b/src/Interactions/gateway.ts @@ -52,9 +52,7 @@ export const run = const run = Gateway.handleDispatch("INTERACTION_CREATE", (i) => pipe( handle[i.type](i).tap((r) => - r.match(Effect.unit, (r) => - Rest.rest.createInteractionResponse(i.id, i.token, r), - ), + Rest.rest.createInteractionResponse(i.id, i.token, r), ), postHandler, Effect.provideService(InteractionContext)(i), diff --git a/src/Interactions/handlers.ts b/src/Interactions/handlers.ts index 06a4cb5..64d52e6 100644 --- a/src/Interactions/handlers.ts +++ b/src/Interactions/handlers.ts @@ -11,7 +11,7 @@ export class DefinitionNotFound { type Handler = Effect< R | Discord.Interaction, E | DefinitionNotFound, - Maybe + Discord.InteractionResponse > export const handlers = ( @@ -27,7 +27,7 @@ export const handlers = ( [Discord.InteractionType.PING]: () => Effect.succeed({ type: Discord.InteractionCallbackType.PONG, - }).asSome, + }), [Discord.InteractionType.APPLICATION_COMMAND]: (i) => { const data = i.data as Discord.ApplicationCommandDatum @@ -100,7 +100,7 @@ export const handlers = ( Arr.map((a) => Effect.struct({ command: Effect.succeed(a), - match: a.predicate(focusedOption), + match: a.predicate(data, focusedOption), }), ), (a) => @@ -117,7 +117,7 @@ export const handlers = ( Effect.provideService(Ctx.FocusedOptionContext)({ focusedOption }), ), ) - .getOrElse(() => Effect.fail(new DefinitionNotFound(i)) as any) + .getOrElse(() => Effect.fail(new DefinitionNotFound(i))) }, } } diff --git a/src/Interactions/index.ts b/src/Interactions/index.ts index a5b4288..ca2bda5 100644 --- a/src/Interactions/index.ts +++ b/src/Interactions/index.ts @@ -80,12 +80,21 @@ export const idRegex = (query: RegExp) => (customId: string) => export const option = (command: string, optionName: string) => ( - data: Discord.ApplicationCommandDatum, - focusedOption: Discord.ApplicationCommandInteractionDataOption, + data: Pick, + focusedOption: Pick< + Discord.ApplicationCommandInteractionDataOption, + "name" + >, ) => Effect.succeed(data.name === command && focusedOption.name === optionName) export const optionOnly = (optionName: string) => - (focusedOption: Discord.ApplicationCommandInteractionDataOption) => + ( + _: unknown, + focusedOption: Pick< + Discord.ApplicationCommandInteractionDataOption, + "name" + >, + ) => Effect.succeed(focusedOption.name === optionName) diff --git a/src/Interactions/webhook.ts b/src/Interactions/webhook.ts index 91d8210..c8f1c56 100644 --- a/src/Interactions/webhook.ts +++ b/src/Interactions/webhook.ts @@ -73,7 +73,6 @@ export interface HandleWebhookOpts { body: string success: (a: Discord.InteractionResponse) => Effect error: (e: Cause) => Effect - empty: Effect } export const makeHandler = (ix: InteractionBuilder) => { @@ -83,17 +82,8 @@ export const makeHandler = (ix: InteractionBuilder) => { headers, body, success, - empty, error, }: HandleWebhookOpts< E | WebhookParseError | BadWebhookSignature | DefinitionNotFound - >) => - handle(headers, body) - .flatMap((o) => - o.match( - () => empty, - (a) => success(a), - ), - ) - .catchAllCause(error) + >) => handle(headers, body).flatMap(success).catchAllCause(error) }