diff --git a/package.json b/package.json index a1044ae..6810ebf 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@gramio/prompt", - "version": "1.1.0", + "version": "1.1.1", "description": "Prompt plugin for GramIO", "main": "dist/index.cjs", "module": "dist/index.js", diff --git a/src/index.ts b/src/index.ts index c7a1b7a..7be5393 100644 --- a/src/index.ts +++ b/src/index.ts @@ -84,7 +84,7 @@ export function prompt(options?: { const prompt = prompts.get(id); if (prompt) { - if (prompt?.event && !context.is(prompt.event)) return next(); + if (prompt?.events && !context.is(prompt.events)) return next(); // @ts-ignore if (prompt.validate && !(await prompt.validate(context))) { if (typeof prompt.onValidateError === "string") diff --git a/src/types.ts b/src/types.ts index fdfd664..a0dddba 100644 --- a/src/types.ts +++ b/src/types.ts @@ -89,7 +89,7 @@ export type OnValidateErrorFunction = ( interface PromptData { resolve: (context: PromptAnswer) => void; - event?: Event; + events?: Event[]; validate?: ValidateFunction; onValidateError?: string | OnValidateErrorFunction; transform?: TransformFunction; @@ -111,8 +111,8 @@ export interface PromptFunction { params?: PromptFunctionParams, ): Promise>; /** Send message and wait answer ignoring events not listed */ - ( - event: Event, + ( + event: MaybeArray, text: Stringable, params?: PromptFunctionParams, ): Promise>; @@ -122,32 +122,34 @@ export interface WaitFunction { /** Wait for the next event from the user */ (): Promise>; /** Wait for the next event from the user ignoring events not listed */ - ( - event: Event, + ( + event: MaybeArray, ): Promise>; /** Wait for the next event from the user ignoring non validated answers */ ( validate: ValidateFunction, ): Promise>; /** Wait for the next event from the user ignoring non validated answers and not listed events with transformer */ - ( - event: Event, + ( + event: MaybeArray, options: { validate?: ValidateFunction; transform?: TransformFunction; }, ): Promise>; /** Wait for the next event from the user ignoring non validated answers and not listed events */ - ( - event: Event, + ( + event: MaybeArray, validate: ValidateFunction, ): Promise>; } export interface WaitWithActionFunction { // biome-ignore lint/style/useShorthandFunctionType: - ( - event: Event, + ( + event: MaybeArray, action: () => MaybePromise, ): Promise<[PromptAnswer, ActionReturn]>; } + +export type MaybeArray = T | T[]; diff --git a/src/utils.ts b/src/utils.ts index eb6d996..f7785a7 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,6 +1,7 @@ import { FormattableString } from "gramio"; import type { EventsUnion, + MaybeArray, OnValidateErrorFunction, PromptAnswer, PromptFunction, @@ -45,8 +46,11 @@ export const events = [ ] as const; function isEvent( - maybeEvent: EventsUnion | Stringable, -): maybeEvent is EventsUnion { + maybeEvent: MaybeArray | Stringable, +): maybeEvent is EventsUnion | EventsUnion[] { + if (Array.isArray(maybeEvent)) + return maybeEvent.every((event) => events.includes(event)); + return events.includes(maybeEvent.toString() as EventsUnion); } @@ -57,7 +61,7 @@ export function getPrompt( defaults: PromptFunctionParams, ): PromptFunction { async function prompt( - eventOrText: Event | Stringable, + eventOrText: MaybeArray | Stringable, textOrParams?: Stringable | PromptFunctionParams, params?: PromptFunctionParams, ) { @@ -81,11 +85,13 @@ export function getPrompt( await context.send(text, sendParams); + const events = isEvent(eventOrText) ? eventOrText : undefined; + return new Promise>((resolve) => { prompts.set(id, { // @ts-expect-error resolve: resolve, - event: isEvent(eventOrText) ? eventOrText : undefined, + events: Array.isArray(events) ? events : events ? [events] : undefined, validate, // @ts-expect-error transform, @@ -101,7 +107,7 @@ export function getPrompt( export function getWait(prompts: PromptsType, id: number): WaitFunction { async function wait( - eventOrValidate?: Event | ValidateFunction, + eventOrValidate?: MaybeArray | ValidateFunction, validateOrOptions?: | ValidateFunction | { @@ -110,15 +116,18 @@ export function getWait(prompts: PromptsType, id: number): WaitFunction { onValidateError?: string | OnValidateErrorFunction; }, ) { + const events = + eventOrValidate && + typeof eventOrValidate !== "function" && + isEvent(eventOrValidate) + ? eventOrValidate + : undefined; + return new Promise>((resolve) => { prompts.set(id, { // @ts-expect-error resolve: resolve, - event: - // @ts-expect-error - eventOrValidate && isEvent(eventOrValidate) - ? eventOrValidate - : undefined, + events: Array.isArray(events) ? events : events ? [events] : undefined, validate: typeof eventOrValidate === "function" ? eventOrValidate @@ -151,7 +160,7 @@ export function getWaitWithAction( Data = never, ActionReturn = any, >( - event: EventsUnion, + events: MaybeArray, action: () => ActionReturn, validateOrOptions?: | ValidateFunction @@ -180,7 +189,7 @@ export function getWaitWithAction( prompts.set(id, { // @ts-expect-error resolve: resolve, - event, + events: Array.isArray(events) ? events : [events], validate: validate, // @ts-expect-error transform: async (context) => {