From 7c3f5cafa62fb7385512325313b3a64178cff1f2 Mon Sep 17 00:00:00 2001 From: teidesu <86301490+teidesu@users.noreply.github.com> Date: Tue, 6 Jul 2021 18:38:06 +0300 Subject: [PATCH] feat(dispatcher): deeplink filter --- packages/dispatcher/src/filters.ts | 48 +++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/packages/dispatcher/src/filters.ts b/packages/dispatcher/src/filters.ts index 5a40bad0..178c4c4f 100644 --- a/packages/dispatcher/src/filters.ts +++ b/packages/dispatcher/src/filters.ts @@ -810,7 +810,7 @@ export namespace filters { * type-safe extension field `.commmand` of the {@link Message} object. * First element is the command itself, then the arguments. * - * If the matched command was a RegExp, the first element is the full + * If the matched command was a RegExp, the first element is the * command, then the groups from the command regex, then the arguments. * * @param commands Command(s) the filter should look for (w/out prefix) @@ -894,6 +894,52 @@ export namespace filters { command('start') ) + /** + * Filter for deep links (i.e. `/start `). + * + * If the parameter is a regex, groups are added to `msg.command`, + * meaning that the first group is available in `msg.command[2]`. + */ + export const deeplink = (params: MaybeArray): UpdateFilter => { + if (!Array.isArray(params)) { + return and( + start, + (msg: Message & { command: string[] }) => { + if (msg.command.length !== 2) return false + + const p = msg.command[1] + if (typeof params === 'string' && p === params) return true + + const m = p.match(params) + if (!m) return false + + msg.command.push(...m.slice(1)) + return true + } + ) + } + + return and( + start, + (msg: Message & { command: string[] }) => { + if (msg.command.length !== 2) return false + + const p = msg.command[1] + for (const param of params) { + if (typeof param === 'string' && p === param) return true + + const m = p.match(param) + if (!m) continue + + msg.command.push(...m.slice(1)) + return true + } + + return false + } + ) + } + /** * Create a filter for {@link ChatMemberUpdate} by update type *