|
1 | 1 | import { Context } from 'grammy'; |
2 | 2 | import { BotReply } from './BotReply'; |
| 3 | +import { RateLimiter } from '../RateLimiter'; |
3 | 4 | export class ChatInfo { |
4 | 5 | constructor(private _ctx: Context) {} |
5 | 6 |
|
@@ -49,4 +50,67 @@ export class ChatInfo { |
49 | 50 | } |
50 | 51 | return undefined; |
51 | 52 | } |
| 53 | + async userIsAdmin(): Promise<boolean> { |
| 54 | + const userId = this._ctx.from!.id; |
| 55 | + const chatMember = await this._ctx.getChatMember(userId); |
| 56 | + |
| 57 | + return chatMember.status === 'administrator' || chatMember.status === 'creator'; |
| 58 | + } |
| 59 | + async userReplyIsAdmin(): Promise<boolean | undefined> { |
| 60 | + const reply = new BotReply(this._ctx); |
| 61 | + const repliedMessage = await reply.getReplyMessage(); |
| 62 | + if (repliedMessage?.from?.id) { |
| 63 | + const userId = repliedMessage.from.id; |
| 64 | + const chatMember = await this._ctx.getChatMember(userId); |
| 65 | + return chatMember.status === 'administrator' || chatMember.status === 'creator'; |
| 66 | + } |
| 67 | + return undefined; |
| 68 | + } |
| 69 | + async adminList(): Promise<string> { |
| 70 | + const admins = await this._ctx.api.getChatAdministrators(this._ctx.chat!.id); |
| 71 | + const adminList = admins |
| 72 | + .filter((admin) => !admin.user.is_bot) // Exclude bots from the admin list |
| 73 | + .map((admin) => `- ${admin.user.first_name} (@${admin.user.username || 'No username'})`) |
| 74 | + .join('\n'); |
| 75 | + return adminList; |
| 76 | + } |
| 77 | + |
| 78 | + async chatInfo(): Promise<{ |
| 79 | + groupName: string; |
| 80 | + groupType: string; |
| 81 | + groupDescription: string; |
| 82 | + memberCount: number; |
| 83 | + admins: string; |
| 84 | + inviteLink: string; |
| 85 | + } | null> { |
| 86 | + const group = this._ctx.chat; |
| 87 | + // Ensure we are in a group context |
| 88 | + if (!group || (group.type !== 'group' && group.type !== 'supergroup')) { |
| 89 | + return null; |
| 90 | + } |
| 91 | + const chatDetails = await this._ctx.api.getChat(group.id); |
| 92 | + |
| 93 | + // Extract information |
| 94 | + const groupName = chatDetails.title || 'Unnamed Group'; |
| 95 | + const groupDescription = chatDetails.description || 'No description available'; |
| 96 | + const groupType = chatDetails.type === 'supergroup' ? 'Supergroup' : 'Group'; |
| 97 | + const memberCount = await this._ctx.api.getChatMemberCount(group.id); |
| 98 | + const inviteLink = chatDetails.invite_link || 'Invite link not available'; |
| 99 | + const creator = await this._ctx.api.getChatAdministrators(group.id); |
| 100 | + const adminList = creator.filter((admin) => admin.status === 'administrator' || admin.status === 'creator'); |
| 101 | + const admins = adminList |
| 102 | + .map((admin) => { |
| 103 | + const username = admin.user.username ? `@${admin.user.username}` : admin.user.first_name; |
| 104 | + return `${username} (${admin.status === 'creator' ? 'Creator' : 'Admin'})`; |
| 105 | + }) |
| 106 | + .join(', '); |
| 107 | + return { groupName, groupType, groupDescription, memberCount, admins, inviteLink }; |
| 108 | + } |
| 109 | + async isAdmin(ctx: Context, userId: number): Promise<boolean> { |
| 110 | + if (!RateLimiter.limit(ctx.chat!.id)) { |
| 111 | + return false; |
| 112 | + } |
| 113 | + const chatAdmins = await ctx.getChatAdministrators(); |
| 114 | + return chatAdmins.some((admin) => admin.user.id === userId); |
| 115 | + } |
52 | 116 | } |
0 commit comments