diff --git a/src/command.ts b/src/command.ts index 90cfedb..0580302 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,7 @@ export class Command implements MiddlewareObj { } middleware() { - return this._composer.middleware(); + return new Composer(this._composer, this._defaultScopeComposer) + .middleware(); } } 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 () => {