feat(client): addChatMembers method and related bound method for Chat

This commit is contained in:
teidesu 2021-04-10 20:06:33 +03:00
parent f4907639b5
commit f0cf8a50a1
3 changed files with 87 additions and 1 deletions

View file

@ -13,6 +13,7 @@ import { signInBot } from './methods/auth/sign-in-bot'
import { signIn } from './methods/auth/sign-in' import { signIn } from './methods/auth/sign-in'
import { signUp } from './methods/auth/sign-up' import { signUp } from './methods/auth/sign-up'
import { start } from './methods/auth/start' import { start } from './methods/auth/start'
import { addChatMembers } from './methods/chats/add-chat-members'
import { getChatPreview } from './methods/chats/get-chat-preview' import { getChatPreview } from './methods/chats/get-chat-preview'
import { getChat } from './methods/chats/get-chat' import { getChat } from './methods/chats/get-chat'
import { getFullChat } from './methods/chats/get-full-chat' import { getFullChat } from './methods/chats/get-full-chat'
@ -325,6 +326,22 @@ export class TelegramClient extends BaseTelegramClient {
}): Promise<User> { }): Promise<User> {
return start.apply(this, arguments) return start.apply(this, arguments)
} }
/**
* Add new members to a group, supergroup or channel.
*
* @param chatId ID of the chat or its username
* @param users ID(s) of the users, their username(s) or phone(s).
* @param forwardCount(default: `100`)
* Number of old messages to be forwarded (0-100).
* Only applicable to legacy groups, ignored for supergroups and channels
*/
addChatMembers(
chatId: InputPeerLike,
users: MaybeArray<InputPeerLike>,
forwardCount?: number
): Promise<void> {
return addChatMembers.apply(this, arguments)
}
/** /**
* Get preview information about a private chat. * Get preview information about a private chat.
* *

View file

@ -0,0 +1,56 @@
import { TelegramClient } from '../../client'
import { InputPeerLike, MtCuteInvalidPeerTypeError } from '../../types'
import { MaybeArray } from '@mtcute/core'
import {
normalizeToInputChannel,
normalizeToInputPeer,
normalizeToInputUser,
} from '../../utils/peer-utils'
import { tl } from '@mtcute/tl'
/**
* Add new members to a group, supergroup or channel.
*
* @param chatId ID of the chat or its username
* @param users ID(s) of the users, their username(s) or phone(s).
* @param forwardCount
* Number of old messages to be forwarded (0-100).
* Only applicable to legacy groups, ignored for supergroups and channels
* @internal
*/
export async function addChatMembers(
this: TelegramClient,
chatId: InputPeerLike,
users: MaybeArray<InputPeerLike>,
forwardCount = 100
): Promise<void> {
const chat = await this.resolvePeer(chatId)
const input = normalizeToInputPeer(chat)
if (!Array.isArray(users)) users = [users]
if (input._ === 'inputPeerChat') {
for (const user of users) {
const p = normalizeToInputUser(await this.resolvePeer(user))
if (!p) continue
await this.call({
_: 'messages.addChatUser',
chatId: input.chatId,
userId: p,
fwdLimit: forwardCount,
})
}
} else if (input._ === 'inputPeerChannel') {
await this.call({
_: 'channels.inviteToChannel',
channel: normalizeToInputChannel(chat)!,
users: await Promise.all(
(users as InputPeerLike[]).map((u) =>
this.resolvePeer(u).then(normalizeToInputUser)
)
).then((res) => res.filter(Boolean)) as tl.TypeInputUser[],
fwdLimit: forwardCount,
})
} else throw new MtCuteInvalidPeerTypeError(chatId, 'chat or channel')
}

View file

@ -2,9 +2,10 @@ import { ChatPhoto } from './chat-photo'
import { tl } from '@mtcute/tl' import { tl } from '@mtcute/tl'
import { ChatPermissions } from './chat-permissions' import { ChatPermissions } from './chat-permissions'
import { TelegramClient } from '../../client' import { TelegramClient } from '../../client'
import { getMarkedPeerId } from '@mtcute/core' import { getMarkedPeerId, MaybeArray } from '@mtcute/core'
import { MtCuteArgumentError, MtCuteTypeAssertionError } from '../errors' import { MtCuteArgumentError, MtCuteTypeAssertionError } from '../errors'
import { makeInspectable } from '../utils' import { makeInspectable } from '../utils'
import { InputPeerLike } from './index'
export namespace Chat { export namespace Chat {
/** /**
@ -428,6 +429,18 @@ export class Chat {
async join(): Promise<void> { async join(): Promise<void> {
await this.client.joinChat(this.inputPeer) await this.client.joinChat(this.inputPeer)
} }
/**
* Add user(s) to this chat
*
* @param users ID(s) of the users, their username(s) or phone(s).
* @param forwardCount
* Number of old messages to be forwarded (0-100).
* Only applicable to legacy groups, ignored for supergroups and channels
*/
async addMembers(users: MaybeArray<InputPeerLike>, forwardCount?: number): Promise<void> {
return this.client.addChatMembers(this.inputPeer, users, forwardCount)
}
} }
makeInspectable(Chat) makeInspectable(Chat)