Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add delay registration to default scope handler #50

Merged
merged 4 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export class Command<C extends Context = Context> implements MiddlewareObj<C> {
{ name: string | RegExp; description: string }
> = new Map();
private _composer: Composer<C> = new Composer<C>();
private _defaultScopeComposer = new Composer<C>();
private _options: CommandOptions = {
prefix: "/",
matchOnlyAtStart: true,
Expand Down Expand Up @@ -274,7 +275,7 @@ export class Command<C extends Context = Context> implements MiddlewareObj<C> {
if (middlewareArray) {
switch (scope.type) {
case "default":
this._composer
this._defaultScopeComposer
.filter(Command.hasCommand(this.names, optionsObject))
.use(...middlewareArray);
break;
Expand Down Expand Up @@ -445,6 +446,7 @@ export class Command<C extends Context = Context> implements MiddlewareObj<C> {
}

middleware() {
return this._composer.middleware();
return new Composer(this._composer, this._defaultScopeComposer)
.middleware();
}
}
26 changes: 24 additions & 2 deletions test/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 () => {
Expand Down
Loading