Skip to content

Allow anonymous admins to use admin scoped commands #74

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
13 changes: 11 additions & 2 deletions src/utils/checks.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import { Composer, Context, Middleware } from "../deps.deno.ts";
import {
ChatTypeContext,
Composer,
Context,
Middleware,
} from "../deps.deno.ts";
import { CommandOptions } from "../types.ts";
import { MaybeArray } from "./array.ts";

export function isAdmin(ctx: Context) {
export function isAdmin(ctx: ChatTypeContext<Context, "group" | "supergroup">) {
if (ctx.senderChat?.id === ctx.chat.id) {
// anonymous admin
return true;
}
return ctx
.getAuthor()
.then((author) => ["administrator", "creator"].includes(author.status));
Expand Down
37 changes: 37 additions & 0 deletions test/command.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -932,6 +932,18 @@ describe("Command", () => {
assertSpyCalls(chatAdministratorsSpy, 1);
});

it("should call chatAdministrators for anonymous admin", async () => {
chatMember = { status: "member" } as ChatMember; // unused

assertSpyCalls(chatAdministratorsSpy, 1);
await mw(makeContext({
chat: { id: -123, type: "group" },
sender_chat: { id: -123, type: "group" },
text: "/a",
} as Message));
assertSpyCalls(chatAdministratorsSpy, 2);
});

it("should call chat", async () => {
assertSpyCalls(chatSpy, 0);
await mw(makeContext({
Expand Down Expand Up @@ -964,6 +976,18 @@ describe("Command", () => {
assertSpyCalls(allChatAdministratorsSpy, 1);
});

it("should call allChatAdministrators for anonymous admin", async () => {
chatMember = { status: "administrator" } as ChatMember; // unused

assertSpyCalls(allChatAdministratorsSpy, 1);
await mw(makeContext({
chat: { id: -124, type: "group" },
sender_chat: { id: -124, type: "group" },
text: "/a",
} as Message));
assertSpyCalls(allChatAdministratorsSpy, 2);
});

it("should call allGroupChats", async () => {
chatMember = { status: "member" } as ChatMember;

Expand Down Expand Up @@ -995,5 +1019,18 @@ describe("Command", () => {
} as Message));
assertSpyCalls(defaultSpy, 1);
});

it("should call group on sender_chat", async () => {
chatMember = { status: "member" } as ChatMember;

assertSpyCalls(allGroupChatsSpy, 1);
await mw(makeContext({
chat: { id: -124, type: "group" },
from: { id: 789 },
sender_chat: { id: -123, type: "channel" },
text: "/a",
} as Message));
assertSpyCalls(allGroupChatsSpy, 2);
});
});
});
Loading