From 8d850b9779575ef16e931248cef4927c7f09e130 Mon Sep 17 00:00:00 2001 From: Roz <3948961+roziscoding@users.noreply.github.com> Date: Wed, 6 Nov 2024 12:38:11 -0300 Subject: [PATCH 1/4] fix: register default scope handler last --- src/command.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/command.ts b/src/command.ts index 90cfedb..0a6b60d 100644 --- a/src/command.ts +++ b/src/command.ts @@ -38,6 +38,7 @@ export class Command implements MiddlewareObj { { name: string | RegExp; description: string } > = new Map(); private _composer: Composer = new Composer(); + private _defaultScopeComposer = new Composer(); private _options: CommandOptions = { prefix: "/", matchOnlyAtStart: true, @@ -274,7 +275,7 @@ export class Command implements MiddlewareObj { if (middlewareArray) { switch (scope.type) { case "default": - this._composer + this._defaultScopeComposer .filter(Command.hasCommand(this.names, optionsObject)) .use(...middlewareArray); break; @@ -445,6 +446,9 @@ export class Command implements MiddlewareObj { } middleware() { - return this._composer.middleware(); + return new Composer() + .use(this._composer) + .use(this._defaultScopeComposer) + .middleware(); } } From ad08d10da6b741b9cb26a4d11f1444245f92ae50 Mon Sep 17 00:00:00 2001 From: Hero Protagonist Date: Thu, 7 Nov 2024 20:22:12 -0300 Subject: [PATCH 2/4] feat: add integration test for ensuring default handler is register last --- test/integration.test.ts | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/test/integration.test.ts b/test/integration.test.ts index d6bb8e3..61fd8c1 100644 --- a/test/integration.test.ts +++ b/test/integration.test.ts @@ -34,13 +34,14 @@ const getBot = () => }, }); -const getDummyUpdate = ({ userInput, language, noChat }: { +const getDummyUpdate = ({ userInput, language, noChat, chatType = "private" }: { userInput?: string; language?: string; noChat?: boolean; + chatType?: Chat["type"]; } = {}) => { const u = { id: 42, first_name: "yo", language_code: language } as User; - const c = { id: 100, type: "private" } as Chat; + const c = { id: 100, type: chatType } as Chat; const m = { text: userInput, from: u, @@ -186,6 +187,27 @@ describe("Integration", () => { assertSpyCalls(handler, 1); }); + it("should prioritize manually added scopes over the default handler ", async () => { + const defaultHandler = spy(() => {}); + const specificHandler = spy(() => {}); + + const commandGroup = new CommandGroup(); + commandGroup.command("command", "_", defaultHandler, { prefix: "!" }) + .addToScope({ type: "all_group_chats" }, specificHandler); + + const bot = getBot(); + bot.use(commands()); + bot.use(commandGroup); + + await bot.handleUpdate( + getDummyUpdate({ + chatType: "group", + userInput: "!command", + }), + ); + assertSpyCalls(defaultHandler, 0); + assertSpyCalls(specificHandler, 1); + }); }); describe("add", () => { it("should add a command that was statically created", async () => { From acdf0dee8ea40a1a4394e5f98481dc2af9793b5b Mon Sep 17 00:00:00 2001 From: Hero Protagonist Date: Thu, 7 Nov 2024 20:24:08 -0300 Subject: [PATCH 3/4] fix: return main composer instead of the nested one --- src/command.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/command.ts b/src/command.ts index 0a6b60d..df2925e 100644 --- a/src/command.ts +++ b/src/command.ts @@ -446,9 +446,11 @@ export class Command implements MiddlewareObj { } middleware() { - return new Composer() - .use(this._composer) - .use(this._defaultScopeComposer) + const finalComposer = new Composer() + .use(this._composer); + finalComposer + .use(this._defaultScopeComposer); + return finalComposer .middleware(); } } From 0a49a0ecd872e1e3bb0d9e6b3ce9d55ffb81c193 Mon Sep 17 00:00:00 2001 From: Hero Protagonist Date: Thu, 7 Nov 2024 20:36:05 -0300 Subject: [PATCH 4/4] fix: return a completely new composer --- src/command.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/command.ts b/src/command.ts index df2925e..0580302 100644 --- a/src/command.ts +++ b/src/command.ts @@ -446,11 +446,7 @@ export class Command implements MiddlewareObj { } middleware() { - const finalComposer = new Composer() - .use(this._composer); - finalComposer - .use(this._defaultScopeComposer); - return finalComposer + return new Composer(this._composer, this._defaultScopeComposer) .middleware(); } }